马拉车算法——求回文子串个数zoj4110

转载 https://www.cnblogs.com/zsben991126/p/10786167.html

Strings in the Pocket
Time Limit: 1 Second Memory Limit: 65536 KB
BaoBao has just found two strings and in his left pocket, where indicates the -th character in string , and indicates the -th character in string .

As BaoBao is bored, he decides to select a substring of and reverse it. Formally speaking, he can select two integers and such that and change the string to .

In how many ways can BaoBao change to using the above operation exactly once? Let be an operation which reverses the substring , and be an operation which reverses the substring . These two operations are considered different, if or .

Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains a string (), while the second line contains another string (). Both strings are composed of lower-cased English letters.

It’s guaranteed that the sum of of all test cases will not exceed .

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

Sample Input
2
abcbcdcbd
abcdcbcbd
abc
abc
Sample Output
3
3
Hint
For the first sample test case, BaoBao can do one of the following three operations: (2, 8), (3, 7) or (4, 6).

For the second sample test case, BaoBao can do one of the following three operations: (1, 1), (2, 2) or (3, 3).

翻转s的一个区间[l,r],求翻转后等于t的区间数。
1.l>r,s=t,求s的回文子串数即可,res+=p[i]/2;
2.l==r,没有。
3.l<r,s不等于t,找到第一个s和t不同的位置l,最后一个s和t不同的位置r,
如果可行,这就是第一个回文串,从l到r,i++,j–判断是否是回文串,计数,最后l–,r++,扩展两边是否可行增加方案数。

求回文子串的个数:只要把p[i]/2就行了:
如果s_new[i]是‘#’,算的是没有中心的偶回文串
反之是奇回文串
给定两个字符串s,t

/*
给定两个字符串s,t
结论:s,t不相同的第一个字符下标为l,最后一个字符下标为r
如果l==r,那么不存在解
如果l<r,那么翻转s的一个子串时,一定是将s[l]和s[r]互相翻转
反证:假设存在s[l]和s[r+k]的翻转方法,
因为s[r+k]=t[r+k]=t[l],且s[l]=t[r+k],可得s[l]=t[l],矛盾
l-k同理

所以本题只需要考虑三种情况:
1.两个串相等:求s的回文子串
2.l==r,无解
3.l<r,首先s[l,r]翻转后和t[l,r]相等,然后找以s[l,r]为中心的回文子串
*/

//求回文子串个数模板
#include<bits/stdc++.h>
using namespace std;
#define maxn 5000006
char s[maxn],t[maxn],s_new[maxn];
int p[maxn];

int init(){
    int len=strlen(s),j=2;
    s_new[0]='$',s_new[1]='#';
    for(int i=0;i<len;i++){
        s_new[j++]=s[i];
        s_new[j++]='#'; 
    }
    s_new[j]=0;
    return j;
}
long long manacher(){
    long long res=0;
    int len=init(),mx=0,id;
    for(int i=0;i<len;i++){
        if(mx>i)p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        while(s_new[i-p[i]]==s_new[i+p[i]])p[i]++;
        if(mx<i+p[i])mx=i+p[i],id=i;
        res+=(long long )p[i]/2;
    }
    return res;
}
int main(){
    int T;cin>>T;
    while(T--){
        scanf("%s%s",s,t);
        int len=strlen(s),l=0x3f3f3f3f,r=0;
        for(int i=0;i<len;i++)
            if(s[i]!=t[i]){l=i;break;}
        for(int i=len-1;i>=0;i--)
            if(s[i]!=t[i]){r=i;break;}
            
        if(l==r){puts("0");continue;} 
        if(l>r){cout<<manacher()<<endl;continue;}
        
        int flag=0;
        for(int i=l,j=r;i<=r;i++,j--)
            if(s[i]!=t[j])flag=1;
        if(flag){puts("0");continue;}
        else {
            int ans=1;
            while(l!=0 && r!=len-1){
                --l;++r;
                if(s[l]==s[r])ans++;
                else break;
            }
            cout<<ans<<endl;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值