【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)
題干:
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?sdoesn'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.
Examples
Input
ababa 1 abOutput
2Input
codeforces 2 do csOutput
1Note
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.
題目大意:
? ?在一串文字中有k個字符對(2位)不允許出現,問最小修改數。輸入時的字符對保證兩個字符不同,且其中字符唯一
解題報告:
? ? 一個字串只含有某個對中的字母,這個字符對才可能存在,要破壞這個字串,就在改變出現次數最少的那個字符,由此尋找符合題意的字串,并且求出修改次數,疊加即可。(主要是這題說明了,這k對字符串都不是重復字符)
既然k個兩字符串中沒有重復的串,那么你刪除一些必要的字符后肯定不會造成新的不符合要求的串,所以只要統計下串中連續的不合法的字符的個數,取最小值就可以了。
? ? 關于造數據:比如第2個樣例的k對,也需要考慮一下類似于addcosd這種岔開的,(其實也沒事,因為禁止串的兩個字符不會同時消除,所以不會產生新的禁止串)
AC代碼:
#include <bits/stdc++.h> #define ll long long using namespace std;char s[100000 + 5]; int main() {int k,cnt1,cnt2;char tmp[15];ll ans = 0;cin>>s;int len = strlen(s);cin>>k;while(k--) {cin>>tmp;if(tmp[0] == tmp[1]) {//其實這題不用考慮這個for(int i = 0; i<len; i++) {while(s[i] == tmp[0] && i<len) ans++,i++;ans--;}continue;} cnt1=cnt2=0;for(int i = 0; i<len; i++) {cnt1=cnt2=0;while(s[i] == tmp[0] || s[i] == tmp[1]) {if(s[i] == tmp[0]) cnt1++;if(s[i] == tmp[1]) cnt2++;i++;}ans += min(cnt1,cnt2);}}printf("%lld\n",ans);return 0; }或者這樣寫:(熟悉這種if -> while句式)(很多地方都會用到,比如求約數和【 HDU - 1215 】七夕節(數論,約數和公式))
if(tmp[0] == tmp[1]) {for(int i = 0; i<len; i++) {if(s[i] == tmp[0] && i<len) {i++;while(s[i] == tmp[0] && i<len) ans++,i++;}}continue;}?
總結
以上是生活随笔為你收集整理的【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SUV坠落百米悬崖3人幸运生还!网友:日
- 下一篇: *【HDU - 1517】【POJ -