hust 1010 kmp next数组的应用
2015-08-26 15:12
423 查看
F - The Minimum Length
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status Practice HUST
1010
Description
There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the
shortest possible string A. For example, A="abcdefg". I got abcd efgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
Input
Multiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.
Output
For each line, output an integer, as described above.
Sample Input
Sample Output
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status Practice HUST
1010
Description
There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the
shortest possible string A. For example, A="abcdefg". I got abcd efgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
Input
Multiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.
Output
For each line, output an integer, as described above.
Sample Input
bcabcab efgabcdefgabcde
Sample Output
3 7#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <ctime> using namespace std; #pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan #define clr(s,x) memset(s,x,sizeof(s)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lowbit(x) (x&(-x)) #define PB push_back #define For(i,a,b) for(int i=a;i<b;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) typedef long long LL; typedef unsigned int uint; typedef unsigned long long ULL; typedef vector<int> vint; typedef vector<string> vstring; void RI (int& x){ x = 0; char c = getchar (); while (c == ' '||c == '\n') c = getchar (); bool flag = 1; if (c == '-'){ flag = 0; c = getchar (); } while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar (); } if (!flag) x = -x; } void RII (int& x, int& y){RI (x), RI (y);} void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);} const double PI = acos(-1.0); const int maxn = 1e6 + 100; const int maxm = 1e6 + 100; const LL mod = 1e9 + 7; const double eps = 1e-9; const int INF = 0x7fffffff; /************************************END DEFINE*********************************************/ char s[maxn],t[maxm]; int nxt[maxm]; int n; void get_next(){ clr(nxt,0); int len = strlen(t); nxt[0] = 0; nxt[1] = 0; For(i,1,len){ int j = nxt[i]; while(j && t[i] != t[j])j = nxt[j]; nxt[i+1] = t[i] == t[j] ? j+1 : 0; } } int kmp(){ get_next(); int lens = strlen(s), lent = strlen(t); int j = 0,cnt = 0; For(i,0,lens){ while(j && t[j] != s[i])j = nxt[j]; if(t[j] == s[i]) j++; if(j == lent){ cnt ++; j = nxt[j]; } } return cnt; } int main() { while(~scanf("%s",t)){ get_next(); printf("%d\n",strlen(t)-nxt[strlen(t)]); } return 0; }
相关文章推荐
- 栈之链栈
- 最长公共子序列
- html5之video
- iOS删除项目中未使用的图片资源
- 自定义Dialog以及Dialog之间跳转
- 【POJ1751】【坑,double 不能用memset】【sqrt i j写错了】
- HDU2795 Billboard 线段树
- POJ 2115 C Looooops(单变元模线性方程)
- malloc函数、free()
- UVA - 1658 Admiral (最小费用最大流)
- 10048 - Audiophobia (Floyd)
- 架构模式及要素
- SharePoint PowerShell命令系列 (5) New-SPSite
- [TYVJ1035] 棋盘覆盖|匈牙利算法|二分图匹配
- c语言, objective code(new 1)
- 安装php时,make test报错
- 设置TableViewCell 分割线从最左侧绘制
- Apache HttpClient 4.3开发指南 实现URL重定向
- Json解析方式
- 关于 hashCode() 你需要了解的 3 件事