Ural 1732. Ministry of Truth 多串匹配KMP
来源:互联网 发布:matlab教程矩阵 编辑:程序博客网 时间:2024/05/17 10:08
1732. Ministry of Truth
Time limit: 1.0 second
Memory limit: 64 MB
Memory 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 utterances are 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 HAVE FAILED!!!” in the only line. Otherwise, output the original utterance replacing the letters that are to be deleted with the underscore character.
Samples
Preved to MedvedPreved Me
Preved __ Me____
this is impossibleim possible
I HAVE FAILED!!!
Problem Author: Alex Samsonov (prepared by Dmitry Ivankov)
Problem Source: XIV Open USU Championship
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 HAVE 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
- Gym 100187L - Ministry of Truth
- ural 1029 Ministry
- ural 1029 Ministry
- ural 1029. Ministry (dp)
- ural 1029. Ministry
- URAL 1029 Ministry
- timus 1029. Ministry URAL 解题报告
- URAL 1029 Ministry(记录路径的dp)
- Ministry
- KMP串匹配算法
- KMP串匹配算法
- KMP串匹配算法
- 最新3D思维导图软件imindmap6破解版/中文汉化版免费下载~乐趣尽在其中
- java集合类
- Jump Game II (最小步数到达终点,贪心) 【leetcode】
- HDU 4721 Food and Productivity (二分+树状数组)
- ultraedit
- Ural 1732. Ministry of Truth 多串匹配KMP
- URAL 1741 Communication Fiend
- 比较两个文件相同的地方
- POJ 2479 Maximum sum(DP)
- 调用栈
- C语言运算符优先级 详细列表
- CODE 34: Recover Binary Search Tree
- priority_queue用法小结
- hdu 1890伸展树(splay tree)区间翻转