Hometask

Time Limit: 2000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 154A
64-bit integer IO format: %I64d      Java class name: (Any)

 

Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.

Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair.

Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".

 

Input

The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105.

The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters.

Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.

 

Output

Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.

 

Sample Input

Input
ababa
1
ab
Output
2
Input
codeforces
2
do
cs
Output
1

Hint

In the first sample you should remove two letters b.

In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.

 

Source

 
 
 
解题:
 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
char str[],s[];
int main(){
int n,i,j,ans,lt,rt;
while(~scanf("%s",str)){
scanf("%d",&n);
for(ans = i = ; i < n; i++){
scanf("%s",s);
for(j = ; str[j]; j++){
lt = rt = ;
while(str[j] == s[] || str[j] == s[]){
if(str[j] == s[]) lt++;
else rt++;
j++;
}
ans += min(lt,rt);
}
}
printf("%d\n",ans);
}
return ;
}
 
 
 

最新文章

  1. 【转载】Shell判断字符串包含关系的几种方法
  2. ArcGIS JS 学习笔记4 实现地图联动
  3. gui2
  4. 【原】gulp快速入门
  5. Yii源码阅读笔记(二十一)——请求处理流程
  6. 【jmeter】测试报告优化&lt;二&gt;
  7. 在VMware虚拟机中配置DOS汇编开发环境!!
  8. java替换字符串和用indexof查找字符
  9. 我使用过的Linux命令之date - 显示、修改系统日期时间
  10. 机顶盒加密系统流程 ECM EMM CW SK
  11. Linux学习之nfs安装配置
  12. BibTex (.bib) 文件的凝视
  13. UVA 10689 Yet another Number Sequence
  14. MariaDB扩展特性--虚拟列
  15. break用法
  16. Python爬虫入门之Urllib库的基本使用
  17. Java Integer常量池——IntegerCache内部类
  18. JAVA实现调用微信js-sdk扫一扫
  19. GPIO口的输入输出模式
  20. 初次使用ets

热门文章

  1. solrJ的使用--覆盖创建索引,查询,删除索引【转自http://blog.sina.com.cn/s/blog_64ac3ab10100t3mq.html】
  2. storm的3节点集群详细启动步骤(非HA和HA)(图文详解)
  3. php使用json_decode返回NULL
  4. [转]合理使用ArrayMap代替HashMap
  5. poj1857 To Europe! To Europe!
  6. Android开发中SharedPreferences的使用
  7. js数组引用
  8. Java Script 学习笔记(一)
  9. qt QTableView/QTableWidget样式设置
  10. 项目中常用git命令操作指令(一般正常的话够用不够再看相关git命令)