답안 #346538

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
346538 2021-01-10T06:24:47 Z aritra741 회문 (APIO14_palindrome) C++14
100 / 100
40 ms 40968 KB
#include <bits/stdc++.h>
using namespace std;


const int MAXN = 300007;
const int N = 26;

long long ans;

struct Palindromic_Tree
{
	int next[MAXN][N];
	int fail[MAXN];
	long long cnt[MAXN]; // কতবার palindromic sub-string টি মুল string এর মধ্যে

	int num[MAXN]; //many palindromic sub-string ends in that position
	int start[MAXN];
	int end[MAXN];
	int len[MAXN];
	int S[MAXN];
	int last;
	int n;
	int p; // total node(number of unique palindrom in string)


	int newnode(int l)
	{
		for (int i = 0; i < N; i++) next[p][i] = 0;
		cnt[p] = 0;
		num[p] = 0;
		len[p] = l;
		return p++;
	}

	void init()
	{
		p = 0;
		newnode(0);
		newnode(-1);
		last = 0;
		n = 0;
		S[n] = -1;
		fail[0] = 1;
	}

	int get_fail(int x) { // KMP
		while (S[n - len[x] - 1] != S[n]) x = fail[x];
		return x;
	}

	void add(int c, int i)
	{
		c -= 'a';
		S[++n] = c;

		int cur = get_fail(last);

		if (!next[cur][c]) {
			int now = newnode(len[cur] + 2);
			end[now] = i;
			start[now] = i - len[cur] - 2 + 1;
			fail[now] = next[get_fail(fail[cur])][c];
			next[cur][c] = now;
			num[now] = num[fail[now]] + 1;
		}
		last = next[cur][c];
		cnt[last]++;
	}

	void count()
	{
		for (int i = p - 1; i >= 0; --i) cnt[fail[i]] += cnt[i];
	}

	void print()
	{
		for (int i = 1; i <= p - 2; i++)
		{
			printf("%c ", S[i] + 'a');
		}
		printf("\n");

		for (int i = 2; i < p; i++)
		{
			printf("%lld ", cnt[i]);
		}
		printf("\n");
		for (int i = 2; i < p; i++)
		{
			printf("%d ", num[i]);
		}
		printf("\n");
		for (int i = 2; i < p; i++)
		{
			printf("%d ", start[i]);
		}
		printf("\n");
		for (int i = 2; i < p; i++)
		{
			printf("%d ", end[i]);
		}
		printf("\n");
	}
} tt;


int main()
{
	string str;

	cin >> str;

	tt.init();

	for (int i = 0; i < str.size(); i++)
	{
		tt.add(str[i], i);
	}

	tt.count();

	for( int i=0;i<tt.p;i++ )
		ans = max( ans, tt.cnt[i] * tt.len[i] ); 

	printf("%lld\n", ans);
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:115:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |  for (int i = 0; i < str.size(); i++)
      |                  ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 0 ms 364 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1772 KB Output is correct
2 Correct 2 ms 1772 KB Output is correct
3 Correct 2 ms 1772 KB Output is correct
4 Correct 2 ms 1772 KB Output is correct
5 Correct 2 ms 1772 KB Output is correct
6 Correct 2 ms 1772 KB Output is correct
7 Correct 2 ms 1772 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 1260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 13932 KB Output is correct
2 Correct 13 ms 13932 KB Output is correct
3 Correct 13 ms 13932 KB Output is correct
4 Correct 13 ms 13932 KB Output is correct
5 Correct 13 ms 13932 KB Output is correct
6 Correct 11 ms 10476 KB Output is correct
7 Correct 13 ms 12012 KB Output is correct
8 Correct 4 ms 1260 KB Output is correct
9 Correct 7 ms 4076 KB Output is correct
10 Correct 12 ms 12012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 40712 KB Output is correct
2 Correct 38 ms 40968 KB Output is correct
3 Correct 38 ms 40968 KB Output is correct
4 Correct 38 ms 40968 KB Output is correct
5 Correct 38 ms 40968 KB Output is correct
6 Correct 40 ms 36616 KB Output is correct
7 Correct 34 ms 34440 KB Output is correct
8 Correct 12 ms 2568 KB Output is correct
9 Correct 12 ms 2460 KB Output is correct
10 Correct 34 ms 33928 KB Output is correct
11 Correct 38 ms 40968 KB Output is correct
12 Correct 15 ms 5896 KB Output is correct