I will give you two strings a and B of the same length. You can invert a substring of string a once to make a and B the same. I will ask you how many ways to invert.
It is discussed in two cases. If the initial a and B are the same, the answer is the sum of the palindrome radius of each character. If a and B are different, find out the first different position on the left and the first different position on the right respectively, and then expand the statistical answer.
AC Code:
#include <bits/stdc++.h> using namespace std; typedef long long ll; string a,b; const int maxn=2e6+5; int p[maxn]; void manacher() { int len=a.size(); string t="$#"; for(int i = 0;i < len;++i) t+=a[i],t+='#'; vector<int>v(t.size(),0); int mx=0,id=0; for(int i = 1;i < t.size();++i) { v[i]=mx>i?min(v[id*2-i],mx-i):1; while(t[i+v[i]]==t[i-v[i]]) v[i]++; if(mx<i+v[i]) mx=i+v[i],id=i; } ll ans=0; for(int i = 2;i < t.size();i++){ ans+=v[i]/2; } cout<<ans<<endl; } int main() { ios::sync_with_stdio(false); int t; cin>>t; while(t--) { cin>>a>>b; int len=a.size(); if(a==b) { manacher(); } else { int l=0,r=len-1; for(int i = 0;i < len;++i) { if(a[i]!=b[i]) { l=i; break; } } for(int i = len-1;i >= 0;--i) { if(a[i]!=b[i]) { r=i; break; } } string a1=a.substr(l,r-l+1); string b1=b.substr(l,r-l+1); reverse(a1.begin(),a1.end()); if(a1==b1) { ll ans=1; while(l>0&&r<len&&(a[--l]==b[++r])) ans++; cout<<ans<<endl; } else cout<<"0"<<endl; } } return 0; }