The Preliminary Contest for ICPC Asia Xuzhou 2019 Colorful String (回文自动机)

The value of a string ss is equal to the number of different letters which appear in this string.

Your task is to calculate the total value of all the palindrome substring.

Input

The input consists of a single string |s|(1 \le |s| \le 3 \times 10^5)∣s∣(1≤∣s∣≤3×105).

The string ss only contains lowercase letters.

Output

Output an integer that denotes the answer.

样例输入复制

abac

样例输出复制

6

样例解释

abacabac has palindrome substrings a,b,a,c,abaa,b,a,c,aba,ans the total value is equal to 1+1+1+1+2=61+1+1+1+2=6。

题意: 给出一个串s, 问串s中的所有回文串, 其含有的不同字母的个数之和为多少。

思路: 使用回文自动机, 求出所有本质不同的回文串, 并记录其位置, 预处理原序列得到cnt数组, 可知任意两个位置之间

含有的不同字母个数, 对于每个回文串,  已知其位置以及长度, 区间内不同字母的个数也就知道了, 再乘以该回文串的

个数, 累加即是答案。

#include <bits/stdc++.h>
//#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#ifdef LOCAL
#define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n"
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#else
#define TIME 0
#endif
#define hash_ 1000000009
#define Continue(x) { x; continue; }
#define Break(x) { x; break; }
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
ll fpow(ll a, ll b, int mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; }
char s[N];
int record[N];
struct Palindrome_tree{
	int nxt[N][26];
	int fail[N]; // 当前节点最长回文后缀的节点
	int len[N]; // 当前节点表示的回文串的长度
	int cnt[N]; // 当前节点回文串的个数, 在getcnt后可得到全部
	int sed[N]; // 以当前节点为后缀的回文串的个数
	int tot; // 节点个数
	int last; // 上一个节点
	void init()
	{
		tot = 0;
		memset(fail, 0, sizeof fail);
		memset(cnt, 0, sizeof cnt);
		memset(sed, 0, sizeof sed);
		memset(len, 0, sizeof len);
		memset(nxt, 0, sizeof nxt);
	}
	void build()
	{
		len[0] = 0, len[1] = -1; // 0为偶数长度根, 1为奇数长度根
		tot = 1, last = 0;
		fail[0] = 1;
	}
	int getfail(char *s, int x, int n)
	{
		while (s[n - len[x] - 1] != s[n]) // 比较x节点回文串新建两端是否相等
			x = fail[x]; // 若不同, 再比较x后缀回文串两端
		return x;
	}
	void insert(char* s, int n)
	{
		for (int i = 0; i < n; i++)
		{
			int c = s[i] - 'a';
			int p = getfail(s, last, i);// 得到第i个字符可以加到哪个节点的两端形成回文串 
			if (!nxt[p][c])
			{
				tot++;
				len[tot] = len[p] + 2;  // 在p节点两端添加两个字符
				fail[tot] = nxt[getfail(s, fail[p], i)][c]; //tot点的后缀回文,可以由上一个节点的后缀回文尝试得到  
				sed[tot] = sed[fail[tot]] + 1; // 以当前节点为结尾的回文串个数
				nxt[p][c] = tot; // 新建节点
			}
			last = nxt[p][c]; // 当前节点成为上一个节点
			cnt[last]++; //当前节点回文串++
			record[last] = i;
		}
	}
	void get_cnt()
	{
		for (int i = tot; i > 0; i--)
			cnt[fail[i]] += cnt[i];
		//fail[i] 的节点 为 i 节点的后缀回文串, 所以个数相加
	}
}pdt;
int cnt[N][27];
void pre(int n)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < 26; j++)
		{
			if (i)
				cnt[i][j] = cnt[i - 1][j];
		}
		++cnt[i][s[i] - 'a'];
	}
}
int calc(int l, int r)
{
	int res = 0;
	for (int i = 0; i < 26; i++)
	{
		int k = 0;
		if (l >= 1)
			k = cnt[l - 1][i];
		if (cnt[r][i] > k)
			++res;
	}
	return res;
}
int main()
{
#ifdef LOCAL
	freopen("D:/input.txt", "r", stdin);
#endif
	scanf("%s", s);
	int n = strlen(s);
	pre(n);
	pdt.init();
	pdt.build();
	pdt.insert(s, n);
	pdt.get_cnt();
	ll ans = 0;
	for (int i = 2; i <= pdt.tot; i++)
	{
		int R = record[i]; 
		int L = record[i] - pdt.len[i] + 1;
		ans += 1LL * calc(L, R) * pdt.cnt[i];
	}
	cout << ans << endl;
	return TIME;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值