hdu3336(Count the string)KMP的应用

题意:给一个字符串,计算所有前缀在字符串中出现的次数和。

解法:KMP计算出Next数组后,每个位置的Next数组不断往前递归,每次相应前缀次数就加1.

代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
const double pi=acos(-1.0);
typedef long long LL;
const int Max=200100;
const int INF=10007;
int Next[Max];
int num[Max];
char s[Max];
int len=0;
void get_next()
{
    int i=0;
    int j=Next[0]=-1;
    while(i<len)
    {
        while(j!=-1&&s[i]!=s[j]) j=Next[j];
        Next[++i]=++j;
    }
}
int main()
{
  int t;cin>>t;
  while(t--)
  {
      //memset(num,0,sizeof num);
      scanf("%d",&len);
      scanf("%s",s);
      get_next();
      for(int i=0;i<=len;i++)
      {
          num[i]=1;
          int t=Next[i];
          while(t>0)
          {
              num[t-1]=(num[t-1]+1)%INF;
              t=Next[t];
          }
      }
      int ans=0;
      for(int i=0;i<len;i++)
      {
          ans=(ans+num[i])%INF;
      }
      printf("%d\n",ans);
  }
   return 0;
}

hdu3336(Count the string)KMP的应用

时间: 2024-10-05 01:08:00

hdu3336(Count the string)KMP的应用的相关文章

[HDU3336]Count the string(KMP+DP)

Solution 不稳定的传送门 对KMP的灵活应用 设dp[i]表示前[1,i]的答案 那么dp[i]=dp[p[i]]+1,p[i]为失配函数 Code #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int mo=10007; int n,T,dp[200010],p[200010],Ans; char s[200010]; int main(

HDUOJ------3336 Count the string(kmp)

D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can wr

HDU 3336 Count the string (KMP next数组运用——统计前缀出现次数)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as nu

hdu 3336 Count the string KMP+DP

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as n

Count the string kmp

问题描述众所周知,aekdycoin擅长字符串问题和数论问题.当给定一个字符串s时,我们可以写下该字符串的所有非空前缀.例如:S:“ABAB”前缀是:“A”.“AB”.“ABA”.“ABAB”对于每个前缀,我们可以计算它在s中匹配的次数,因此我们可以看到前缀“a”匹配两次,“ab”也匹配两次,“ab a”匹配一次,“ab ab”匹配一次.现在,您需要计算所有前缀的匹配时间之和.对于“abab”,它是2+2+1+1=6.答案可能非常大,因此输出答案mod 10007. 输入第一行是一个整数t,表示

HDU Count the string (KMP)

题面见http://acm.hdu.edu.cn/showproblem.php?pid=3336 给你一个字符串,让你找它的前缀在整个字符串出现的次数. 作为一个不会思考的笨比,直接用kmp去一个个计数,果不其然,t了 找了博客来看,大概就是kmp+dp,要用到kmp中的pret数组(有的人习惯叫next数组,知道就行) dp的方程形式很简单,但很难理解. 这是原博主的原话: 如果用dp[i]表示该字符串前i个字符中出现任意以第i个字符结尾的前缀的次数,它的递推式是 dp[i]=dp[pret

hud 3336 count the string (KMP)

这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了-- 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和 如: num(abab)=num(a)+num(ab)+num(aba)+num(abab)=2+2+1+1=6; 题解:next[i]记录的是 长度为i 不为自身的最大首尾重复子串长度  num[i]记录长度为next[i]的前缀所重复出现的次数 附上代码: const mo=10007; var sum,next:

Count the string(hdu3336)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7190    Accepted Submission(s): 3318 Problem Description It is well known that AekdyCoin is good at string problems as well as nu

KMP——hdu 3336 count the string

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10478    Accepted Submission(s): 4893 Problem Description It is well known that AekdyCoin is good at string problems as well as n