답안 #493451

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
493451 2021-12-11T11:50:27 Z Bogdan1110 회문 (APIO14_palindrome) C++17
100 / 100
65 ms 62316 KB
#include <bits/stdc++.h>
using namespace std;

string s;

struct PalTree {
	struct Node {
		int cnt;
		int len;
		int dub;
		int slink;
		int to[26];
		Node() { 
			len = dub = cnt = slink = 0; 
			for (int i = 0; i < 26 ; i++ ) to[i] = 0;
		}
		Node( int _len, int _dub, int _cnt, int _slink) {
			len = _len;
			dub = _dub;
			cnt = _cnt;
			slink = _slink;
			for (int i = 0 ; i < 26 ; i++ ) to[i] = 0;
		}
	};
	vector<Node>vec;
	int suff = 2;
	
	void AddNode( int i ) {
		while(i - 1 - vec[suff].len < 0 ||  s[i]!= s[i-vec[suff].len-1] ) {
			suff = vec[suff].slink;
		}
		//cout << i << ' ' << suff << "    ";
		int gde=  s[i]-'a';
		int cvor = vec[suff].to[gde];
		if ( vec[suff].to[gde] == 0 ) {
			vec[suff].to[gde] = vec.size();
			cvor = vec.size();
			vec.push_back(Node(vec[suff].len+2, vec[suff].dub+1, 0, suff));
			if ( suff == 1 ) {
				vec[cvor].slink = 2;
			}
			else {
				suff = vec[suff].slink;
				while(i - 1 - vec[suff].len < 0 ||  s[i- vec[suff].len -1] != s[i] ) {
					suff = vec[suff].slink;
				}
				vec[cvor].slink = vec[suff].to[gde];
			}
			vec[cvor].dub = vec[vec[cvor].slink].dub + 1;
		}
		suff = cvor;
		vec[cvor].cnt++;
		//cout << i <<  ' ' << vec[suff].len <<  ' ' << vec[suff].slink << ' ' << suff << endl;
	} 
	
	void Init() {
		vec.clear();
		suff = 2;
		vec.push_back(Node(0,0,0,0));
		vec.push_back(Node(-1,0,0,1));
		vec.push_back(Node(0,0,0,1));
	}
	
	void CalcCnt() {
		for (int i = vec.size()-1 ; i >= 2  ; i-- ) {
			vec[vec[i].slink].cnt += vec[i].cnt;
		}
	}
	
};
PalTree pt;
int main () {
	cin >> s;
	int n = s.length();
	pt.Init();
	for ( int i = 0 ; i < n ; i++ ) {
		pt.AddNode(i);
	}
	pt.CalcCnt();
	long long rez = 0;
	for (int i = 0 ; i < pt.vec.size(); i++ ) {
		rez = max(rez, (long long)pt.vec[i].len * pt.vec[i].cnt);
		//cout << pt.vec[i].len << ' ' << pt.vec[i].cnt << endl ;
	}
	cout << rez << endl ;
}



















Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:81:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |  for (int i = 0 ; i < pt.vec.size(); i++ ) {
      |                   ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2316 KB Output is correct
2 Correct 2 ms 2316 KB Output is correct
3 Correct 2 ms 2316 KB Output is correct
4 Correct 2 ms 2316 KB Output is correct
5 Correct 2 ms 2316 KB Output is correct
6 Correct 2 ms 2316 KB Output is correct
7 Correct 2 ms 2316 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 1392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 15988 KB Output is correct
2 Correct 17 ms 15988 KB Output is correct
3 Correct 16 ms 15936 KB Output is correct
4 Correct 19 ms 16008 KB Output is correct
5 Correct 18 ms 16000 KB Output is correct
6 Correct 15 ms 16004 KB Output is correct
7 Correct 16 ms 15992 KB Output is correct
8 Correct 5 ms 588 KB Output is correct
9 Correct 8 ms 4476 KB Output is correct
10 Correct 15 ms 15988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 62188 KB Output is correct
2 Correct 63 ms 62200 KB Output is correct
3 Correct 58 ms 62196 KB Output is correct
4 Correct 63 ms 62280 KB Output is correct
5 Correct 61 ms 62316 KB Output is correct
6 Correct 65 ms 62240 KB Output is correct
7 Correct 44 ms 31448 KB Output is correct
8 Correct 13 ms 980 KB Output is correct
9 Correct 13 ms 1016 KB Output is correct
10 Correct 43 ms 31448 KB Output is correct
11 Correct 60 ms 62172 KB Output is correct
12 Correct 17 ms 4584 KB Output is correct