Strings in the Pocket

Time Limit: 1 Second Memory Limit: 65536 KB
BaoBao has just found two strings s=s1s2…sns=s_1s_2\dots s_ns=s1​s2​…sn​ and t=t1t2…tnt=t_1t_2\dots t_nt=t1​t2​…tn​ in his left pocket, where sis_isi​ indicates the iii-th character in string , and tit_iti​ indicates the iii-th character in string ttt.

As BaoBao is bored, he decides to select a substring of and reverse it. Formally speaking, he can select two integers lll and rrr such 1≤l≤r≤n1 \le l \le r \le n1≤l≤r≤nthat and change the string to s1s2…sl−1srsr−1…sl+1slsr+1…sn−1sns_1s_2\dots s_{l-1}s_rs_{r-1}\dots s_{l+1}s_ls_{r+1}\dots s_{n-1}s_ns1​s2​…sl−1​sr​sr−1​…sl+1​sl​sr+1​…sn−1​sn​.

In how many ways can BaoBao change to using the above operation exactly once? Let (a,b)(a,b)(a,b) be an operation which reverses the substring sasa+1…sbs_as_{a+1}\dots s_bsa​sa+1​…sb​, and (c,d)(c,d)(c,d) be an operation which reverses the substring scsc+1…sds_cs_{c+1}\dots s_dsc​sc+1​…sd​. These two operations are considered different, if a≠ca=\not ca≠​c or b≠db=\not db≠​d.

Input

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

The first line contains a string s(1≤∣s∣≤2×106)s(1\le |s| \le 2 \times 10^6)s(1≤∣s∣≤2×106), while the second line contains another string t(∣t∣=∣s∣)t(|t| = |s|)t(∣t∣=∣s∣). Both strings are composed of lower-cased English letters.

It’s guaranteed that the sum of ∣s∣|s|∣s∣ of all test cases will not exceed 2×1072 \times 10^72×107.

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

题目大意:

先输入一个整数ttt,代表共有ttt组测试样例,对于每一组测试样例,输入两个字符串s1,s2s_1,s_2s1​,s2​,现在仅能将字符串s1s_1s1​中的某个字串翻转s1s_1s1​与s2s_2s2​相等,问共有几中反转方法。

解题思路:

此题可以将题目分为两种情况,一种是字符串s1s_1s1​与字符串s2s_2s2​完全相同,对于这种情况,可以通过manacher来计算字符串sis_isi​中每个回文串的长度,将长度求和即为总的方案数,对于s1s_1s1​与s2s_2s2​不等的情况,可以先从左往右找到s1s_1s1​与s2s_2s2​第一个不同的位置lll,同理,从右往左找到两字符串第一次不同的位置rrr,先判断s1s_1s1​的字串slsl+1…sr−1srs_ls_{l+1}\dots s_{r-1}s_rsl​sl+1​…sr−1​sr​反转后是否会使s1s_1s1​与s2s_2s2​完全相同,不会的话证明无法在反转一次的情况下使两字符串相同,输出000,否则则以此字串为边界同时往两边扩展,判断字串长度是否能延伸,记录输出即可。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
string s1,s2,s1_new;
int p[6004000];
int get_newstring() {s1_new.clear();int len=s1.size();s1_new+='$';for(int i=0;i<len;i++) {s1_new+='#';s1_new+=s1[i];}s1_new+='#';return s1_new.size();
}
ll manacher() {int len=get_newstring();fill(p,p+len,0);int id=0,mx=0;ll ans=0;for(int i=0;i<len;i++) {if(i<mx) p[i]=min(p[2*id-i],mx-i);elsep[i]=1;while(s1_new[i-p[i]]==s1_new[i+p[i]]) {p[i]++;}if(p[i]+i>mx) {mx=i+p[i];id=i;}ans+=(ll)(p[i]/2);}return ans;
}
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;cin>>t;while(t--) {cin>>s1>>s2;ll ans=0;if(s1==s2) {ans=manacher();}else {int l=0,r=s1.size()-1;while(s1[l]==s2[l]) l++;while(s1[r]==s2[r]) r--;bool ju=false;int t1=l,t2=r;while(s1[t1]==s2[t2]) {if(t1==r&&t2==l) break;t1++;t2--;}if(t2==l&&t1==r) {ju=true;}else ju=false;if(l==r) ju=false;if(ju==true) {ans=1;l--,r++;while(l>=0&&r<s1.size()&&s1[l]==s2[r]&&s1[r]==s2[l]) {ans++;l--;r++;}}else ans=0;}cout<<ans<<endl;}return 0;
}

