HDU 3336 Count the string(KMP算法next数组的应用)

解题思路:

求前缀数组出现的次数之和,next[i] > 0 表示长度为next[i]的前缀又出现了一次。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
#define FOR(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
const int maxn = 200000 + 10;
const int MOD = 10007;
char s[maxn];
int next[maxn];
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int m;
        scanf("%d", &m);
        scanf("%s", s);
        next[0] = 0; next[1] = 0;
        for(int i=1;i<m;i++)
        {
            int j = next[i];
            while(j && s[i] != s[j]) j = next[j];
            next[i+1] = (s[i] == s[j]) ? j + 1 : 0;
        }
        int ans = m;
        for(int i=1;i<=m;i++)
        {
            if(next[i] > 0) ans = (ans + 1) % MOD;
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-12-25 18:41:33

HDU 3336 Count the string(KMP算法next数组的应用)的相关文章

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

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

hdu 3336 Count the string

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

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

HDU - 3336 Count the string (扩展kmp)

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&qu

Hdu 3336 Count the String(DP+KMP)(好题)

题意:对于长度为len的字符串,我们知道它包含有len个前缀,现在要你统计出这个字符串里面,包含这些前缀的总个数. 思路:这题可以运用KMP的next数组来解,不过也太难想了吧orz,为了用next解这题想那么多也不算是很好的方法orz. 如何根据next数组的性质来解这道题,next数组的值是当前子串的后缀与前缀匹配的个数,所以根据这个性质把题待求的对象改一下:求每种字母作为结尾的串在原串中出现的次数.从最后一个字母分析,首先字符串本身就是以last字母作结,next[last]=x,所以又找

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

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

HDU 3336 Count the string (基础KMP)

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&qu