HDU 3336 Count the string

原创 2018年04月15日 17:36:22

题目链接:http://hdu.hustoj.com/showproblem.php?pid=3336

Problem 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 write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
 

Input
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
 

Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 

Sample Input
14abab
 

Sample Output
6


题目描述:求字符串的出现的所有前缀次数和

思路:DP+KMP。当某一个字母新出现时,其对应Next数组回溯到-1的位置回溯了多少次就是对ans贡献+多少次(Next数组特性得到的),又next数组每次回溯的次数一样,所以不用每次都找,用DP数组记录一下就ok。


代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 10007;
const int N = 1e6+5;

string str,mo;
int Next[200005];
int dp[200005];

void Get_next()
{
    int i,j;
    j = Next[0] = -1;
    i = 0;
    int len = str.length();
    while(i<len)
    {
        while(j!=-1&&str[i]!=str[j])
            j = Next[j];
        Next[++i] = ++j;
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        cin>>str;
        mo="";
        Get_next();
        MEM(dp,0);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            dp[i] = (dp[Next[i]]+1)%mod;
            ans = (ans+dp[i])%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baodream/article/details/79951135

HDU - 3336 Count the string

Problem Description It is well known that AekdyCoin is good at string problems as well as number th...
  • u011345136
  • u011345136
  • 2014-09-15 17:06:01
  • 958

HDU 3336-Count the string

HDU 3336 KMP+DP #include #include using namespace std; #define MAX 200005 #define mod 1000...
  • yzllz001
  • yzllz001
  • 2016-06-30 12:13:10
  • 218

HDU 3336 Count the string 所有前缀在串中的出现总次数

题目链接题意给定一个串 SS,求其所有前缀在其中的出现次数的总和。思路考虑 failfail 数组,fail[i]=jfail[i] = j 的含义是 S[0..j−1]==S[i−j..i−1]S[...
  • kkkkahlua
  • kkkkahlua
  • 2017-09-19 18:58:23
  • 247

HDU 3336 Count the string (next数组活用)

HDU 3336 Count the string (next数组),网上很多人这题的做法都是错的,只能怪数据太水...
  • Tc_To_Top
  • Tc_To_Top
  • 2015-02-11 10:48:33
  • 1391

hdu3336 Count the string

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 6            a  b  a  b  a  b  ‘\0’ 下...
  • lezong2011
  • lezong2011
  • 2013-09-03 21:55:34
  • 568

hdu 3336 Count the string(★)

题意: 给一个字符串,输出包括从1到len长 字符串前缀的总个数 题解: 设dp【i】:以string[i]结尾的子串总共含前缀的数量 所以dp[j]=dp[i]+1,即以i结尾的子串中含前缀的数...
  • Ezereal
  • Ezereal
  • 2015-10-02 16:28:27
  • 240

HDU 3336 Count the string

Count the string                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768...
  • qq_39259536
  • qq_39259536
  • 2017-09-21 18:18:29
  • 44

hdu 3336 count the string

解释 题意:求给定字符串含前缀的数量 abab 前缀为 a ab aba abab abab中共有六个子串是前缀a a ab ab aba abab 所以答案为6 利用kmp中的匹...
  • KIDGIN7439
  • KIDGIN7439
  • 2014-06-06 16:44:52
  • 288

HDU 3336 Count The String

K - Count the string                                 HDU - 3336题意:例如abab这样的字符串的子字符串看为有a,ab,aba,abab四...
  • qq_41444888
  • qq_41444888
  • 2018-01-19 18:37:20
  • 122
收藏助手
不良信息举报
您举报文章:HDU 3336 Count the string
举报原因:
原因补充:

(最多只允许输入30个字)