链接: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 (马拉车+回文串)的更多相关文章

  1. 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串

    Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...

  2. 小白月赛13 B小A的回文串 (马拉车算法求最长回文子串)

    链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  3. Manacher's Algorithm 马拉车算法(最长回文串)

    这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...

  4. 马拉车,O(n)求回文串

    马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...

  5. Manacher's Algorithm 马拉车算法(求最长回文串)

    作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...

  6. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. 回文串--- Girls' research

    HDU   3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...

  8. 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 ...

  9. HDOJ/HDU 2163 Palindromes(判断回文串~)

    Problem Description Write a program to determine whether a word is a palindrome. A palindrome is a s ...

随机推荐

  1. python icmp\dns\http监控网络各个节点状态,并记录日志

    配置文件如下:支持多节点: { "dns":[{"domainname":"www.baidu.com","dnsserver&q ...

  2. java基础-03基本语法

    关键词 常用关键字53个(含2个保留字): 1.保留关键字(2个) const --常量 常数:用于修改字段或局部变量的声明. goto--转到 指定跳转到标签,找到标签后,程序将处理从下一行开始的命 ...

  3. Flask--偏函数, 线程安全, 请求上下文

    一 . 偏函数 from functools import partial def func(a, b): return a + b new_func = partial(func, 3, 4) # ...

  4. PHP Yii2 composer环境安装

    PHP Yii2 composer环境安装 composer 安装 任意目录执行: php -r "copy('https://install.phpcomposer.com/install ...

  5. css:a:visited限制

    :active 对于:active伪类可以在div上生效.没有限制 :visited使用限制 :visited只适用于带href的a标签.如果给a标签绑定了click事件,那跳转的url必须跟href ...

  6. JavaScript DOM 高级程序设计读书笔记一

    创建可重用的对象 简而言之,对象就是包含一组变量(称为属性)和函数(称为方法)的集合的实例.对象通常由类派生而来,而类中定义了对象拥有的属性和方法.如果你的脚本中都是对象之间的交互操作,那么就可以称之 ...

  7. echarts 折线图自定义颜色与修改legend颜色

    option4 = { title : { text: '', subtext: '' }, color:['#2db7f5','#ff6600','#808bc6'],      //关键加上这句话 ...

  8. Django web编程2 -- 编辑页面内容

    你将创建一些表单,让用户能够添加主题和条目,以及编辑既有的条目.你还将学习Django如何防范对基于表单的网页发起的常见攻击,这让你无需花太多时间考虑确保应用程序安全的问题. 然后,我们将实现一个用户 ...

  9. Java基础--异常处理

    1.异常的传统处理方式 缺点: [1] 通过判断影响执行效率. [2] 判断逻辑和业务逻辑交织在一起,可维护性很差. public class Test01 { public static void ...

  10. Java基础知识拾遗(二)

    Lambda表达式 lambda表达式本质上就是一个匿名方法.但是这个方法不是独立执行的,而是构成了一个函数式接口定义的抽象方法的实现,该函数式接口定义了它的目标类型. 只有在定义了lambda表达式 ...