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;
}