HDU 6194 string string string

来源:互联网 发布:布达佩斯大饭店 知乎 编辑:程序博客网 时间:2024/06/06 01:50

题目链接:HDU 6194

string string string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 671    Accepted Submission(s): 196


Problem Description
Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle Mao was so lazy that he left the problem to you. I hope you can give him a solution.
Given a string s, we define a substring that happens exactly k times as an important string, and you need to find out how many substrings which are important strings.
 

Input
The first line contains an integer T (T100) implying the number of test cases.
For each test case, there are two lines:
the first line contains an integer k (k1) which is described above;
the second line contain a string s (length(s)105).
It's guaranteed that length(s)2106.
 

Output
For each test case, print the number of the important substrings in a line.
 

Sample Input
22abcabc3abcabcabcabc
 

Sample Output
69
 

Source
2017 ACM/ICPC Asia Regional Shenyang Online 
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6205 6204 6203 6202 6201 
 

题意:给出一个字符串,问其中恰好出现k次的不同子串有多少个。

题目分析:询问不同子串个数那基本就是后缀数组没跑了,这里将所有的子串按排名列出来,可以看出,排名连续的子串的公共前缀一定出现了至少k次。之后我们的工作就是判断这里有多找个前缀出现次数比k次多,然后分别排名往前和往后看一位,如果还有公共前缀就把这些前缀去掉,height数组保存的就是牌名相邻的2个串公共前缀,我们是滑动大小为k的窗口并维护最小值,可以单调栈可以RMQ,不过后缀数组模板自带RMQ,调整下RMQ查询即可。

细节问题:

我的模板height[i]保存的是排名i与排名i-1的公共前缀,所以要注意查询范围。

rank数组从1开始,height数组也是从1开始。

RMQ模板传的2个参数是左右边界在实际字符串的位置,所以对于排名i需要传sa[i]。

对于K=1的情况不特判会RE(RE-1),实际上k=1相当于窗口大小为1,那么公共前缀就是自己的长度,其他相同,不需要添油加醋(WA-2)TAT。

PS:对于第9题,我们队一早就发现了这个bug,然后有一段时间忙着写题没管,然后等卡题了于是我们闲着无聊就二进制从1开始挨个试着交,然后交了200多次竟然过了。。。然后吃瓜群众看到我们原来还有这种操作,就开始也暴力提交,,,于是boom!!!真是打得最欢乐的一场网络赛了,整个比赛节奏都被我们带歪了。。

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int K;const int maxn=200010;int t1[maxn],t2[maxn],c[maxn];bool cmp(int *r,int a,int b,int l){    return r[a]==r[b]&&r[a+l]==r[b+l];}void da(int str[],int sa[],int ranks[],int height[],int n,int m){    n++;    int i,j,p,*x=t1,*y=t2;    for(i=0;i<m;i++) c[i]=0;    for(i=0;i<n;i++) c[x[i]=str[i]]++;    for(i=1;i<m;i++) c[i]+=c[i-1];    for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;    for(j=1;j<=n;j<<=1)    {        p=0;        for(i=n-j;i<n;i++) y[p++]=i;        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;        for(i=0;i<m;i++) c[i]=0;        for(i=0;i<n;i++) c[x[y[i]]]++;        for(i=1;i<m;i++) c[i]+=c[i-1];        for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;x[sa[0]]=0;        for(i=1;i<n;i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;        if(p>=n) break;        m=p;    }    int k=0;    n--;    for(i=0;i<=n;i++)ranks[sa[i]]=i;    for(i=0;i<n;i++)    {        if(k)k--;        j=sa[ranks[i]-1];        while(str[i+k]==str[j+k])k++;        height[ranks[i]]=k;    }}int ranks[maxn],height[maxn];char str[maxn];int r[maxn],sa[maxn];int pre[maxn],vis[maxn];int RMQ[maxn],mm[maxn];int dp[maxn][60];void Rmq_Init(int n){    int m=floor(log(n+0.0)/log(2.0));    for(int i=1;i<=n;i++) dp[i][0]=height[i];    for(int i=1;i<=m;i++)    {        for(int j=n;j;j--){            dp[j][i]=dp[j][i-1];            if(j+(1<<(i-1))<=n)                dp[j][i]=min(dp[j][i],dp[j+(1<<(i-1))][i-1]);        }    }}int Rmq_Query(int l,int r){    int a=ranks[l],b=ranks[r];    if(a>b) swap(a,b);    a++;    int m=floor(log(b-a+1.0)/log(2.0));    return min(dp[a][m],dp[b-(1<<m)+1][m]);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&K);        scanf("%s",str);        int len=strlen(str);        for(int i=0;i<len;i++) r[i]=str[i];        r[len]=0;        da(r,sa,ranks,height,len,128);        Rmq_Init(len);        long long int ans=0;        for(int i=1;i+K-1<=len;i++)        {            int buf2=0,buf3=0,buf1=0;            int a1=sa[i],a2=sa[i+K-1];            if(K!=1) buf1=Rmq_Query(a1, a2);            else buf1=len-sa[i];            if(i!=0) buf2=height[i];            if(i+K-1<len) buf3=height[i+K];                if(buf2>buf1||buf3>buf1) continue;                else                {                    ans+=buf1-max(buf3,buf2);                }        }        printf("%lld\n",ans);    }}


原创粉丝点击