URAL - 1732 - Ministry of Truth(KMP)
2013-08-29 11:43
399 查看
题意:输入两行字符串,问能否从第一行字符串中减掉一些字符(变成空格,连续的空格又合为1个空格)变成第二行字符串(每行字符串的长度 <= 100000)。
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1732
——>>好想哭……
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cry.gif)
如此题目,比赛5个小时,看到多模板,一直不敢下手敲KMP,认为是AC自动机或者后缀数组,还没敲完AC自动机比赛就Ended,过的队伍说用KMP,吃个饭回来,写了简单的KMP一交,立马AC,而且代码量较比赛时其他队伍过的少一半!我彻底无语了,想神马AC自动机!!!
——>>KMP匹配出来后,把匹配的位置标记,输出时就可以根据这些标记输出,下一次扫描的左端,就应该是这次匹配成功时的位置+2,要隔一个空格嘛~
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1732
——>>好想哭……
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cry.gif)
如此题目,比赛5个小时,看到多模板,一直不敢下手敲KMP,认为是AC自动机或者后缀数组,还没敲完AC自动机比赛就Ended,过的队伍说用KMP,吃个饭回来,写了简单的KMP一交,立马AC,而且代码量较比赛时其他队伍过的少一半!我彻底无语了,想神马AC自动机!!!
——>>KMP匹配出来后,把匹配的位置标记,输出时就可以根据这些标记输出,下一次扫描的左端,就应该是这次匹配成功时的位置+2,要隔一个空格嘛~
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
#include <cstdio> #include <cstring> using namespace std; const int maxn = 100000 + 10; char l1[maxn], s[maxn]; bool vis[maxn]; int f[maxn], L, R; void getFail(char *P){ int len = strlen(P); f[0] = f[1] = 0; for(int i = 1; i < len; i++){ int j = f[i]; while(j && P[j] != P[i]) j = f[j]; f[i+1] = P[i] == P[j] ? j+1 : 0; } } bool KMP(char *T, char *P){ int m = strlen(P); getFail(P); int j = 0; for(int i = L; i < R; i++){ while(j && P[j] != T[i])j = f[j]; if(P[j] == T[i]) j++; if(j == m){ for(int k = i; k > i-m; k--) vis[k] = 1; L = i + 2; return 1; } } return 0; } int main() { while(gets(l1)){ int len = strlen(l1); L = 0, R = len; memset(vis, 0, sizeof(vis)); bool ok = 1; while(scanf("%s", s)){ if(ok && !KMP(l1, s)) ok = 0; if(getchar() != ' ') break; } if(ok){ for(int i = 0; i < len; i++) if(vis[i] || l1[i] == ' ') putchar(l1[i]); else putchar('_'); puts(""); } else puts("I HAVE FAILED!!!"); } 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
- URAL 1732. Ministry of Truth ( KMP 多模式串匹配 )
- Ural 1732. Ministry of Truth 多串匹配KMP
- Gym 100187L - Ministry of Truth
- Ural 1732. Ministry of Truth 多串匹配KMP
- URAL 1029 Ministry
- Moment of Truth(关键时刻)
- URAL 1996 Cipher Message 3 (FFT + KMP)
- URAL 1014 Product of Digits
- 【URAL 1057】 Amount of Degrees 【数位DP】
- Ural 1014-The Product of Digits
- URAL 1057 Amount of Degrees 数位dp
- 关键时刻(Moment Of Truth)
- Ural 1613-For Fans of Statistics(vector)
- ural 1057 - Amount of Degrees(数位统计)
- ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.al
- 【Ural】【1057】Amount of degrees