hdu 4763 Theme Section
来源:互联网 发布:淘宝网招聘官网 编辑:程序博客网 时间:2024/05/01 18:06
kmp好题啊!
题目大意: 找一个最长的子串符合是前缀也是后缀同时在中间也要出现,然后是不能重叠的。
思路: 通过next数组,找出前缀和后缀相等的长度然后去字符串中间寻找是否出现。 通过j=next[j] 减少比较量!
#include"cstdio"#include"cstring"#include"cmath"#include"cstdlib"#include"iostream"#include"algorithm"using namespace std;char x[1234567];int next[1234567];void getnext(char *t){ int i,j; next[0]=-1; i=0;j=-1; while(t[i]) { if(j==-1||t[i]==t[j]) next[++i]=++j; else j=next[j]; }}int kmp(char *s,char *t,int n,int m){ int i,j; i=j=0; while(i<n&&j<m) { if(j==-1||s[i]==t[j]) { i++;j++; } else j=next[j]; if(j==m) return 1; } return 0;}int main(){ int t; scanf("%d",&t); while(t--) { int j; int len; scanf("%s",x); getnext(x); len=strlen(x); int ans=0; j=next[len]; while(j) { if(j*3<=len&&kmp(x+j,x,len-2*j,j)) { ans=j; break; } j=next[j]; } printf("%d\n",ans); }}
0 0
- HDU 4763 Theme Section
- hdu 4763 Theme Section
- hdu 4763 Theme Section
- HDU-4763 Theme Section
- HDU 4763 Theme Section
- Hdu-4763 Theme Section
- hdu 4763 Theme Section
- hdu 4763 Theme Section
- HDU 4763Theme Section
- hdu 4763 Theme Section
- hdu 4763 Theme Section
- hdu 4763Theme Section
- KMP-hdu-4763-Theme Section
- hdu 4763 Theme Section (双哈希)
- hdu 4763 Theme Section(KMP)
- hdu 4763 Theme Section kmp
- HDU 4763 Theme Section(KMP)
- hdu 4763 Theme Section(KMP)
- Hypertable工具之dumplog
- 预处理#define
- Http协议知识整理
- MATLAB弹出对话框的使用
- 英特尔® 快速存储技术
- hdu 4763 Theme Section
- Hadoop IPC机制前奏曲之java自带RMI
- 一 100万并发连接服务器笔记之准备篇
- IOS 设置文件是否使用ARC
- nginx.conf的完整配置说明
- 第02章-VTK安装(2)
- 如何保养和清洁玉雕
- 每天进步一点点——Linux中的线程局部存储(一)
- jQuery取得/设置select的值