KMP入门题目[不定期更新]
HDU 1711 Number Sequence(模板题)
#include <cstdio> const int MAXN = ;
const int MAXL = ; int N, M; int textS[MAXN];
int tarS[MAXL];
int next[MAXL]; void GetNextVal( int* s, int* nextval, int len )
{
int i = , j = -;
nextval[] = -;
while ( i < len )
{
if ( j == - || s[i] == s[j] )
{
++i, ++j;
nextval[i] = j;
}
else j = nextval[j];
}
return;
} int KMP( int *t, int lent, int *s, int lens )
{
GetNextVal( t, next, lent );
int i = , j = ;
while ( j < lens )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lent ) return j;
}
else i = next[i];
}
return -;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d%d", &N, &M );
for ( int i = ; i < N; ++i ) scanf( "%d", &textS[i] );
for ( int i = ; i < M; ++i ) scanf( "%d", &tarS[i] ); int ans = KMP( tarS, M, textS, N );
if ( ans < ) printf( "%d\n", ans );
else printf("%d\n", ans - M + );
}
return ;
}
HDU 1686 Oulipo(模板题)
#include <cstdio>
#include <cstring>
#include <cstdlib> const int MAXN = ;
const int MAXL = ; char tarS[MAXL];
char testS[MAXN];
int next[MAXL]; void GetNextVal( char *s, int len, int *p )
{
int i = , j = -;
p[] = -;
while ( i < len )
{
if ( j == - || s[i] == s[j] )
{
++i, ++j;
p[i] = j;
}
else j = p[j];
}
return;
} int KMP( char *t, char *s )
{
int cnt = ;
int lent = strlen(t);
int lens = strlen(s);
GetNextVal( t, lent, next ); int i = , j = ;
while ( j < lens )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lent )
{
++cnt;
i = next[i];
}
}
else i = next[i];
} return cnt;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%s%s", tarS, testS );
printf("%d\n", KMP( tarS, testS ) );
}
return ;
}
HDU 2203 亲和串
#include <cstdio>
#include <cstring>
#include <cstdlib> const int MAXN = ; char testS[ MAXN << ];
char tarS[MAXN];
char temp[MAXN];
int next[MAXN]; void GetNextVal( char *s, int len, int *p )
{
int i = , j = -;
p[] = -;
while ( i < len )
{
if ( j == - || s[i] == s[j] )
{
++i, ++j;
p[i] = j;
}
else j = p[j];
}
return;
} bool KMP( char *s, int lenS, char *t, int lenT )
{
GetNextVal( t, lenT, next );
int i = , j = ;
while ( j < lenS )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lenT ) return true;
}
else i = next[i];
}
return false;
} int main()
{
while ( ~scanf( "%s", temp ) )
{
scanf("%s", tarS );
int lenS = strlen(temp);
int lenT = strlen(tarS);
if ( lenT > lenS )
{
puts("no");
continue;
} strcpy( testS, temp );
strcpy( &testS[lenS], temp ); KMP( testS, lenS + lenS, tarS, lenT ) ? puts("yes") : puts("no"); }
return ;
}
POJ 3450 Corporate Identity KMP+二分
二分串长,求出每个串长的所有子串,只要找到一个符合的子串即可。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ;
const int MAXL = ; int N;
char str[MAXN][MAXL];
char temp[MAXL];
char anser[MAXL];
int nextval[MAXL];
int len[MAXN]; void GetNextVal( char *s, int lenth )
{
int i = , j = -;
nextval[] = -;
while ( i < lenth )
{
if ( j == - || s[i] == s[j] )
{
++i, ++j;
if ( s[i] != s[j] ) nextval[i] = j;
else nextval[i] = nextval[j];
}
else j = nextval[j];
}
return;
} bool KMP( char *t, char *s, int lenth, int lenn )
{
GetNextVal( t, lenth );
int i = , j = ;
while ( j < lenn )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lenth ) return true;
}
else i = nextval[i];
//if ( i == lenth ) return true;
}
return false;
} bool check( int tpL )
{
bool flag = false;
bool ok = false;
for ( int st = ; st + tpL - < len[]; ++st )
{
for ( int k = ; k < tpL; ++k )
temp[k] = str[][ st + k ];
temp[ tpL ] = '\0';
//puts(temp); ok = true;
for ( int i = ; i < N; ++i )
if ( !KMP( temp, str[i], tpL, len[i] ) )
{
ok = false;
break;
}
if ( ok )
{
flag = true;
if ( anser[] == '\0' ||
( strlen(anser) == strlen(temp) && strcmp( anser, temp ) > ) || ( strlen(anser) < strlen(temp) ) )
strcpy( anser, temp );
}
}
return flag;
} int main()
{
while ( scanf( "%d", &N ), N )
{
int bound = << ;
for ( int i = ; i < N; ++i )
{
scanf( "%s", str[i] );
len[i] = strlen( str[i] );
bound = min( bound, len[i] );
} int low = ;
int high = bound;
int mid, ans = ;
anser[] = '\0';
while ( low <= high )
{
mid = ( low + high ) >> ;
if ( check( mid ) )
{
ans = mid;
low = mid + ;
}
else high = mid - ;
} if ( !ans ) puts("IDENTITY LOST");
else puts(anser);
}
return ;
}
POJ 2752 Seek the Name, Seek the Fame
next函数的应用
#include <cstdio>
#include <cstring>
#include <cstdlib> const int MAXN = ; char str[MAXN];
int next[MAXN];
int ans[MAXN];
int len; void getNext( char *s )
{
int i = , j = -;
next[] = -;
while ( i < len )
{
if ( j == - || s[i] == s[j] )
{
++i, ++j;
next[i] = j;
}
else j = next[j];
}
return;
} int main()
{
while ( ~scanf( "%s", str ) )
{
len = strlen(str);
getNext( str );
int cnt = ;
ans[] = len;
int i = len;
while ( next[i] > )
{
ans[ ++cnt ] = next[i];
i = next[i];
}
for ( ; cnt >= ; --cnt )
{
printf( "%d", ans[cnt] );
if ( cnt ) putchar(' ');
}
puts("");
}
return ;
}
HDU 2087 剪花布条
求一个串在另一个串中的出现次数,KMP模板题
#include <cstdio>
#include <cstring>
#include <cstdlib> const int MAXN = ; char str[MAXN];
char tmp[MAXN];
int nextval[MAXN];
int cnt; 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];
}
} void KMP( char *t, char *s ) //s为主串,t为模式串
{
int lenth = strlen(t);
int len = strlen(s);
getNextval( t, nextval, lenth );
int i = , j = ;
while ( j < len )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lenth )
{
++cnt;
i = ;
}
}
else i = nextval[i];
}
return;
} int main()
{
while ( scanf( "%s", str ) == && str[] != '#' )
{
scanf( "%s", tmp );
cnt = ;
KMP( tmp, str );
printf( "%d\n", cnt );
}
return ;
}
HDU 2594 Simpsons’ Hidden Talents (next函数应用)
把两个串连接起来,中间用一个没出现过的字符分隔开,直接输出最后一个字符的next的值。
#include <cstdio>
#include <cstring>
#include <cstdlib> using namespace std; const int MAXN = ; char str[MAXN << ];
char tmp[MAXN];
int nextval[MAXN << ];
int length; void getNext(char s[],int next[])
{
length=strlen(s);
int i=,j=-;
next[]=-;
while(i<length)
{
if(j==-||s[i]==s[j])
{
++i;
++j;
next[i]=j;
}
else
j=next[j];
}
return;
} int main()
{
while ( scanf( "%s%s", str, tmp ) == )
{
int len = strlen(str);
str[len] = '$';
str[len + ] = '\0';
strcat( str, tmp );
getNext( str, nextval );
str[ nextval[length] ] = '\0';
if ( nextval[length] > )
printf( "%s %d\n", str, nextval[length] );
else puts("");
}
return ;
}
URAL 1423. String Tale ( KMP模板题 )
#include <cstdio>
#include <cstring>
#include <cstdlib> using namespace std; const int MAXN = ; int nextval[MAXN << ];
char str[MAXN << ];
char tmp1[MAXN];
char tmp2[MAXN];
int len; 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 ) //s为主串,t为模式串
{
int lenth = strlen(t);
int len = strlen(s);
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 ( scanf( "%d", &len ) == )
{
scanf( "%s%s", tmp1, tmp2 );
strcpy( str, tmp1 );
strcat( str, tmp1 );
int ans = KMP( tmp2, str );
//printf( "ans = %d\n", ans );
if ( ans == - ) puts("-1");
else if ( ans == len ) puts("");
else printf( "%d\n", len + len - ans );
}
return ;
}
KMP入门题目[不定期更新]的更多相关文章
- net core 小坑杂记之配置文件读取(不定期更新)
其实很早就想写了,原想等积累差不多了再写的,但是发现遇到一个当时记下效果会比较好,所以就不定期更新这个系列了,后面获取会整个整理一下. 此篇记载net core入门时踩的一些坑,网上教程太少了,也不规 ...
- 基于C/S架构的3D对战网络游戏C++框架_【不定期更新通知】
由于笔者最近有比赛项目要赶,这个基于C/S架构的3D对战网络游戏C++框架也遇到了一点瓶颈需要点时间沉淀,所以近一段时间不能保证每天更新了,会保持不定期更新.同时近期笔者也会多分享一些已经做过学过的C ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
- 从壹开始前后端分离 [.netCore 不定期更新 ] 三十五║ 完美实现全局异常日志记录
缘起 哈喽我是不定期更新的日常,昨天群里小伙伴问到了记录日志,当然,以前我也挖过这个坑,后来一直没有来得及填上,也想着 swagger 一直又有错误信息展示的功能,就迟迟没有添加这个功能,不过昨天夜里 ...
- React性能优化记录(不定期更新)
React性能优化记录(不定期更新) 1. 使用PureComponent代替Component 在新建组件的时候需要继承Component会用到以下代码 import React,{Componen ...
- 不定期更新的IDEA功能整理
目录 不定期更新的IDEA功能整理 idea 命令 Preferences 和 Project Structure Keymap HTTP Proxy Postfix Completion 插件 插件 ...
- 采用异步来实现重新连接服务器或者重新启动服务 C#中类的属性的获取 SignalR2简易数据看板演示 C#动态调用泛型类、泛型方法 asp .net core Get raw request. 从壹开始前后端分离[.NetCore 不定期更新] 38 ║自动初始化数据库
采用异步来实现重新连接服务器或者重新启动服务 开启异步监听,不会导致主线程的堵塞,在服务异常断开后一直检测重新连接服务,成功连接服务后通知各个注册的客户端! #region 检测断线并重连OPC服务 ...
- poj 2186 强连通入门题目
每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...
- JavaScript中的小陷阱(不定期更新。。)
1. var scores = [1, 2, 3]; var total = 0; for (var score in scores) { total += score; } var mean = t ...
随机推荐
- 为什么我们使用192.168.0.1作为内网ip
私有IP地址是一段保留的IP地址.只是使用在局域网中,在Internet上是不使用的. 私有IP地址的范围有: 10.0.0.0-10.255.255.255 172.16.0.0—172.31.25 ...
- 有关UIView、subview的几个基础知识点-IOS开发 (实例)
环境是xcode4.3 首先要弄懂几个基本的概念. 一)三个结构体:CGPoint.CGSize.CGRect 1. CGPoint /* Points. */ struct CGPoint { C ...
- Java的别名机制
基本类型存储了实际的数值,而并非指向一个对象的引用,所以在为其赋值的时候,是直接将一个地方的内容复制到另一个地方. 但是在为对象"赋值"的时候,情况却发生了变化.对一个对象进行操作 ...
- 【UIScrollView】基本方法+基本描述
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )]; scrollView.backgroundColor = [ ...
- Css compatibility
http://meiert.com/en/indices/css-properties/ http://www.standardista.com/css3/css3-selector-browser- ...
- sql Mirroring
http://www.codeproject.com/Articles/109236/Mirroring-a-SQL-Server-Database-is-not-as-hard-as http:// ...
- C#更改控制台文本颜色
C#更改控制台文本的前景色和背景色 关键字:C# NET 控制台 前景色 背景色地址:http://www.cnblogs.com/txw1958/archive/2012/12/07/cshar ...
- iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述
本文档尝试用Video Toolbox进行H.265(HEVC)硬件编码,视频源为iPhone后置摄像头.去年做完硬解H.264,没做编码,技能上感觉有些缺失.正好刚才发现CMFormatDescri ...
- android开发,assets下面的资源文件不会变化/改动
我再调试asserts下面的资源文件,发现我改动assets下面的文件内容,在真机上测试的时候还是最原先的内容,没有变,后来,卸载,重装就ok了. 原因: assets下面的资源文件,若覆盖重装,则里 ...
- memcached+memadmin
一.安装apache yum install httpd 二.安装php yum install php* 三.apache配置 vi /etc/httpd/conf/httpd.conf 添加Add ...