ZOJ 4110 Strings in the Pocket (马拉车+回文串)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110
题目:
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串和T串 要求对S串的一个子串做一次翻转操作可以得到T串的方案数
思路:
对子串分两种情况
第一种是S串和T串完全相同 可以的方案数就是S串中的所有的回文子串 因为S串长度为2e6 必须要用马拉车线性去处理出所有的回文子串
第二种是S串和T串有不同的部分 找出两个不同的字符最远的位置(l,r) 先判断S串的这个区间是否能通过翻转变成T串的区间 如果不可以直接输出0 如果可以 则向两侧同时延展寻找是否可以翻转
代码:
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
const int maxn=2e6+;
int t,len[maxn*];
char S[maxn],T[maxn],s[maxn*]; int init(char *str){
int n=strlen(str);
for(int i=,j=;i<=*n;j++,i+=){
s[i]='#';
s[i+]=str[j];
}
s[]='$';
s[*n+]='#';
s[*n+]='@';
s[*n+]='\n';
return *n+;
} void manacher(int n){
int mx=,p=;
for(int i=;i<=n;i++){
if(mx>i) len[i]=min(mx-i,len[*p-i]);
else len[i]=;
while(s[i-len[i]]==s[i+len[i]]) len[i]++;
if(len[i]+i>mx) mx=len[i]+i,p=i;
}
} int main(){
scanf("%d",&t);
while(t--){
scanf("%s",S);
scanf("%s",T);
int Len=strlen(S),tot1=-,tot2=Len;
for(int i=;i<Len;i++){
if(S[i]!=T[i]){
tot1=i;break;
}
}
for(int i=Len-;i>=;i--){
if(S[i]!=T[i]){
tot2=i;break;
}
}
if(tot1==-){
int n=init(S);
for(int i=;i<=n;i++) len[i]=;
manacher(n);
ll ans=;
for(int i=;i<=n;i++) ans+=len[i]/;
printf("%lld\n",ans);
continue;
}
else{
int tmp=;
for(int i=tot1;i<=tot2;i++){
if(S[i]!=T[tot2-(i-tot1)]){
tmp=;
break;
}
}
if(tmp==){
printf("0\n");
continue;
}
else{
ll ans=; tot1--; tot2++;
while (tot1>= && tot2<Len && S[tot1]==T[tot2] && S[tot2]==T[tot1]){
tot1--; tot2++; ans++;
}
printf("%lld\n",ans);
}
}
}
return ;
}
ZOJ 4110 Strings in the Pocket (马拉车+回文串)的更多相关文章
- 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串
		
Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...
 - 小白月赛13 B小A的回文串 (马拉车算法求最长回文子串)
		
链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
 - Manacher's Algorithm 马拉车算法(最长回文串)
		
这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...
 - 马拉车,O(n)求回文串
		
马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...
 - Manacher's Algorithm 马拉车算法(求最长回文串)
		
作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...
 - [LeetCode] Longest Palindromic Substring 最长回文串
		
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
 - 回文串--- Girls' research
		
HDU 3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...
 - Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
		
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
 - HDOJ/HDU 2163 Palindromes(判断回文串~)
		
Problem Description Write a program to determine whether a word is a palindrome. A palindrome is a s ...
 
随机推荐
- 用Case类生成模板代码
			
将类定义为case类会生成许多模板代码,好处在于: ①会生成一个apply方法,这样就可以不用new关键字创建新的实例. ②由于case类的构造函数参数默认是val,那么构造函数参数会自动生成访问方法 ...
 - CRM销售人员针对的客户的权重分配的思想
			
巧妙使用权重http://www.ziawang.com/article/52/ Django 项目CRM总结 - Pythia丶陌乐 - 博客园https://www.cnblogs.com/sup ...
 - React生命周期详解
			
React生命周期图解: 一.旧版图解: 二.新版图解: 从图中,我们可以清楚知道React的生命周期分为三个部分: 实例化.存在期和销毁时. 旧版生命周期如果要开启async rendering, ...
 - think
			
https://github.com/crossoverJie/Java-Interview Java-Interview https://github.com/aalansehaiyang/tech ...
 - 数组中的reduce 函数理解
			
第一次见到reduce 是在js 的高级程序设计中,它的意思是把一个数组减少为一个数,举的例子是数组中元素的求和.它接受一个函数作为参数,函数又有两个参数,一个是prev, 前一个值,一个是next, ...
 - Plugin Kotlin was not installed: Cannot download
			
到 https://plugins.jetbrains.com/plugin/6954-kotlin下载对应版本(右键复制链接地址,用迅雷下载),放到androidstudio安装目录下的plugin ...
 - php 两个数组,若键相同,则值合并
			
<?php $arr1 = array('9' => '4.08', '10' => '0.10', '11' => '4.08', '12' => '0.01'); $ ...
 - ubuntu18.04搭建 kubernetes(k8s)集群
			
下面使用kubeadm来创建k8s cluster1. 所有主机节点上都需要安装docker # sudo apt-get update # sudo apt-get install \ apt-tr ...
 - Neo4j安装
			
一.Windows版本 1)下载java8,并配置环境变量 java下载请点击,提取码:f6ci 2)Neo4j下载 选windows版本 新建系统环境变量: 并配置Path环境变量,添加bin所在目 ...
 - [图解Java]ReentrantLock重入锁
			
图解ReentrantLock 0. demo 我先给出一个demo, 这样大家就可以根据我给的这段代码, 边调试边看源码了. 还是那句话: 注意"My" , 我把Reentran ...