您的位置:首页 > 编程语言 > C语言/C++

hdu3336-Count the string

2013-04-30 20:51 218 查看

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 ;
}
else
k = 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  学习笔记 c++ 杭电 kmp