您的位置:首页 > 其它

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

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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: