HDU 5343 MZL's Circle Zhou 后缀自动机+DP

本文介绍了一道经典的字符串组合计数问题:通过选取两个字符串A和B的不同子串组合成新的字符串,求解能够构成的不同新字符串的数量。文章详细解释了解题思路,包括如何避免重复计数,并给出了使用后缀自动机进行高效求解的具体实现。

MZL's Circle Zhou

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description
 
MZL's Circle Zhou is good at solving some counting problems. One day, he comes up with a counting problem:
You are given two strings a,b which consist of only lowercase English letters. You can subtract a substring x (maybe empty) from string a and a substring y (also maybe empty) from string b, and then connect them as x+y with x at the front and y at the back. In this way, a series of new strings can be obtained.
The question is how many different new strings can be obtained in this way.
Two strings are different, if and only if they have different lengths or there exists an integer i such that the two strings have different characters at position i.
 

 

Input
The first line of the input is a single integer T (T5), indicating the number of testcases. 
For each test case, there are two lines, the first line is string a, and the second line is string b1<=|a|,|b|<=90000.
 

 

Output
For each test case, output one line, a single integer indicating the answer.
 

 

Sample Input
2
acbcc
cccabc
bbbabbababbababbaaaabbbbabbaaaabaabbabbabbbaaabaab
abbaabbabbaaaabbbaababbabbabababaaaaabbaabbaabbaab
 
 
Sample Output
135
557539
 
题意:
  给你 两个字符串 A,B,你可以从A串中选一个子串或空, 从B串种选一个子串或空,组成一个新串
  问你能组成多少不同的新串
题解:
  麻烦的是如何去重
  考虑一个string , 我们只对分割点在最后位置的方案进行计数,这样即可避免重复计算
  首先分别对A,B进行建立后缀自动机
  预处理出对于B, 以字符X开始的不同的子串个数
  利用A进行计数,当当前A后缀自动机状态 st  nex[st][X] = NULL 时,我们将B串所有以X开始的子串加入st状态后面,更新answer
 
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
inline long long read(){long long x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;

const long long INF = 1e14;
const int N = 5e5+7;
const long long mod = 1000000007;

struct SAM{
    int isPlus[N * 2],endpos[N * 2];int d[N * 2];
    int cnt[N * 2];int pos[N];
    int tot,slink[2*N],trans[2*N][28],minlen[2*N],maxlen[2*N],pre;
    int newstate(int _maxlen,int _minlen,int* _trans,int _slink){
        maxlen[++tot]=_maxlen;minlen[tot]=_minlen;
        slink[tot]=_slink;
        if(_trans)for(int i=0;i<26;i++)trans[tot][i]=_trans[i];
        return tot;
    }
    int add_char(char ch,int u){
        int c=ch-'a',v=u;
        int z=newstate(maxlen[u]+1,-1,NULL,0);
        isPlus[z] = 1;
        while(v&&!trans[v][c]){trans[v][c]=z;d[z]+=1;v=slink[v];}
        if(!v){ minlen[z]=1;slink[z]=1;return z;}
        int x=trans[v][c];
        if(maxlen[v]+1==maxlen[x]){slink[z]=x;minlen[z]=maxlen[x]+1;return z;}
        int y=newstate(maxlen[v]+1,-1,trans[x],slink[x]);
        slink[z]=slink[x]=y;minlen[x]=minlen[z]=maxlen[y]+1;
        while(v&&trans[v][c]==x){trans[v][c]=y;d[x]--,d[y]++;v=slink[v];}
        minlen[y]=maxlen[slink[y]]+1;
        return z;
    }
    void init_sam() {
        for(int i = 1; i <= tot; ++i)
            for(int j = 0; j < 26; ++j) trans[i][j] = 0;
        pre = tot = 1;
    }
}A,B;

unsigned long long f[N],dp[N];
char a[N],b[N];
int main() {
    int T,cas = 1;
    scanf("%d",&T);
    while(T--) {
        scanf("%s%s",a,b);
        int n = strlen(a);
        int m = strlen(b);
        A.init_sam();
        B.init_sam();
        for(int i = 0; i < n; ++i) A.pre = A.add_char(a[i],A.pre);
        for(int i = 0; i < m; ++i) B.pre = B.add_char(b[i],B.pre);

        for(int i = 0; i <= m; ++i) B.cnt[i] = 0;
        for(int i = 1; i <= B.tot; ++i) B.cnt[B.maxlen[i]] += 1;
        for(int i = 1; i <= m; ++i) B.cnt[i] += B.cnt[i-1];
        for(int i = B.tot; i >= 1; --i) B.pos[B.cnt[B.maxlen[i]]--] = i;

        for(int i = 0; i <= n; ++i) A.cnt[i] = 0;
        for(int i = 1; i <= A.tot; ++i) A.cnt[A.maxlen[i]] += 1;
        for(int i = 1; i <= n; ++i) A.cnt[i] += A.cnt[i-1];
        for(int i = A.tot; i >= 1; --i) A.pos[A.cnt[A.maxlen[i]]--] = i;
        memset(f,0,sizeof(f));
        memset(dp,0,sizeof(dp));
        for(int i = B.tot; i >= 1; --i) {
            int v = B.pos[i];
            f[v] = 1;
            for(int j = 0; j < 26; ++j) {
                if(B.trans[v][j])f[v] += f[B.trans[v][j]];
            }
        }
        for(int i = 0; i < 26; ++i)
            dp[i] = f[B.trans[1][i]];
        unsigned long long ans = 0;
        for(int i = A.tot; i >= 1; --i) {
            int v = A.pos[i];
            for(int j = 0; j < 26; ++j) {
                if(A.trans[v][j] == 0) {
                    ans += dp[j]*(A.maxlen[v] - A.minlen[v] + 1);
                }
            }
        }
        for(int i = A.tot; i >= 2; --i) ans += (A.maxlen[i] - A.minlen[i] + 1);
        cout<<ans+1<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zxhl/p/7620482.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值