UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213777#2411. 三元组zhangxinyang1115094ms79452kbC++2.3kb2024-11-13 19:24:062024-11-13 23:01:43

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
using namespace std;
const int MAXN = 1e6 + 5;
const int MAXC = 26;
const int mod = 998244353;
int T;
int nm[MAXN][2];
char str[MAXN];
class PAM {
	public:
		struct node {
			map<int, int> ch;
			int fail, len, num;
			int sum;
		} T[MAXN];
		int las, tot;
		inline int get_fail(int x, int pos) {
			while (str[pos - T[x].len - 1] != str[pos]) {
				x = T[x].fail;
			}
			return x;
		}
		void init() {
			T[0].ch.clear(), T[1].ch.clear();
			T[0].fail = 1, T[1].fail = 0;
			T[0].len = 0, T[1].len = -1;
			T[0].num = T[1].num = T[0].sum = T[1].sum = 0;
			las = 0, tot = 1;
		}
		void insert1(char s[], int len) {
			s[0] = -1;
			for (int i = 1; i <= len; i++) {
				int p = get_fail(las, i);
				if (!T[p].ch[s[i] - 'a']) {
					T[++tot].len = T[p].len + 2;
					T[tot].ch.clear();
					int u = get_fail(T[p].fail, i);
					T[tot].fail = T[u].ch[s[i] - 'a'];
					T[tot].num = T[T[tot].fail].num + 1;
					T[tot].sum = (T[T[tot].fail].sum + T[tot].len) % mod;
					T[p].ch[s[i] - 'a'] = tot;
				}
				las = T[p].ch[s[i] - 'a'];
				nm[i][0] = (1ll * T[las].num * (i + 1) % mod - T[las].sum + mod) % mod;
			}
		}
		void insert2(char s[], int len) {
			s[0] = 0;
			for (int i = 1; i <= len; i++) {
				int p = get_fail(las, i);
				if (!T[p].ch[s[i] - 'a']) {
					T[++tot].len = T[p].len + 2;
					T[tot].ch.clear();
					int u = get_fail(T[p].fail, i);
					T[tot].fail = T[u].ch[s[i] - 'a'];
					T[tot].num = T[T[tot].fail].num + 1;
					T[tot].sum = (T[T[tot].fail].sum + T[tot].len) % mod;
					T[p].ch[s[i] - 'a'] = tot;
				}
				las = T[p].ch[s[i] - 'a'];
				int pos = len - i + 1;
				nm[pos][1] = (1ll * T[las].num * (pos - 1) % mod + T[las].sum) % mod;
			}
		}
} tree;
signed main() {
//	cin.tie(nullptr)->ios::sync_with_stdio(false);
	cin >> T;
	while (T--) {
		cin >> str + 1;
		int n = strlen(str + 1);
		tree.init();
		tree.insert1(str, n);
		int n2 = n >> 1;
		for (int i = 1; i <= n2; i++) swap(str[i], str[n - i + 1]);
		tree.init();
		tree.insert2(str, n);
		int res = 0;
		for (int i = 1; i < n; i++)
			res = (res + 1ll * nm[i][0] * nm[i + 1][1] % mod) % mod;
		cout << res << endl;
		for(int i=1;i<=n;i++) str[i]='\0';
	}
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 20ms
memory: 79376kb

input:

10
kyynjrttqngurtcvcjnnuqmstrntnpghrqqqp
lqfnqzykyzzynqnqyywzzwwfnyzlwykznyfhyknfnk
fuhjuhhuhuqjuuqu...

output:

27169
35530
31188
37732
65217
28457
133147
22985
43141
10759

result:

ok 10 lines

Test #2:

score: 10
Accepted
time: 20ms
memory: 79372kb

input:

10
eedleeeldeededlllleldleeeleleddleellele
kllrnnjqjniqqknjmlfcirjyrycfrkyqqjrky
uksfueafjuarusziepe...

output:

66808
20895
14900
37820
39914
38054
29364
37426
38775
29747

result:

ok 10 lines

Test #3:

score: 10
Accepted
time: 20ms
memory: 79380kb

input:

10
aaqzqvvvvzaqvazzvqqzqzavaaavvqaqqqvaqavvavvaavzvqzavaqvzvaazavqavazzvqvqazzvzvqqaqaaqqazqaqqzzzza...

output:

6230886
3556080
11774834
3833447
8206626
3621091
5300328
6484259
3025216
18509610

result:

ok 10 lines

Test #4:

score: 10
Accepted
time: 19ms
memory: 79400kb

input:

10
bsppzzcusncccbbpgguszngssnzncsnzpppbpuuczgcbucpupcubbzubsncncpbgbuzungnznsnpncpbugzcpguungzgcnbng...

output:

559443824
305794405
414279981
912202469
497470943
263140764
422669314
204178180
579056344
460822521

result:

ok 10 lines

Test #5:

score: 10
Accepted
time: 15ms
memory: 79452kb

input:

10
pzrpbfgjbnqhbpzohubzunqjnujfbfgazjqeaqhjgppqnogagegebuebhbeoeboouoojhbhaahjooggrozeephgqfrzrfrpjz...

output:

442803205
483459867
245377898
502317569
156660068
301473465
396310915
489306288
862513697
753896393

result:

ok 10 lines

Test #6:

score: 0
Time Limit Exceeded

input:

100000
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

341665830
471661
1999742
543480
459540
202296822
191363172
1241848
713235
369157
575809
170913820
68...

result:


Test #7:

score: 0
Time Limit Exceeded

input:

10000
iqtuiwnrdznudrwdznuwoyhzhhmiqytqhdqjuhihyuyrmwqurmdzdwiidomqrihqrjzyjwytjhznuwuhiojnhiduhytjir...

output:

452745570
395658802
415729637
581286624
608938037
896800719
444684505
913683462
444333468
228535301
...

result:


Test #8:

score: 0
Time Limit Exceeded

input:

1000
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...

output:

845573779
254782300
56185577
239721206
427653199
812292186
496897443
160197728
268511769
327773907
7...

result:


Test #9:

score: 0
Time Limit Exceeded

input:

100
yuuxiorshssxoiyxrsxyyyuxsuyuhsruhxisoyiuuixhrsxuxyyxxosihhyisxxohoyiiurxhixrruryiihxhsxhxuhoroou...

output:

732636589
377635994
204944545
800901731
855220417
505651421
500433866
804284443
619658341
415348374
...

result:


Test #10:

score: 0
Time Limit Exceeded

input:

10
qvvvvqqgqgvvvgqvgqvqqwqgwqvqggvgqqqgqvwwggwwqqqqqwvwqggqvgqqvvvvvwqvqvvgvvqgvwqggwqqvvwwvvgwwwgqg...

output:

277639311
181813441
852565296
302618524
261112096
362781838
797779507

result: