传送: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$开始不同的位置。

  1. $S$不可以翻转成$T$:就是指$S$串中不同的那一段不可以通过翻转得到$T$,方案数为0。
  2. $S$与$T$不同的“中间”那一段可以通过翻转得到对应$T$的那一段。这个时候需要向外扩展判断最长可以扩展到的位置。
  3. $S$与$T$完全相同,这个时候就需要通过manacher来求解整个串内回文子串的个数。

代码:

  1. 不分奇偶讨论的manacher
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+;
char S[maxn],T[maxn],s[maxn*];
int p[maxn*],len;
int init(){
s[]=s[]='#';
for (int i=;i<len;i++){
s[i*+]=S[i];
s[i*+]='#';
}
len=len*+;
s[len]=;
}
void manacher(){
int id,mx=;
for (int i=;i<len;i++){
if(i<mx) p[i]=min(p[(id<<)-i],p[id]+id-i);
else p[i]=;
while (s[i-p[i]]==s[i+p[i]]) p[i]++;
if (mx<i+p[i]){
id=i;mx=i+p[i];
}
}
}
int main(){
int t; scanf("%d",&t);
while (t--){
scanf("%s",S);
scanf("%s",T);
len=strlen(S);
int l=,r=len-; ll ans=;
while (S[l]==T[l] && l<len) l++;
while (S[r]==T[r] && r>=) r--;
if (l==r){printf("0\n"); continue;}
if (l<len){
ans=;
for (int i=l;i<=r;i++)
if (S[i]!=T[l+r-i]){
ans=; break;
}
if (ans){
ans=;
l--;r++;
while (l>= && r<len && S[l]==T[r] && S[r]==T[l]){
l--;r++;ans++;
}
}
printf("%d\n",ans);
}
else{
init();
manacher(); ans=;
for (int i=;i<len;i++) ans+=(p[i]/);
printf("%lld\n",ans-);
}
}
return ;
}

  2.分奇偶讨论的manacher

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+;
char S[maxn],T[maxn];
int odd[maxn],eve[maxn],len;
ll manacher(){
int l=-,r=-,x;
ll ans=;
for(int i=;i<len;i++)
{
if (i>r) x=;
else x=min(odd[l+r-i],r-i);
while (i-x>= && i+x<len && S[i-x]==S[i+x]) x++;
odd[i]=x;
ans+=x;
if (i+x->r) {r=i+x-;l=i-x+;}
}
l=r=-;
for(int i=;i<len;i++)
{
if(i>r) x=;
else x=min(eve[l+r-i+],r-i+);
while (i-x->= && i+x<len && S[i-x-]==S[i+x]) x++;
eve[i]=x;
ans+=x;
if (i+x>=r) {l=i-x;r=i+x-;}
}
return ans;
}
int main(){
int t; scanf("%d",&t);
while (t--){
scanf("%s",S);
scanf("%s",T);
len=strlen(S);
int l=,r=len-; ll ans=;
while (S[l]==T[l] && l<len) l++;
while (S[r]==T[r] && r>=) r--;
if (l==r){printf("0\n"); continue;}
if (l<len){
ans=;
for (int i=l;i<=r;i++)
if (S[i]!=T[l+r-i]){
ans=; break;
}
if (ans){
ans=;
l--;r++;
while (l>= && r<len && S[l]==T[r] && S[r]==T[l]){
l--;r++;ans++;
}
}
printf("%d\n",ans);
}
else{
ans=manacher();
printf("%lld\n",ans);
}
}
return ;
}

zoj4110 Strings in the Pocket(manacher)的更多相关文章

  1. O(n)回文子串(Manacher)算法

    O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串. ...

  2. 【学习笔记】字符串—马拉车(Manacher)

    [学习笔记]字符串-马拉车(Manacher) 一:[前言] 马拉车用于求解连续回文子串问题,效率极高. 其核心思想与 \(kmp\) 类似:继承. --引自 \(yyx\) 学姐 二:[算法原理] ...

  3. HDU 4513 吉哥系列故事——完美队形II(Manacher)

    Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...

  4. 牛客小白月赛13 小A的回文串(Manacher)

    链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...

  5. 2019浙江省赛 Strings in the Pocket【manacher】

    Strings in the Pocket 题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意 给你两个字符 ...

  6. 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)

    2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...

  7. ZOJ 4110 Strings in the Pocket (马拉车+回文串)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110 题目: BaoBao has just found two s ...

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

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 BaoBao has just found two strings ...

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

    给出两个字符串,询问有多少种反转方法可以使字符串1变成字符串2. 如果两个串相同,就用马拉车算法找回文串的数量~ 如果两个串不同,从前往后找第一个不同的位置l,从后往前找第二个不同的位置r,反转l和r ...

随机推荐

  1. Citrix XenApp工作原理

    Citrix XenApp™作为一种Windows®应用交付系统,可在数据中心集中管理应用,并将应用按需交付给身处各地.使用各种设备的用户.利用集成的应用虚拟化技术,XenApp克服了传统应用部署方法 ...

  2. Spring事务隔离级别和传播性

    事务的隔离级别也分为四种: read uncommited(读未提交). read commited(读提交). read repeatable(读重复). serializable(序列化), 这四 ...

  3. eclipse的Git忽略某些不需要提交的文件

    Eclipse切换到Navigator视图,找到.gitignore文件(如果是maven项目,一般找作为modules的项目的.gitignore文件),添加内容: .settings .proje ...

  4. C#对接JAVA系统遇到的AES加密坑

    起因对接合作伙伴的系统,需要对数据进行AES加密 默认的使用了已经写好的帮助类中加密算法,发现结果不对,各种尝试改变加密模式改变向量等等折腾快一下午.最后网上查了下AES在JAVA里面的实现完整代码如 ...

  5. php的pid文件指定用户

    比如pid文件指定www用户,首先得有这用户和用户组. 找到pathtophp-fpm.conf文件,修改里面得相关内容. 修改listen.owner=www listen.group=www us ...

  6. JS获取URL中文参数乱码的解决方法

    浏览器URL参数值中带有汉字字符,在接收时直接获取会出现乱码,下面是解决方法(传递前不需要encodeURI): function getUrlVars() { var vars = [], hash ...

  7. 目前php连接mysql的主要方式

    mysqli和PDO, 其中mysqli可以有面向过程,面向对象两种方式.而pdo只有面向对象的方式. <?php // $mysql_server = "localhost" ...

  8. Android开发日常-listview滚动方法梳理

    listview滚动方法梳理 1.setSelection(position); 滚动到指定条目 2.setSelectionFromTop(position,y): 距离指定条目向下偏移y(像素) ...

  9. Robot Framework浏览器驱动下载

    运行robot framework 有时打不开浏览器,可能用到的驱动不对,以下是各浏览器驱动下载,仅供参考!~ 各浏览器下载地址: Firefox浏览器驱动:geckodriver    https: ...

  10. Spring MVC 的国际化和本地化

    国际化: i18n 本地化: l10n java.util.Locale 类表示一个语言区域.一个 Locale 对象包含 3 个主要元件:language.country.variant java. ...