Xtu summer individual 2 C-hometask

Source: Internet
Author: User
HometaskTime Limit: 2000 msmemory limit: 262144 kbthis problem will be judged on codeforces. Original ID: 154a
64-bit integer Io format: % i64d Java class name: (any)

Sergey attends lessons ofN-Ish language. Each lesson he has es a hometask. This time the task is to translate some sentence toN-Ish language. Sentences ofN-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 grammarN-Ish. The spelling rules state thatN-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 stringS, Consisting of lowercase Latin letters-that's the initial sentence inN-Ish, written by Sergey. The length of stringSDoesn't exceed 105.

The next line contains integerK(0 bytes ≤ bytesKLimit ≤ limit 13)-the number of forbidden pairs of letters.

NextKLines 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 wrongly ded 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 shoshould remove two letters B.

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

Sourcecodeforces round #109 (Div. 1) solution:
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 #define INF 0x3f3f3f11 using namespace std;12 char str[100010],s[3];13 int main(){14     int n,i,j,ans,lt,rt;15     while(~scanf("%s",str)){16         scanf("%d",&n);17         for(ans = i = 0; i < n; i++){18             scanf("%s",s);19             for(j = 0; str[j]; j++){20                 lt = rt = 0;21                 while(str[j] == s[0] || str[j] == s[1]){22                     if(str[j] == s[0]) lt++;23                     else rt++;24                     j++;25                 }26                 ans += min(lt,rt);27             }28         }29         printf("%d\n",ans);30     }31     return 0;32 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.