QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#125884#801. 回文自动机NOI_AK_ME#0 5ms22144kbC++233.6kb2023-07-17 20:50:032023-07-17 20:52:08

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-17 20:52:08]
  • 评测
  • 测评结果:0
  • 用时:5ms
  • 内存:22144kb
  • [2023-07-17 20:50:03]
  • 提交

answer

#include <cstdio>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <cstring>

typedef long long int int64;

const int Max_N = 1000005;

namespace SA_IS
{
	int *sa;
	
	template<typename _Char>
	void sais_core(const int n, const int m, const _Char s[], char type[], int lms[], int cnt[])
	{
		int n1 = 0, ch = -1;
		
		type[n - 1] = 1;
		for (int i = n - 2; i >= 0; --i)
			type[i] = s[i] == s[i + 1] ? type[i + 1] : s[i] < s[i + 1];
		
		std::fill(cnt, cnt + m, 0);
		for (int i = 0; i < n; ++i)
			++cnt[static_cast<int>(s[i])];
		std::partial_sum(cnt, cnt + m, cnt);
		
		auto induced_sort = [&](const int v[])
		{
			std::fill(sa, sa + n, 0);
			
			int *cur = cnt + m;
			auto push_S = [&](const int x) { sa[--cur[static_cast<int>(s[x])]] = x; };
			auto push_L = [&](const int x) { sa[cur[static_cast<int>(s[x])]++] = x; };
			
			std::copy(cnt, cnt + m, cur);
			for (int i = n1 - 1; i >= 0; --i)
				push_S(v[i]);
			
			std::copy(cnt, cnt + m - 1, cur + 1);
			for (int i = 0; i < n; ++i)
				if (sa[i] > 0 && type[sa[i] - 1] == 0)
					push_L(sa[i] - 1);
			
			std::copy(cnt, cnt + m, cur);
			for (int i = n - 1; i >= 0; --i)
				if (sa[i] > 0 && type[sa[i] - 1])
					push_S(sa[i] - 1);
		};
		
		for (int i = 1; i < n; ++i)
			if (type[i - 1] == 0 && type[i] == 1)
				type[i] = 2, lms[n1++] = i;
		induced_sort(lms);
		
		auto lms_equal = [&](int x, int y)
		{
			if (s[x] == s[y])
				while (s[++x] == s[++y] && type[x] == type[y])
					if (type[x] == 2 || type[y] == 2)
						return true;
			return false;
		};
		
		int *s1 = std::remove_if(sa, sa + n, [&](const int x) { return type[x] != 2; });
		for (int i = 0; i < n1; ++i)
			s1[sa[i] >> 1] = ch += ch <= 0 || !lms_equal(sa[i], sa[i - 1]);
		for (int i = 0; i < n1; ++i)
			s1[i] = s1[lms[i] >> 1];
		
		if (ch + 1 < n1)
			sais_core(n1, ch + 1, s1, type + n, lms + n1, cnt + m);
		else
			for (int i = 0; i < n1; ++i)
				sa[s1[i]] = i;
		
		for (int i = 0; i < n1; ++i)
			lms[n1 + i] = lms[sa[i]];
		induced_sort(lms + n1);
	}
	
	template<typename _Char>
	void main(const _Char s[], const int n, const int m)
	{
		static int _lms[Max_N], _cnt[Max_N << 1];
		static char _type[Max_N << 1];
		sais_core(n + 1, m, s, _type, _lms, _cnt);
	}
}

void klaap(const char s[], const int sa[], int lcp[], const int n)
{
	static int rk[Max_N];
	for (int i = 0; i < n; ++i)
		rk[sa[i]] = i;
	for (int i = 0, h = lcp[0] = 0; i < n; ++i)
		if (h -= h != 0, rk[i])
		{
			for (int j = sa[rk[i] - 1]; i + h < n && j + h < n && s[i + h] == s[j + h]; ++h)
				;
			lcp[rk[i]] = h;
		}
}

int scnt, parent[Max_N * 2], len[Max_N * 2], cnt[Max_N * 2];

inline void link(const int v, const int p)
{
	parent[v] = p;
}

char s[Max_N];
int64 Ans;

