字符串hash fzu 2137 奇异字符串


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值