QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#506473#8475. Palindrome Stringszhoukangyang#WA 0ms18128kbC++142.8kb2024-08-05 17:52:072024-08-05 17:52:07

Judging History

你现在查看的是最新测评结果

  • [2024-08-05 17:52:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:18128kb
  • [2024-08-05 17:52:07]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int > 
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
#define pb emplace_back
#define i128 __int128
using namespace std;
const int N = 2e6 + 7;
int n, Q;
char s[N];
int cnt[N];
int ch[N][26], fa[N], len[N], lst = 1, tot = 1;
void cpy(int x, int y) {
	memcpy(ch[y], ch[x], sizeof(int) * 26), fa[y] = fa[x], len[y] = len[x];
}
void ins(int x) {
	int p = lst, now = lst = ++tot;
	len[now] = len[p] + 1;
	for(; p && !ch[p][x]; p = fa[p]) ch[p][x] = now;
	if(!p) fa[now] = 1;
	else {
		int pto = ch[p][x];
		if(len[pto] == len[p] + 1) fa[now] = pto;
		else {
			int sp = ++tot;
			cpy(pto, sp), len[sp] = len[p] + 1;
			fa[pto] = fa[now] = sp;
			for(; p && ch[p][x] == pto; p = fa[p]) ch[p][x] = sp;
		}
	}
}

int q[N], c[N];
ll sum[N];
void qsort() {
	L(i, 1, tot) 
		c[len[i]] += 1;
	L(i, 1, tot) 
		c[i] += c[i - 1];
	L(i, 1, tot) 
		q[c[len[i]]--] = i;
	R(i, tot, 1) 
		cnt[fa[q[i]]] += cnt[q[i]], 
		sum[fa[q[i]]] += sum[q[i]];
}

char b[N];
int mx[N];
int edp[N];
void Add(int l, int r) {
	edp[l] += 1;
	edp[r + 1] -= 1;
}
void manacher(int n) {
	b[0] = '$', b[n * 2 + 1] = '#';
	L(i, 1, n) b[i * 2 - 1] = '#', b[i * 2] = s[i];
	n <<= 1;
	int l = 0;
	L(i, 1, n) {
		if(i < l + mx[l]) mx[i] = min(mx[l * 2 - i], l + mx[l] - i);
		while(b[i + mx[i]] == b[i - mx[i]]) ++mx[i];
		if(i + mx[i] > l + mx[l]) l = i;
	}
	L(i, 2, n) {
		int pos = i / 2;
		int dis = mx[i] / 2 - 1;
		if(i % 2 == 0) {
			Add(pos - dis, pos);
		} else {
			Add(pos - dis - 1, pos - 1);
		}
	}
}
const int mod = 1e9 + 7;

int lsp[N];
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> Q;
	string S;
	cin >> S;
	reverse(S.begin(), S.end());
	L(i, 1, n) s[i] = S[i - 1];
	manacher(n);
	L(i, 1, n + 1) edp[i] += edp[i - 1];
	L(i, 1, n) ins(s[i] - 'a'), lsp[i] = lst, ++cnt[lsp[i]], sum[lsp[i]] += edp[i + 1] + 1;
	qsort();
	// cout << S << endl;
	// L(i, 1, n) cout << edp[i] << ' ';
	// cout << endl;
	while(Q--) {
		string s;
		cin >> s;
		reverse(s.begin(), s.end());
		int v1 = 0, v2 = 0, pw = 1;
		int len = sz(s);
		vi vis(len + 1);
		L(i, 0, len - 1) {
			(v1 += (ll)pw * (s[i] - 'a' + 1) % mod) %= mod;
			v2 = ((ll)v2 * 33 + s[i] - 'a' + 1) % mod;
			pw = (ll)pw * 33 % mod;
			if(v1 == v2) {
				// cout << "visit " << i << endl;
				vis[i + 1] = 1;
			}
		}
		ll ans = 0;
		int cur = 1;
		R(i, sz(s) - 1, 0) {
			cur = ch[cur][s[i] - 'a'];
			if(vis[i]) {
				ans += cnt[cur];
			}
		}
		ans += sum[cur];
		cout << ans << '\n';
	}
	return 0;
} 

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 17940kb

input:

8 3
icpccamp
p
c
pc

output:

4
7
4

result:

ok 3 number(s): "4 7 4"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 18128kb

input:

10 3
bbbabbbbbb
baaaa
abb
bb

output:

63
4
30

result:

wrong answer 1st numbers differ - expected: '10', found: '63'