ZOJ4110 Strings in the Pocket(2019浙江省赛)

给出两个字符串,询问有多少种反转方法可以使字符串1变成字符串2。

如果两个串相同,就用马拉车算法找回文串的数量~

如果两个串不同,从前往后找第一个不同的位置l,从后往前找第二个不同的位置r,反转l和r,判断是否成功~

如果不成功,记为0

如果成功,以l和r为起点判断是否能反转,记录次数

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e6+100;
typedef long long ll;
char s1[maxn];
char s2[maxn];
char s[maxn*2];
int len[maxn*2];
int init(char *str){
    int n=strlen(str);
    for(int i=1,j=0;i<=2*n;j++,i+=2){
        s[i]=‘#‘;
        s[i+1]=str[j];
    }
    s[0]=‘$‘;
    s[2*n+1]=‘#‘;
    s[2*n+2]=‘@‘;
    s[2*n+3]=‘\n‘;
    return 2*n+1;
}

void manacher(int n){
    int mx=0,p=0;
    for(int i=1;i<=n;i++){
        if(mx>i) len[i]=min(mx-i,len[2*p-i]);
        else len[i]=1;
        while(s[i-len[i]]==s[i+len[i]]) len[i]++;
        if(len[i]+i>mx) mx=len[i]+i,p=i;
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",s1);
        scanf("%s",s2);
        int Len=strlen(s1),l=-1,r=Len;
        for(int i=0;i<Len;i++){
            if(s1[i]!=s2[i]){
                l=i;break;
            }
        }
        for(int i=Len-1;i>=0;i--){
            if(s1[i]!=s2[i]){
                r=i;break;
            }
        }
        if(l==-1){
            ll ans=0;
            int n=init(s1);
            manacher(n);
            for (int i=1;i<=n;i++) ans+=len[i]/2;
            printf("%lld\n",ans);
            continue;
        }
        else{
            int tmp=0;
            for(int i=l;i<=r;i++){
                if(s1[i]!=s2[l+r-i]){
                    tmp=1;
                    break;
                }
            }
            if(tmp==1){
                printf("0\n");
                continue;
            }
            else{
                ll ans=1;l--;r++;
                while (l>=0&&r<Len&&s1[l]==s2[r]&&s1[r]==s2[l]){
                        l--;r++;ans++;
                }
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/zhanglichen/p/12307615.html

时间: 2024-07-31 06:41:05

ZOJ4110 Strings in the Pocket(2019浙江省赛)的相关文章

2019浙江省赛K zoj4110 Strings in the Pocket(manachar)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意 给你两个串,可以翻转a串的一个区间,问有多少对l,r使得翻转后的a串等于b串 题解 沙比提,比赛时想了想两个串相等就用马拉车求回文子串个数,觉得两个串不相等情况很复杂就没想下去了,其实两个串不相等的情况更好处理 两个串不一样的话,一定需要翻转第一个和最后一个不相等的位置(关键),判一下中间是不是回文串,然后维护一下两边即可 特判只有一个字符不相等的时候 代码 #i

zoj4110 Strings in the Pocket(manacher)

传送:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意:给定两个串$S$和$T$,可以翻转$S$串中的任意一个子段,得到$T$.问,可以翻转的方案书有多少? 数据范围:多组数据.$1\leq|S|\leq2\times10^5$,$\sum|S|\leq2\times10^7$. 分析:很明显需要分类讨论$S$与$T$比较的各种情况. 首先需要判断$S$串从左和从右找到与$T$开始不同的位置. $S$不可以翻转成

2017浙江省赛 D - Let&#39;s Chat ZOJ - 3961

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题目: ACM (ACMers' Chatting Messenger) is a famous instant messaging software developed by Marjar Technology Company. To attract more users, Edward, the boss of Marjar Company, has re

第14届浙江省赛--Let&#39;s Chat

Let's Chat Time Limit: 1 Second      Memory Limit: 65536 KB ACM (ACMers' Chatting Messenger) is a famous instant messaging software developed by Marjar Technology Company. To attract more users, Edward, the boss of Marjar Company, has recently added

2019模拟赛09场解题报告

目录 2019模拟赛09场解题报告 目录la~~ 题一:瞬间移动 题二:食物订购 题三:马蹄印 题四:景观美化 2019模拟赛09场解题报告 标签(空格分隔): 解题报告 Forever_chen 2019.8.20 目录la~~ 题一:瞬间移动 [题面] 有一天,暮光闪闪突然对如何将一个整数序列a1,a2,...,an排序为一个不下降序列起了兴趣.身为一只年轻独角兽的她,只能进行一种叫做"单元转换"(unit shift)的操作.换句话说,她可以将序列的最后一个元素移动到它的起始位置

HDU6706 CCPC 2019网络赛 huntian oy 推式子+杜教筛

CCPC 2019 网络赛 HDU 6706 huntian oy 标签 奇奇怪怪的数论结论 杜教筛 前言 我的csdn和博客园是同步的,欢迎来访danzh-博客园~ 简明题意 给定n,a,b,求: \[\sum_{i=1}^n\sum_{j=1}^igcd(i^a-j^a,i^b-j^b)[gcd(i,j)=1]\%(10^9+7)\] 思路 首先有一个结论: \[gcd(i^a-j^a,i^b-j^b)=i^{gcd(a,b)}-j^{gcd(a,b)}\] 上面的结论对于i,j互质是成立的

2019年浙江省赛 I Fibonacci in the Pocket

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4108 题意:求第l个斐波那契数到第r个斐波那契数的和,判断这个和奇偶性,若为奇输出1,偶输出0 题解:很明显要利用前缀和,通过打表可以发现斐波那契前缀和的奇偶性为 奇 偶 偶 ,循环节长度为3,f(i)表示第i个前缀和,l到r的斐波那契数求和等于f(l)-f(r-1),所以只需要判断l和r-1对三取余的结果就可以判断f(l)和f(r-1)的奇偶性.注意到题目给的数据

2019省赛训练组队赛4.9周二 2017浙江省赛

A - Cooking Competition "Miss Kobayashi's Dragon Maid" is a Japanese manga series written and illustrated by Coolkyoushinja. An anime television series produced by Kyoto Animation aired in Japan between January and April 2017. In episode 8, two

Strings in the Pocket(马拉车+字符串判断)

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 BaoBao has just found two strings s 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