1.利用unsigned long long 越界取模
2.题意的奇异串是AxA,就是x旁边两个串是要一样的,不是相反的。注意x不能在A中出现,根据这个,A的范围只可能在x与上一个字母x之间,可以直接枚举。这样对于一个字母x,总的枚举复杂度是O(n)的,最多26种字母,总复杂度O(26*n)。枚举后判断是否一样可以用hash,或者后缀数组。
#include <cstdio>
#include <deque>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define Debug(x) (cerr << #x << " = " << (x) << endl)
#define Debug2(x, y) (cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl)
template<class T> inline T& RD(T &x){
char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x;
}
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
typedef unsigned long long uint;
const uint MAGIC = 131;
uint h[maxn];
uint base[maxn];
void init_hash(string s){
h[0] = 0;
int l = s.length();
for(int i=1;i<=l;i++){
h[i] = h[i-1]*MAGIC + s[i-1];
}
base[0] = 1;
for(int i=1;i<=l;i++){
base[i] = base[i-1]*MAGIC;
}
}
// 子串[l,r)的hash值 编号从0开始
uint hash(int l,int r){
return h[r] - h[l]*base[r-l];
}
/*example
字符串1234567890 MAGIC = 10
hash[2,4)
h[0:9] = [0, 1, 12, 123,1234, 12345, ....];
h[r] = h[4] = 1234
h[l] = h[2] = 12
hash[l,r) = 1234 - 12*base[4-2] = 34;
*/
string s;
int main(){
int T;
scanf("%d",&T);
while(T --){
cin >> s;
init_hash(s);
uint ans = 0;
for(int i=1;i<s.length()-1;i++){
int front = i-1;
int tail = i+1;
while(front >= 0 && tail < s.length()){
if(s[front] == s[i] || s[i] == s[tail]){
break;
}
if(hash(front,i) == hash(i+1,tail+1)){
ans += (uint)(tail-front+1)*(tail-front+1);
}
front --,tail ++;
}
}
printf("%I64u\n", ans);
}
return 0;
}