URAL 1732. Ministry of Truth ( KMP 多模式串匹配 )
问在第一个串中删掉几个字符能否得到第二个串。注意在第二个串中不连续的单词在第一个串中也必须不连续。
一组数据:
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 多模式串匹配 )的更多相关文章
- URAL 1732 Ministry of Truth(KMP)
Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...
- 模式串匹配之KMP算法
模式串匹配之KMP算法 KMP算法 模式值计算(next[j]) (1) next[0]=-1, 第一个字符模式值为-1 (2) next[j]=-1, T中下标为j的字符与首字符相同,且j前面的1 ...
- DP+路径 URAL 1029 Ministry
题目传送门 /* 题意:就是从上到下,找到最短路,输出路径 DP+路径:状态转移方程:dp[i][j] = min (dp[i-1][j], dp[i][j-1], dp[i][j+1]) + a[[ ...
- 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 ...
- 模式串匹配KMP详解
关于KMP模式串匹配网上蛮多的. 对于KMP有自己理解所以写下来希望能够对你们的学习有帮助. 之前暑假的时候学过,然后好长时间没用发现又忘了,现在再看看发现有了新的理解. ============== ...
- 单模式串匹配----浅谈kmp算法
模式串匹配,顾名思义,就是看一个串是否在另一个串中出现,出现了几次,在哪个位置出现: p.s. 模式串是前者,并且,我们称后一个 (也就是被匹配的串)为文本串: 在这篇博客的代码里,s1均为文本串, ...
- hdu 1686 Oulipo 【KMP】(计算模式串匹配的次数——与已匹配的字串可以有交集)
题目链接:https://vjudge.net/contest/220679#problem/B 题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于100 ...
- Ural 1029 Ministry 题解
目录 Ural 1029 Ministry 题解 题意 题解 程序 Ural 1029 Ministry 题解 题意 给定一个\(n\times m(1\le n \le10,1\le m \le50 ...
- AC自动机——多模式串匹配的算法思想
标准KMP算法用于单一模式串的匹配,即在母串中寻求一个模式串的匹配,但是现在又存在这样的一个问题,如果同时给出多个模式串,要求找到这一系列模式串在母串存在的匹配个数,我们应该如何处理呢? 基于KMP算 ...
随机推荐
- AVR446_Linear speed control of stepper motor步进电机曲线分析
1.1. 单片机代码处理 // 定义定时器预分频,定时器实际时钟频率为:72MHz/(STEPMOTOR_TIMx_PRESCALER+1) #define STEPMOTOR_TIM_PRESCA ...
- Spring Cloud 入门 Eureka-Client服务提供
前面文章介绍了如果创建“服务注册中心”,现在创建“服务提供者”,并向服务注册中心注册自己,在服务提供方中尝试着提供一个接口来获取当前所有的服务信息. 先,创建一个基本的Spring Boot应用.命名 ...
- 【操作系统作业-lab4】 linux 多线程编程和调度器
linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...
- Cluster - HA -keepalived
学习须知 VRRP:https://www.cnblogs.com/aftree/p/9376427.html 需求 集群中,对后端RealServer的状态做检测,实现自动化问题检测和问题自动处理机 ...
- 封装动态数组类Array
功能: 1.增.删.改.查 2.扩容.缩容 3.复杂度分析 4.均摊复杂度 5.复杂度震荡 分析动态数组的时间复杂度: 分析resize的时间复杂度: public class Array<E& ...
- TCL之表达式
- Redis ---------- key的操作
key命名规则 除空格和\n,其他都可以 select db -index选择数据库 例1 select 5 例2 type 查看数据key类型 type name 例3 keys pat ...
- Linux系统Mini版配置相关
一:修改ip 编辑:vi /etc/sysconfig/network-sc/ifcfg-eth0 配置如下图:
- C# WPF使一个窗口挡在最前面,且不处理此窗口就不能点击其他窗口
模态框弹出就可以了.窗口用ShowDialog调用. 可以在子窗体里把WindowStartupLocation设置为CenterOwner, 让子窗体在MainWindow的中心位置显示
- 30-RoutingMiddleware介绍以及MVC引入
1-构建路由 public class Startup { // This method gets called by the runtime. Use this method to add serv ...