QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#32269#81. Hold or Continue?querqqqWA 2ms9996kbC++1.4kb2022-05-18 16:02:442022-05-18 16:02:45

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-18 16:02:45]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9996kb
  • [2022-05-18 16:02:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int last, cnt;
int ch[maxn << 1][26], fa[maxn << 1], len[maxn << 1], siz[maxn << 1];
void ins(int c) {
	int p = last, np = ++cnt;
	last = np;
	len[np] = len[p] + 1;
	for (; p && !ch[p][c]; p = fa[p])ch[p][c] = np;
	if (!p)fa[np] = 1;
	else {
		int q = ch[p][c];
		if (len[p] + 1 == len[q])fa[np] = q;
		else {
			int nq = ++cnt; len[nq] = len[p] + 1;
			memcpy(ch[nq], ch[q], sizeof(ch[q]));
			fa[nq] = fa[q]; fa[q] = fa[np] = nq;
			for (; ch[p][c] == q; p = fa[p])ch[p][c] = nq;
		}
	}
	siz[np] = 1;
}

char s[maxn], t[maxn];
int n, m, T;
int dp[maxn];
int main() {
	scanf("%s", s + 1);
	n = strlen(s + 1);
	last = cnt = 1;
	for (int i = 1; i <= n; i++)
		ins(s[i] - 'A');
	scanf("%d", &T);
	while (T--) {
		scanf("%s", t + 1);
		m = strlen(t + 1);
		fill(dp, dp + m + 1, INF);
		int p = 1;
		dp[0] = 0;
		bool flg = 1;
		int tmp = 0;
		for (int i = 1; i <= m; i++) {
			int c = t[i] - 'A';
			//int tmp = 0;
			if (ch[p][c]) {
				tmp++;
				p = ch[p][c];
			}
			else {
				while (p && !ch[p][c])p = fa[p];
				if (p == 0 && !ch[p][c]) {
					flg = 0;
					break;
				}
				else {
					tmp = len[p] + 1;
					p = ch[p][c];
				}
			}
			dp[i] = min(dp[i], dp[i - tmp] + 1);
		}
		printf("%d\n", flg ? dp[m] : -1);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 9996kb

input:

3
15 0 3
35 50 40
15 0 30

output:

-1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

wrong answer 1st lines differ - expected: 'C', found: '-1'