TJ2nd做法,56pts(过hack)
查看原帖
TJ2nd做法,56pts(过hack)
416816
TG_Space_Station楼主2024/8/20 22:35
#include <bits/stdc++.h>
using namespace std;

const int N = (1 << 21) + 5;
int T, ans;
int n; string str;
int suf[N], pre[N];
int nxt[N], tmp[30], per[30];

int trans(char ch)
{
	return ch - 'a' + 1;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	int i, j;
	cin >> T;
	
	while(T--)
	{
		ans = 0;
		memset(per, 0, sizeof per), memset(nxt, 0, sizeof nxt);
		memset(suf, 0, sizeof suf), memset(pre, 0, sizeof pre);
		cin >> str; n = str.size(); str = " " + str;
		
		for(i = 2; i <= n; i++)
		{
			j = nxt[i - 1];
			while(str[j + 1] != str[i] && j) j = nxt[j];
			nxt[i] = j;
			if(str[j + 1] == str[i]) nxt[i]++;
		}
		
		memset(tmp, 0, sizeof tmp);
		for(i = n; i >= 1; i--)
		{
			tmp[trans(str[i])]++;

			if(tmp[trans(str[i])] % 2)
				suf[i] = suf[i + 1] + 1;
			else
				suf[i] = suf[i + 1] - 1;
		}
		
		memset(tmp, 0, sizeof tmp);
		for(i = 1; i <= n; i++)
		{
			tmp[trans(str[i])]++;
	
			if(tmp[trans(str[i])] % 2)
				pre[i] = pre[i - 1] + 1;
			else
				pre[i] = pre[i - 1] - 1;
		}
		
		for(i = 1; i < n; i++)
		{
			if(i >= 2)
			{
				ans += per[suf[i + 1]];
	
				for(j = 2 * i; j < n; j += i)
				{ 
					if((i % (j - nxt[j])) == 0 && (j / (j - nxt[j])) > 1)
						ans += per[suf[j + 1]];
					else
						break;
				}
			}

			for(j = pre[i]; j <= 26; j++) //for [1, pre[i]) the per won't change
				per[j] = per[j] + 1;
		}
		
		cout << ans << "\n";
	}
	return 0;
}
2024/8/20 22:35
加载中...