QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#794184#8521. Pattern Search IIwangjunruiWA 18ms13100kbC++141.5kb2024-11-30 12:42:462024-11-30 12:42:46

Judging History

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

  • [2024-11-30 12:42:46]
  • 评测
  • 测评结果:WA
  • 用时:18ms
  • 内存:13100kb
  • [2024-11-30 12:42:46]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1.5e5 + 5;
int n;
char str[N];
struct node
{
	int size, ans;
	int pref[N], preg[N];
	int suff[N], sufg[N];
	inline node friend operator+(const node &lhs, const node &rhs)
	{
		node res;
		res.size = lhs.size + rhs.size;
		res.ans = min(lhs.ans, rhs.ans);
		for (int i = 1; i <= n; ++i)
		{
			if (lhs.pref[i] == n + 1)
			{
				res.pref[i] = lhs.pref[i];
				res.preg[i] = lhs.preg[i];
			}
			else
			{
				res.pref[i] = rhs.pref[lhs.pref[i]];
				res.preg[i] = lhs.size + rhs.preg[lhs.pref[i]];
			}
			
			if (rhs.sufg == 0)
			{
				res.suff[i] = lhs.suff[i];
				res.sufg[i] = lhs.sufg[i];
			}
			else
			{
				res.suff[i] = lhs.suff[rhs.suff[i]];
				res.sufg[i] = lhs.sufg[rhs.suff[i]] + rhs.size;
			}
			if (lhs.suff[i] == 0 && rhs.pref[i + 1] == n + 1)
				res.ans = min(res.ans, lhs.sufg[i] + rhs.preg[i + 1]);
		}
		return res;
	}
} A, B;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> str;
	n = (int)strlen(str);
	if (n == 1)
	{
		cout << "1\n";
		return 0;
	}
	A.size = B.size = 1;
	for (int i = 1; i <= n; ++i)
	{
		if (str[i - 1] == 'a')
		{
			A.pref[i] = A.suff[i] = i;
			B.pref[i] = i + 1, B.suff[i] = i - 1;
			B.preg[i] = B.sufg[i] = 1;
		}
		else
		{
			B.pref[i] = B.suff[i] = i;
			A.pref[i] = i + 1, A.suff[i] = i - 1;
			A.preg[i] = A.sufg[i] = 1;
		}
	}
	A.ans = B.ans = INT_MAX;
	for (int i = 1; i <= 30; ++i)
	{
		swap(A, B);
		B = A + B;
	}
	cout << B.ans << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 16ms
memory: 13028kb

input:

aabbaab

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 1ms
memory: 6004kb

input:

a

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5940kb

input:

b

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 18ms
memory: 13080kb

input:

aa

output:

2

result:

ok 1 number(s): "2"

Test #5:

score: -100
Wrong Answer
time: 17ms
memory: 13100kb

input:

bb

output:

4

result:

wrong answer 1st numbers differ - expected: '3', found: '4'