【manacher】Strings in the Pocket相关推荐

  1. 【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)

    题干: BaoBao has just found two strings  and  in his left pocket, where  indicates the -th character i ...

  2. hdu 3068 最长回文【manacher】(模板题)

    <题目链接> 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如a ...

  3. 【POI2007】OSI-Axes of Symmetry【计算几何】【manacher】

    题意:给一个 nnn 个点的多边形,求对称轴个数. n≤105n\leq 10^5n≤105 显然对称轴一定在顶点或边的中点上. 但你 n2n^2n2 枚举完全没有一点能过的样子. 冷静分析,发现有 ...

  4. 【Manacher】【贪心】字符串连接(金牌导航 Manacher-4)

    正题 金牌导航 Manacher-4 题目大意 给出一个字符串,让你用最少的回文串连接得到该串(这里连接是可以有重合的) 解题思路 先用Manacher求出以x为左端点的回文串右端点最大的位置 然后在 ...

  5. 【Manacher】绿绿和串串(luogu 5446)

    正题 luogu 5446 题目大意 定义对折为:以最后一个字符为对称轴翻转 问你一个字符串可能是由那些前缀对折若干次而来的 解题思路 先用Manacher求出回文长度 那么满足以下条件之一的前缀就是 ...

  6. 【Manacher】最长双回文串(luogu 4555)

    正题 luogu 4555 题目大意 给出一个字符串,让你求两个相邻的回文串的最大长度和 解题思路 先用Manacher求出最长回文串 然后暴力配对即可 代码 #include<cstdio&g ...

  7. 【manacher】双倍回文(金牌导航 manacher-2/luogu 4287)

    双倍回文 金牌导航 manacher-2 luogu 4287 题目大意 设串为x,将其取反为x',定义双倍回文为形如xx'xx'的串 现在给你一个字符串,让你求最大双倍回文子串 输入样例 16 gg ...

  8. BZOJ2342 Shoi2011 双倍回文 【Manacher】

    BZOJ2342 Shoi2011 双倍回文 Description Input 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. Output 输 ...

  9. 【Go】strings库字符串处理详说

    一.引入 strings 包实现了用于操作字符的简单函数 import "strings" 二.使用 EqualFold | 判断两个utf-8编码字符串(将unicode大写.小 ...

最新文章

  1. 简述Linux 文件系统的目录结构
  2. 设备租赁系统源码_滑雪场一卡通管理系统,设备租赁更简便
  3. linux 进程间通信 dbus-glib【实例】详解二(上) 消息和消息总线(附代码)
  4. Java中,一切皆是对象——java中的对象类型与基本数据类型的区别
  5. Dx11DemoBase 基类(二) 初始化 DirectX11 的 4个基本步骤
  6. 阿里云ecs实例中创建数据库
  7. 【EOJ Monthly 2019.02 - B】解题(思维,抽屉原理,暴力,模运算,优化,tricks)
  8. python 引入同一路径的类_Python入门 模块导入 import ...\from... import...
  9. e盾服务端源码_原罪西游源码发布!!!
  10. 【maven】论 maven settings 文件 mirrors 对 IDEA 的影响
  11. python制作合同模板带图片_办公自动化7_用Python操作Word批量生成合同
  12. python学习day05
  13. linux 下常用操作命令
  14. 全国多地元旦迎雾霾天气 京津冀霾明天短暂减弱
  15. 怎么利用matlab求导,利用Matlab求导的几个命令
  16. HDU2825-AC自动机+状压dp
  17. 在线购物系统——设计类
  18. 绿色软件在Windows10中设置开机自启方法
  19. MySql学习【一】mysql的安装,操作数据库/表/查询表中数据/mysql日期计算
  20. 使用7z程序CLI实现基础功能

热门文章

  1. BugkuCTF-MISC题旋转跳跃
  2. java map清除值为null的元素_Java中的集合框架大总结
  3. 个人家用nas_NAS不会用?NAS真的很难操作吗?可能是你没选对!
  4. 计算机应用基础自考,自考计算机应用基础
  5. android测光代码,常用测光表软件:Android篇
  6. 三星s10能升级android11,三星 S10+手机已在测试 Android 11 系统
  7. 暗黑2战网服务器爆率修改,暗黑2修改MOD最初级基础
  8. java 数据纠错,纠错码简介
  9. java生成pdf怎么合并行或者列_Java基础之PDF文件的合并
  10. 计算机网络协议的特点,计算机网络传输层协议类型与特点