【HDU 4763】Theme Section(KMP)
2024-09-08 14:59:36
这题数据水的一B。直接暴力都能够过。
比赛的时候暴力过的。回头依照正法做了一发。
匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了。
KMP的题可能不会非常直接的出,可是KMP的思想常常渗透在非常多题目里面,近期须要多练习一下。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1000005;
int _next[maxn],L;
char s[maxn];
void kmp_init(){
int i,j;
int m = strlen(s);
j = _next[0] = -1;
i = 0;
while(i < m){
while(j != -1 && s[i] != s[j]) j = _next[j];
_next[++i] = ++j;
}
}
bool kmp_solve(int len){
int i,j,m = L - len;
i = len;
j = 0;
while(i < m){
while(j != -1 && s[j] != s[i]) j = _next[j];
i++; j++;
if(j >= len){
return true;
}
}
return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s);
L = strlen(s);
kmp_init();
int i = L - 1;
int ans = 0;
while(_next[i] >= 0){
if(kmp_solve(_next[i] + 1)){
ans = _next[i] + 1;
break;
}
i = _next[i];
}
printf("%d\n",ans);
}
return 0;
}
最新文章
- 【翻译】MongoDB指南/引言
- vscode过滤pyc文件
- 转载-V.I.Arnold, beyond a mathematician
- SQLServer中的事务与锁
- System.BadImageFormatException : 未能加载文件或程序集“Medici.PaymentRecover, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。试图加载格式不正确的程序。
- OPENGL学习之路(0)--安装
- 在php中写接口时 对json格式的转换 简单的方法
- python_GUI
- CUBRID学习笔记 8 复制数据库
- Peer Code Reviews Made Easy with Eclipse Plug-In
- 关于对javascript 提升概念 的总结与思考。
- 初识Vim
- 在Eclipse中编译maven项目出的问题
- MySql 使用规范推荐
- JavaScript 对象分类
- C++面试笔记(2)
- WebStorm连接Github教程
- RocketMQ的使用
- 1、从C语言到C++
- Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)