[kuangbin带你飞]专题十六 KMP & 扩展KMP & ManacherK - Count the string HDU - 3336(前缀数量问题)

K - Count the string HDU - 3336

题目链接:https://vjudge.net/contest/70325#problem/K

题目:

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.

InputThe 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.

OutputFor 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题意:给你一个字符串,求出所有前缀的个数和,可以重叠思路:首先前缀个数就是字符串长度len,然后利用kmp求next数组,如果next[n]为4,对应前后缀最长相等的部分为abcd,那么a,ab,abc,abcd,都要加一次就是加next【n】,所以这题就是相当于求next数组,只要满足next[i]+1!=next[i+1]就行,就可以找到所有前缀的个数和了

//
// Created by HJYL on 2019/8/16.
//
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
char str[maxn];
int nextt[maxn];
void getnext()
{
    int i=0,j=-1;
    nextt[0]=-1;
    int len=strlen(str);
    while(i<len)
    {
        if(j==-1||str[i]==str[j])
        {
            i++,j++;
           //if(str[i]!=str[j])
                nextt[i]=j;
           // else
               // nextt[i]=nextt[j];
        } else
            j=nextt[j];
    }
}
int main()
{
    //freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin);
    int T;
    scanf("%d",&T);
    int n;
    while(T--)
    {
        scanf("%d",&n);
        getchar();
        scanf("%s",str);
        int len=strlen(str);
        //cout<<"len="<<len<<endl;
        getnext();
        int res=len+nextt[len];
        for(int i=0;i<len;i++)
        {
            if(nextt[i]+1!=nextt[i+1]&&nextt[i]>0)
                res+=nextt[i];
        }
        printf("%d\n",res%10007);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Vampire6/p/11366768.html

时间: 2024-10-12 16:05:09

[kuangbin带你飞]专题十六 KMP & 扩展KMP & ManacherK - Count the string HDU - 3336(前缀数量问题)的相关文章

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher B - Oulipo HDU - 1686(kmp)

B - Oulipo HDU - 1686 题目链接:https://vjudge.net/contest/70325#problem/B 题目: The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pa

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)

H - Seek the Name, Seek the Fame POJ - 2752 题目链接:https://vjudge.net/contest/70325#problem/H 题目: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. Th

[kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus HDU - 1024

A - Max Sum Plus Plus HDU - 1024 题目链接:https://vjudge.net/contest/68966#problem/A 题目: 现在我觉得你在Ignatius.L的“Max Sum”问题上得到了一个AC.要成为一个勇敢的ACMer,我们总是挑战自己更难的问题.现在你面临着一个更加困难的问题. 给定连续的数字序列S 1,S 2,S 3,S 4 ... S x,... S n(1≤x≤n≤1,000,000,-32768≤Sx≤32767).我们定义函数su

[kuangbin带你飞]专题十 匹配问题 一般图匹配

过去做的都是二分图匹配 即 同一个集合里的点 互相不联通 但是如果延伸到一般图上去 求一个一般图的最大匹配 就要用带花树来解决 带花树模板 用来处理一个无向图上的最大匹配 看了一会还是不懂  抄了一遍kuangbin的模板熟悉了一下 还有一个一般图最大权匹配 保存下来了VFK菊苣的模板题代码当作板子 http://uoj.ac/submission/16359 但愿以后的比赛永远也遇不到 .. 遇到了也能抄对 .. 抄错了也能过 .. R ural1099 kuangbin模板 #include

[kuangbin带你飞]专题十 匹配问题 二分图多重匹配

二分图的多重匹配问题不同于普通的最大匹配中的"每个点只能有最多一条边" 而是"每个点连接的边数不超过自己的限定数量" 最大匹配所解决的问题一般是"每个人都有一群想加入的团体 并且一个团体只能收一个人 问有多少人可以加入一个自己喜欢的团体" 而多重匹配是 "每个人都有一群想加入的团体 每个团体可以收给定的人数 问有多少人可以加入一个自己喜欢的团体" 解决这个问题 目前看貌似有三个办法 1 拆点 一个团体可以招x个人 就把它拆成x

【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus

A - Max Sum Plus Plus 1 https://vjudge.net/contest/68966#problem/A 2 3 http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127085.html 4 5 /* 6 状态dp[i][j]有前j个数,组成i组的和的最大值.决策: 7 第j个数,是在第包含在第i组里面,还是自己独立成组. 8 方程 dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-

【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D - Doing Homework

https://vjudge.net/contest/68966#problem/D http://blog.csdn.net/u010489389/article/details/19218795 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath>

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 G - 免费馅饼

https://vjudge.net/contest/68966#problem/G 正解一: http://www.clanfei.com/2012/04/646.html 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7 #define IN