int main(int argc, char **argv)
{
	scanf("%s", s);
	int n = strlen(s);
	
	static int sa[Max_N], lcp[Max_N];
	
	SA_IS::sa = sa;
	SA_IS::main(s, n, 128);
	klaap(s, sa + 1, lcp, n);
	
	len[0] = -1, scnt = 1;
	len[++scnt] = n - sa[1], cnt[scnt] = 1, link(scnt, 1);
	for (int i = 1; i != n; ++i)
	{
		int p = scnt;
		while (len[parent[p]] >= lcp[i])
			cnt[parent[p]] += cnt[p], p = parent[p], Ans = std::max(Ans, static_cast<int64>(cnt[p]) * len[p]);
		if (len[p] != lcp[i])
			len[++scnt] = lcp[i], link(scnt, parent[p]), link(p, scnt), cnt[scnt] += cnt[p], p = scnt;
		len[++scnt] = n - sa[i + 1], cnt[scnt] = 1, link(scnt, p);
	}
	int p = scnt;
	while (p)
		cnt[parent[p]] += cnt[p], p = parent[p], Ans = std::max(Ans, static_cast<int64>(cnt[p]) * len[p]);
	
	printf("%lld", Ans);
	
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 35
Accepted
time: 0ms
memory: 22144kb

input:

bdgedcfadbabbfgeacdgbggaefddebabbfgeacdgbefaecfddffeddacbabfcgecedacbffeddacbabfebadggfafabcdfdeaabdeecgbcecegcgecedacbfgdagbgagafdegecadfebcdbgfacdecdegecadfebbcdfdeaabdbfgcbccfcaebcecfdfccagdafaeaacbggaefddebcbecdafageeaabcbdafadcbecdbcgcbdgedcfadbcaefbdfcbgfcdeceddaaffgcedfcdcgdcgbfdddfdadgagbbef...

output:

5594

result:

ok answer is '5594'

Test #2:

score: 0
Accepted
time: 2ms
memory: 20040kb

input:

bdgfcbabegfbbbgecfbddbaceaefbebgeafdbbgaebebdabgebabacccebbaebeafbefaabdgfcbabegdbaceaefbegcaegagcdgcacccfbbfgffgcdgbccgecbdbcagbbcacccfbbfgeegfcaecbcebebdabgebbbebbgcfafbbbgbdbabgbabfgdfaggfbcbabeebbdaagacgbafecebfccdbgfacgcabefaaedadeacgdeegfcaecbcabacccebbacdbbdceeegcdbbdceeegbaccaecfbgbbebbgcfaf...

output:

7308

result:

ok answer is '7308'

Test #3:

score: 0
Accepted
time: 5ms
memory: 18124kb

input:

baeedcbgaeaabdcaeeagbeffgedegdfcggaeafeegccecbacaaaabdcaeeaggedcbbaebfbcbbbebeaeagedddgabgccdecfeegcababaddfcabcbbbebeaegabeddeedaaabebgcafgeefgeabcaafgcbcfaafgadddgdbccbcddfacfcgdeefgeabcaagbgbgdbefdcefcacafcagcfadegebcababaddfcaffbfgdfecefgafcfgddbagfgceabefcaaebagddabcbbbebeaedaddaacgfcabeffgfgeg...

output:

6011

result:

ok answer is '6011'

Test #4:

score: 0
Accepted
time: 5ms
memory: 18276kb

input:

fadabcedabffccgceafdfgebfgebdfffccgceafdfbabeebbccbcebdaabagbdcabbebbgbbdddddcfdfefcfgcaedcdfbfcgagggeacabgddfdggddgcgagfefgeafdaefefgeafdaefbabeebbccabccadccgcbbdddddcfdfadabcedabgbdegbcgdecfcefaedcffadabcedabadgbbacdfbfecccfacaaggggffddffffbcgacfgbcbeadagbfffefcfgcaedgeacabgddfgbcccdcgegbdcabbebbg...

output:

5874

result:

ok answer is '5874'

Test #5:

score: 0
Accepted
time: 0ms
memory: 20200kb

input:

efggbbfcabcdfbceagadfagaeegbegcbfbcfcgfbgdfffcdeagfcggffcacbbadedceffedbgafcbegdggabccbcecfcbfegdcbecdedfdeebebecffcaafgffabgbgedfcdabgbeffaagbffcdccaddcadgbbcadedgcfbgbdefggbbfcabddccdefedagebfbbfadfagagedffaagabcgbcffaggfebdbefdcfecegaeggggdacedbgfcdedfdeebececbfefeegdebddaeafbffabgbgedfcfcdgcaacc...

output:

5751

result:

ok answer is '5751'

Test #6:

score: -35
Wrong Answer
time: 2ms
memory: 22120kb

input:

eafdccagcbcaeebabcggdgdfcdfgeacbgcdfbgcabdegdbbaabgecbaaagffecffedeffcedcdgcecbgfedbdgabfdcbefcecdfdfaegccbfeefdccgfbfebecffedeffcbdegdbbaabeafcbegcbagecbaaagffefcdafgageffefbcceecdffabcbbdbefcdafgageedcdgcecbgcaeebabcggfbeegdaccfdffabcbbdbcbefcecdfdcdccfcbadcecffedeffcdcgaagfaegddecgffafcdgdfcdfgea...

output:

5755

result:

wrong answer expected '6156', found '5755'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%