hdu 3336 Count the string

本文探讨了如何高效计算字符串前缀匹配次数,并通过实例展示了使用记忆化搜索和KMP算法进行优化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
  
  
1 4 abab
 

Sample Output
  
  
6
 
 
题意:有一个字符串,长度不大于200000,求字符串前缀的的数量, 在取mod10007
 
思路:如果是要暴力的话是 n方 200000 * 200000 = 4 * 10e10 约 400s
所以只能用 nlog2n 的算法 200000 * log2(200000) = 3.5 * 10e6 约 35ms
记忆化搜索
每次都从上一前缀的末字符的后面字符开始找。
 
首先是 长度为1的前缀是 s[0]:a。 在字符串s中找3个相同的。 分别为 0, 2, 4。
然后是 长度为2的前缀是 ab, 要能找到 与ab相同的必须是 先与a相同的, 所以a就不用找了,从 1,3,5位置找与b相同的。
找到了 1,3, 5。
同理 :
接着 aba 从 2, 4位置找与a相同的。
找到了 2, 4;
abab 从 3, 5位置找与b相同的。
找到了 3, 5;
ababa 从 4 位置中找与a相同的。
找到了 4;
ababab 从 5 位置找与b相同的。
知道了 5
sum = 3 + 3 + 2 + 2 + 1 + 1 = 12;
 
但是我用了队列,速度有点慢!
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int V = 200000 + 50;
const int MOD = 10007;

//记忆化搜索

int T, n, que[V], fr, re;
char ch[V];
int main() {
    int i, j;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        scanf("%s", &ch);
        fr = re = 0;
        for(i = 0; i < n; ++i)
            que[re++] = i;
        int ans = 0;
        for(i = 0; i < n; ++i) {
            int m = re;
            for(j = fr; j != m; j = (j + 1) % V) {
                if(ch[que[j]] == ch[i]) {
                    ans++;
                    que[re] = que[j] + 1;
                    re = (re + 1) % V;
                }
            }
            fr = m;
            ans %= MOD;
        }
        printf("%d\n", ans);
    }
}

KMP + DP:

首先先用KMP对模式串自我匹配。
next[i] 表示以第i个字符结尾的子串 与 从第一个字符开始的子串最大能匹配到的位置。
如:


KMP完后, 就是DP了。
dp[i] 表示的是以第i个字符结尾的个数。 这里要右移一位,引入dp[0], 表示 匹配失败。

转移: dp[i] = dp[next[i - 1] + 1] + 1;
根据next[i] 的定义, 也就是 相当于 + 一次匹配成功的串, 再加一个ch[0~i]。
如:

代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int V = 200000 + 50;
const int MOD = 10007;
char ch[V];
int next[V], T, n, dp[V];
void KMP(char *p,int *next) {
    int j, k;
    next[0] = k = -1;
    for(j = 1; j < n; ++j) {
        while(k >= 0 && p[j] != p[k + 1])
            k = next[k];
        if(p[k + 1] == p[j])
            k++;
        next[j] = k;

    }
}
int main() {
    int i, j;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        scanf("%s", &ch);
        KMP(ch, next);
        dp[0] = 0;
        int ans = 0;
        for(i = 1; i <= n; ++i) {
            dp[i] = dp[next[i - 1] + 1] + 1;
            ans = (ans + dp[i]) % MOD;
        }
        printf("%d\n", ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值