hash大法,還不懂為什麼要這要做,但是就是可以這樣做,先當模闆,類似問題可以套用。
#include"cstdio"
#include"cstring"
#include"cmath"
#include"cstdlib"
#include"iostream"
#include"algorithm"
using namespace std;
typedef unsigned long long ll;
const ll maxn=100100;
ll hash[maxn],base[maxn];
char s[maxn];
void init(ll len){
ll seed=131;
hash[0]=0;base[0]=1;
for(ll i=1;i<=len;i++){
hash[i]=hash[i-1]*seed+s[i];
base[i]=base[i-1]*seed;
}
}
ll gethash(ll L,ll R){
return hash[R]-hash[L-1]*base[R-L+1];
}
int main()
{
int T;
cin>>T;
while(T--){
scanf("%s",s+1);
ll len=strlen(s+1);
init(len);
ll ans=0;
for(ll i=2;i<len;i++){
ll L=i-1,R=i+1;
while(L>=1&&R<=len){
if(s[L]==s[i]||s[R]==s[i])break;
if(gethash(L,i-1)==gethash(i+1,R))
ans+=(ll)(2*(i-L)+1)*(2*(i-L)+1);
L--;R++;
}
}
printf("%I64u\n",ans);
}
return 0;
}