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
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

inputoutput
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
[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. using namespace std;  
  5. const int maxn = 100010;  
  6. int n;  
  7. char s[maxn], t[maxn];  
  8. int p[maxn];  
  9. bool mark[maxn];  
  10. bool kmp(char *T, char *P, int N, int M)  
  11. {  
  12.     int i, j;  
  13.     p[0] = j = -1;  
  14.     for (i = 1; i < M; i++)  
  15.     {  
  16.         while (j > -1 && P[j + 1] != P[i]) j = p[j];  
  17.         if (P[j + 1] == P[i]) j++;  
  18.         p[i] = j;  
  19.     }  
  20.     j = -1;  
  21.     for (i = 0; i < N; i++)  
  22.     {  
  23.         while (j > -1 && P[j + 1] != T[i]) j = p[j];  
  24.         if (P[j + 1] == T[i]) j++;  
  25.         if (j == M - 1)  
  26.         {  
  27.             for (int k = i + n; k >= i - M + 1 + n; --k)  
  28.                 mark[k] = 1;  
  29.             n += i + 2;  
  30.             return true;  
  31.         }  
  32.     }  
  33.     return false;  
  34. }  
  35. int main()  
  36. {  
  37.     char c;  
  38.     bool flag = 1;  
  39.     gets(s);  
  40.     n = 0;  
  41.     int ls = strlen(s);  
  42.     while (scanf("%s", t))  
  43.     {  
  44.         c = 0;  
  45.         scanf("%c", &c);  
  46.         if (flag)  
  47.         {  
  48.             if (!kmp(s + n, t, ls - n, strlen(t)))  
  49.             {  
  50.                 flag = 0;  
  51.                 break;  
  52.             }  
  53.             if (n >= ls) break;  
  54.         }  
  55.         if (c != ' 'break;  
  56.     }  
  57.     if (!flag) puts("I HAVE FAILED!!!");  
  58.     else  
  59.     {  
  60.         for (int i = 0; i < ls; i++)  
  61.             if (s[i] == ' ') putchar(s[i]);  
  62.             else if (!mark[i]) printf("_");  
  63.             else printf("%c", s[i]);  
  64.         printf("\n");  
  65.     }  
  66.     return 0;  
  67. }  
原创粉丝点击