Ural 1732. Ministry of Truth 多串匹配KMP
2013-07-28 16:52
225 查看
1732. Ministry of Truth
Time limit: 1.0 secondMemory limit: 64 MB
In whiteblack on blackwhite is written the utterance that has been censored by the Ministry of Truth. Its author has already disappeared along with his whole history, and now, while Big Brother is watching
somebody else, you, as an ordinary official of the Minitrue, have to delete some letters from the utterance so that another utterance will appear, which has been approved of by the Ministry.
The Ministry of Truth defines a word as a nonempty sequence of English letters and an utterance as a sequence of one or more words separated with one or more spaces. There can also
be spaces before the first word and after the last word of an utterance. In order to compare two utterances, one should delete all the leading and trailing spaces and replace each block of consecutive spaces with one space. If the resulting strings coincide,
then the utterances are considered to be equal. When the official deletes a letter from the utterance, this letter turns into a space.
Input
The first line contains the original utterance and the second line contains the utterance that must be obtained. The length of each utterance is at most 100000 symbols. The words in both utterancesare separated with exactly one space; there are no leading or trailing spaces in each line. The original and the required utterances are different.
Output
If you can't carry out your order, output “I H***E FAILED!!!” in the only line. Otherwise, output the original utterance replacing the letters that are to be deleted with the underscore character.Samples
input | output |
---|---|
Preved to Medved Preved Me | Preved __ Me____ |
this is impossible im possible | I H***E FAILED!!! |
Problem Source: XIV Open USU Championship
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; const int maxn = 100010; int n; char s[maxn], t[maxn]; int p[maxn]; bool mark[maxn]; bool kmp(char *T, char *P, int N, int M) { int i, j; p[0] = j = -1; for (i = 1; i < M; i++) { while (j > -1 && P[j + 1] != P[i]) j = p[j]; if (P[j + 1] == P[i]) j++; p[i] = j; } j = -1; for (i = 0; i < N; i++) { while (j > -1 && P[j + 1] != T[i]) j = p[j]; if (P[j + 1] == T[i]) j++; if (j == M - 1) { for (int k = i + n; k >= i - M + 1 + n; --k) mark[k] = 1; n += i + 2; return true; } } return false; } int main() { char c; bool flag = 1; gets(s); n = 0; int ls = strlen(s); while (scanf("%s", t)) { c = 0; scanf("%c", &c); if (flag) { if (!kmp(s + n, t, ls - n, strlen(t))) { flag = 0; break; } if (n >= ls) break; } if (c != ' ') break; } if (!flag) puts("I H***E FAILED!!!"); else { for (int i = 0; i < ls; i++) if (s[i] == ' ') putchar(s[i]); else if (!mark[i]) printf("_"); else printf("%c", s[i]); printf("\n"); } return 0; }
相关文章推荐
- URAL 1732. Ministry of Truth ( KMP 多模式串匹配 )
- Ural 1732. Ministry of Truth 多串匹配KMP
- URAL 1732 Ministry of Truth KMP
- URAL - 1732 - Ministry of Truth(KMP)
- URAL 1732 . Ministry of Truth KMP
- URAL 1732 Ministry of Truth(KMP)
- [数据结构] KMP字符串匹配
- fzu 1926 填空 【kmp字符串匹配】
- KMP(字符串匹配)算法
- 【KMP】SDUTOJ2125 串结构练习——字符串匹配
- KMP字符串匹配
- hihocoder#1015 (KMP字符串匹配次数)
- KMP、BM、Sunday等字符串匹配算法及实现
- 查找-基于DFA的KMP字符串匹配
- Algorithm Gossip (11) KMP字符串匹配
- KMP模式串匹配模板
- 洛谷 3375_[模板]KMP字符串匹配_KMP
- Tyvj 2016 (Clover 9) 外星人 字符串匹配:KMP
- 【kmp字符串匹配】hdu 2087 剪花布条
- 字符串匹配的KMP算法(简单清晰的认识KMP)