hdu3476-Cyclic Nacklace

点击打开链接




// File Name: hdu3746.cpp
// Author: bo_jwolf
// Created Time: 2013年05月04日 星期六 19:54:29

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>

using namespace std;
const int maxn = 100010 ;
char str[ maxn ] ;
int next[ maxn ] ;
void getnext( char *p )
{
	int j  , k ;
	j = 0 , k = -1 ;
	int len = strlen( p ) ;
	next[ 0 ] = -1 ;
	while( j < len )
	{
		if( k == -1 || p[ j ] == p[ k ] )
		{
			j++ ;
			k++ ;
			next[ j ] = k ;
		}
		else
			k = next[ k ] ;
	}
}


int main()
{
	int Case ;
	scanf( "%d" , &Case ) ;
	while( Case-- )
	{
		scanf( "%s" , str ) ;
		getnext( str );
		int len = strlen( str ) ;

		if( next[ len ] == 0 )
		{
			printf( "%d\n" , len ) ;
			continue ;
		}
		int t = len - next[ len ] ;
		if( len % t == 0 )
			printf( "0\n" );
		else
			printf( "%d\n" , t - len % t ) ;
	}
	return 0;
}


你可能感兴趣的:(C++,学习笔记,杭电)