Submission #205626

# Submission time Handle Problem Language Result Execution time Memory
205626 2020-02-29T10:30:39 Z mihai50000 Palindromes (APIO14_palindrome) C++17
100 / 100
75 ms 40976 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

struct PalTree
{
	struct Node
	{
		map <char, int> urm;
		int len;
		int cnt;
		int fail;
	};
	
	int n = 2;
	int ans = 0;
	
	vector <Node> vec;
	
	PalTree(string s) : vec(s.size() + 2)
	{
		vec[0].fail = vec[0].len = -1;
		vec[1].fail = vec[1].len = 0;
		
		int last = 0;
		
		for(int i = 0; i < s.size(); ++i)
		{
			char ch = s[i];
			int pos = last;
			
			while(s[i - vec[pos].len - 1] != ch)
			{
				pos = vec[pos].fail;
			}
			if(vec[pos].urm.count(ch))
			{
				pos = vec[pos].urm[ch];
				vec[pos].cnt++;
				last = pos;
				continue;
			}
			
			vec[n].cnt = 1;
			vec[n].len = vec[pos].len + 2;
			vec[pos].urm[ch] = n;
			
			int fail = vec[pos].fail;
			
			while(fail != -1)
			{
				if(s[i - 1 - vec[fail].len] == ch && vec[fail].urm.count(ch))
				{
					fail = vec[fail].urm[ch];
					break;
				}
				
				fail = vec[fail].fail;
			}
			
			if(fail < 1)
				fail = 1;
			
			vec[n].fail = fail;
			last = n;
			n++;
		}
		
		for(int i = n - 1; i > 0; --i)
		{
			vec[vec[i].fail].cnt += vec[i].cnt;
			ans = max(ans, vec[i].cnt * vec[i].len);
		}
	}
};

main()
{
	string s;
	cin >> s;
	
	PalTree arb(s);
	cout << arb.ans << '\n';
}

Compilation message

palindrome.cpp: In constructor 'PalTree::PalTree(std::__cxx11::string)':
palindrome.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < s.size(); ++i)
                  ~~^~~~~~~~~~
palindrome.cpp: At global scope:
palindrome.cpp:79:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 4 ms 256 KB Output is correct
7 Correct 5 ms 256 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 5 ms 256 KB Output is correct
13 Correct 4 ms 256 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 5 ms 256 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
19 Correct 4 ms 256 KB Output is correct
20 Correct 5 ms 256 KB Output is correct
21 Correct 4 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 256 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 256 KB Output is correct
26 Correct 5 ms 256 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 5 ms 376 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 5 ms 256 KB Output is correct
32 Correct 5 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1656 KB Output is correct
2 Correct 7 ms 1656 KB Output is correct
3 Correct 7 ms 1656 KB Output is correct
4 Correct 6 ms 1656 KB Output is correct
5 Correct 7 ms 1656 KB Output is correct
6 Correct 7 ms 1656 KB Output is correct
7 Correct 7 ms 1656 KB Output is correct
8 Correct 6 ms 1144 KB Output is correct
9 Correct 6 ms 1016 KB Output is correct
10 Correct 6 ms 1400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 13816 KB Output is correct
2 Correct 26 ms 13816 KB Output is correct
3 Correct 28 ms 13920 KB Output is correct
4 Correct 26 ms 13816 KB Output is correct
5 Correct 26 ms 13816 KB Output is correct
6 Correct 23 ms 12152 KB Output is correct
7 Correct 25 ms 12792 KB Output is correct
8 Correct 21 ms 7672 KB Output is correct
9 Correct 25 ms 8952 KB Output is correct
10 Correct 25 ms 12792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 40852 KB Output is correct
2 Correct 67 ms 40852 KB Output is correct
3 Correct 65 ms 40852 KB Output is correct
4 Correct 67 ms 40976 KB Output is correct
5 Correct 66 ms 40852 KB Output is correct
6 Correct 64 ms 38672 KB Output is correct
7 Correct 64 ms 37652 KB Output is correct
8 Correct 49 ms 22164 KB Output is correct
9 Correct 52 ms 22164 KB Output is correct
10 Correct 75 ms 37396 KB Output is correct
11 Correct 69 ms 40852 KB Output is correct
12 Correct 51 ms 23772 KB Output is correct