hdu3336-Count the string

来源:互联网 发布:preview.exe软件下载 编辑:程序博客网 时间:2024/06/05 21:57

Count the string

使用get_next函数多了,渐渐对get_next函数的理解又深了一点点,

这其中dp[i]从1加到n

dp[i]=dp[next[i]]+1;


// File Name: hdu3336.cpp// Author: bo_jwolf// Created Time: 2013年04月30日 星期二 20:39:15#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 = 200005 ;int dp[ maxn ] ;const int MOD = 10007 ;char str[ maxn ] ;int next[ maxn ];int len ;void get_next(){int j = 0 , k = -1 ;next[ 0 ] = -1 ;len = strlen( str ) ;while( j < len ){if( k == -1 || str[ j ] == str[ k ] ){j++ ;k++ ;next[ j ] = k ; }elsek = next[ k ] ;}}int main(){int n ;int Case ;int ans ;scanf( "%d" ,&Case );while( Case-- ){scanf( "%d" , &n ) ;scanf( "%s" , str );get_next();dp[ 0 ] = 0 ;ans = 0 ;for(int i = 1 ; i <= n ; i++ ){dp[ i ] = dp[ next[ i ] ] + 1 ;dp[ i ] = dp[ i ] % MOD ;ans += dp[ i ];ans = ans % MOD ;}printf( "%d\n" , ans ) ;}return 0;}






原创粉丝点击