Hometask

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 (0k13) 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 test(s)

Input

ababa

1

ab

Output

2

Input

codeforces

2

do

cs

Output

1

dp[i][j] 26 i 1len0 len – 1wa

^{?}Download sourceCode.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <vector> #include <queue> #include <algorithm> #include <cmath> using namespace std; #define INF 1000010 int m[30]; char strr[100010]; int str[100010]; int dp[100010][30]; int len; void init(){ int i, j; char cc = 'a'; len = strlen(strr); for(i = 0; i <= 26; i ++){ m[i] = 100; for(j = 0; j <= len; j ++) dp[j][i] = INF; } for(i = 1; i <= len; i ++){ str[i] = strr[i - 1] - 'a'; } } int main(){ int k; int i, j; while(~scanf("%s", strr)){ init(); scanf("%d", &k); getchar(); for(i = 1; i <= k; i ++){ char cc1 = getchar(); char cc2 = getchar(); getchar(); int c1 = cc1 - 'a' ; int c2 = cc2 - 'a' ; m[c1] = c2; m[c2] = c1; } dp[0][26] = 0; for(i = 1; i <= len; i ++){ for(j = 0; j <= 26; j ++) if(dp[i - 1][j] != INF){ //maxm = max(maxm, dp[i - 1][i - j]); dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1); if(m[str[i]] != j){ dp[i][str[i]] = min(dp[i][str[i]], dp[i - 1][j]); } } } int ans = INF; for(i = 0; i < 26; i ++){ //printf("%d\n", dp[len - 1][i]); ans = min(ans, dp[len][i]); } printf("%d\n", ans); } return 0; } |

texttexttext`code`

more code

~~~~