问在第一个串中删掉几个字符能否得到第二个串。注意在第二个串中不连续的单词在第一个串中也必须不连续。

一组数据:

Input:

abababbbbabab
bb aba ab

Output:

I HAVE FAILED!!!

#include <cstdio>
#include <cstring>
#include <cstdlib> using namespace std; const int MAXN = ; char str[MAXN];
char tmp[MAXN];
int nextval[MAXN];
int flag[MAXN];
int strL, tmpL; void getNextval( char* s, int* nextval, int length )
{
int i=,j=-;
nextval[]=-;
while(i<length)
{
if(j==-||s[i]==s[j])
{
++i;
++j;
//next[i]=j;
if (s[i]!=s[j])
nextval[i]=j;
else
nextval[i]=nextval[j];
}
else
j=nextval[j];
}
} int KMP( char *t, char *s, int lenth, int len ) //s为主串,t为模式串
{
getNextval( t, nextval, lenth );
int i = , j = ;
while ( j < len )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lenth ) return j;
}
else i = nextval[i];
}
return -;
} int main()
{
while ( gets( str ) != NULL )
{
strL = strlen(str);
memset( flag, -, sizeof(flag) );
bool ok = true;
int i = ;
while ( )
{
scanf( "%s", tmp );
tmpL = strlen(tmp); int ans = KMP( tmp, &str[i], tmpL, strL - i ); //puts(tmp);
//puts(&str[i]); if ( ans == - ) ok = false;
else flag[ i + ans - tmpL ] = tmpL; char ch = getchar(); if ( ch == '\n' ) break;
i += ans + ;
}
if ( ok )
{
for ( int i = ; i < strL; ++i )
{
if ( str[i] == ' ' ) putchar(' ');
else if ( flag[i] == - ) putchar('_');
else
{
for ( int j = ; j < flag[i]; ++j )
putchar( str[i+j] );
i += flag[i] - ;
}
}
puts("");
}
else puts("I HAVE FAILED!!!");
}
return ;
}

URAL 1732. Ministry of Truth ( KMP 多模式串匹配 )的更多相关文章

  1. URAL 1732 Ministry of Truth(KMP)

    Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...

  2. 模式串匹配之KMP算法

    模式串匹配之KMP算法 KMP算法 模式值计算(next[j]) (1) next[0]=-1,  第一个字符模式值为-1 (2) next[j]=-1, T中下标为j的字符与首字符相同,且j前面的1 ...

  3. DP+路径 URAL 1029 Ministry

    题目传送门 /* 题意:就是从上到下,找到最短路,输出路径 DP+路径:状态转移方程:dp[i][j] = min (dp[i-1][j], dp[i][j-1], dp[i][j+1]) + a[[ ...

  4. codeforces Gym 100187L L. Ministry of Truth 水题

    L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  5. 模式串匹配KMP详解

    关于KMP模式串匹配网上蛮多的. 对于KMP有自己理解所以写下来希望能够对你们的学习有帮助. 之前暑假的时候学过,然后好长时间没用发现又忘了,现在再看看发现有了新的理解. ============== ...

  6. 单模式串匹配----浅谈kmp算法

    模式串匹配,顾名思义,就是看一个串是否在另一个串中出现,出现了几次,在哪个位置出现: p.s.  模式串是前者,并且,我们称后一个 (也就是被匹配的串)为文本串: 在这篇博客的代码里,s1均为文本串, ...

  7. hdu 1686 Oulipo 【KMP】(计算模式串匹配的次数——与已匹配的字串可以有交集)

    题目链接:https://vjudge.net/contest/220679#problem/B 题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于100 ...

  8. Ural 1029 Ministry 题解

    目录 Ural 1029 Ministry 题解 题意 题解 程序 Ural 1029 Ministry 题解 题意 给定一个\(n\times m(1\le n \le10,1\le m \le50 ...

  9. AC自动机——多模式串匹配的算法思想

    标准KMP算法用于单一模式串的匹配,即在母串中寻求一个模式串的匹配,但是现在又存在这样的一个问题,如果同时给出多个模式串,要求找到这一系列模式串在母串存在的匹配个数,我们应该如何处理呢? 基于KMP算 ...

随机推荐

  1. B1954 The xor-longest Path

    爆炸入口 给定一颗带权值的,节点数为n的树,求树上路径最大异或和. solution: 先dfs将所有点到根的异或和算出来.然后放进tire树中贪心. #include<cstdio> # ...

  2. Mathematica 讲座

    Mathematica 讲座笔记本 [下载] 第一章 Mathematica 简介 [观看] [下载] 第二章 Mathematica 界面和编程语言 [观看] [下载] 第三章 符号运算 [观看] ...

  3. Oracle 启动 停止JOB

    转自:https://www.cnblogs.com/qianbing/p/6971633.html --查看job下次执行时间以及间隔时间 '; --启动job ); --停用job EXEC DB ...

  4. java基础 数组 Set Map 集合综合应用 生成带0的随机字符串 "00000001" 水果商品号问题

    package com.swift.test01; /*有四种水果(苹果,香蕉,西瓜,橘子) 1.给每种水果设定一个商品号,商品号是8个0-9的随机数,商品号码不能重复, 最小值 "0000 ...

  5. [JZOJ] 5905. 黑暗之魂(darksoul)

    基环树直径裸题 分别在子树做DP,环上做DP,环上可以用单调队列优化到\(O(n)\) 写起来很麻烦 #include<algorithm> #include<iostream> ...

  6. JS之执行上下文

    执行上下文(execution context),是JS中的一个很重要的概念.它对于我们理解函数定义,执行时都做了什么有着很大的意义.理解它我们才能明白我们常说的函数声明提升,作用域链,闭包等原理. ...

  7. 对Neural Machine Translation by Jointly Learning to Align and Translate论文的详解

    读论文 Neural Machine Translation by Jointly Learning to Align and Translate 这个论文是在NLP中第一个使用attention机制 ...

  8. 1016-06-首页20-封装工具条-有控件在viewDidLoad的时候距离顶部是0--到了viewWillAppear或viewDidAppear系统就加了64

  9. Triangular Sums 南阳acm122

    Triangular Sums 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 The nth Triangular number, T(n) = 1 + … + n ...

  10. SPOJ1026 概率DP

    Favorite Dice BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorit ...