Submission #138030

# Submission time Handle Problem Language Result Execution time Memory
138030 2019-07-29T01:57:25 Z CaroLinda Palindromes (APIO14_palindrome) C++14
100 / 100
206 ms 104032 KB
#include <bits/stdc++.h>

#define debug 
#define lp(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,int>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair

const int MAXN = 6e5+1 ;
const int ALF= 35 ;

using namespace std ;

// ----------------------------------------------------

//Call Partial

map<string, ll> mapa ;
bool s[110][110] ;

void callPartial(char str[])
{

	int n = strlen(str) ;
	ll ans = 0 ;

	lp(i,0,n) s[i][i] = true ;
	lp(i,0,n-1) s[i][i+1] = ( str[i] == str[i+1] ) ;

	lp(i,2, n)
		for(int j = 0 ; j + i < n ; j++ )
			s[j][j+i] = ( s[j+1][j+i-1] && str[j] == str[j+i] ) ;


	lp( i , 0, n )
		lp(j,0,n)
			if( s[i][j] )
			{

				string aux ;
				aux.resize(j-i+1) ;

				lp(k,i,j+1)
					aux[k-i] = str[k] ;

				if( mapa.find(aux) == mapa.end() )
					mapa.insert( mk(aux , 0) ) ;

				ll tot = ++ mapa[aux] ;

				ans = max( ans , tot * (j-i+1) ) ;

			}


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

}


// ----------------------------------------------------

struct Trie
{

	int nodes ;
	int trie[MAXN][ALF] , lazy[MAXN] , parent[MAXN] ;
	ll ans ;

	Trie()
	{

		memset(trie, 0, sizeof trie) ;
		memset(lazy,0,sizeof lazy) ;
		parent[0] = 0 ;
		nodes = 0 ;
		ans = 0 ;

	}

	// The interval becomes [l,r]
	int add( char str[] , int l , int r , int address )
	{

		int cur = address ;

		for(int i = l ; i <= r ; i++ )
		{

			char c = str[i] - 'a' ;

			if( trie[cur][c] == 0 )
			{
				trie[cur][c] = ++ nodes ;
				parent[ nodes ] = cur ;
			}

			cur = trie[cur][c] ;

		}

		lazy[cur] ++ ;

		return cur ;

	}

	int goUp( int address, int qtd )
	{


		lp(i,0,qtd) address = parent[address] ;

		return address ;

	}


	ll dfs( int cur , int depth , bool what )
	{


		ll tempAns = lazy[cur] ;

		lp(i,0,30)
			if( trie[cur][i] != 0 )
				tempAns += dfs(trie[cur][i] , depth+1 , what ) ;

		ll multiplier = ( what ?  ( depth/2 ) * 2 : ( ( (depth-1)/2 )*2 + 1 ) ) ;

		ans = max( ans , tempAns * multiplier ) ;

		debug("%d -> %lld %lld\n" , cur , tempAns , multiplier ) ;

		return tempAns ;

	}


} ;

int n ;

int p[MAXN] , myAd[MAXN] ;

Trie trie ;

char str[MAXN] ;

// ----------------------------------------------------

void findAns()
{

	
	//If I want an odd palindrome
	lp(i,0,27)
		if( trie.trie[0][i] != 0 )
			trie.dfs( trie.trie[0][i] , 1 , 0 ) ;


	//If I want an even palindrome
	trie.dfs( trie.trie[0][27] , 1 , 1 ) ;


}

void manacher()
{

	int center = 0 , right = -1 ;

	lp(i,0,n)
	{

		int rad= 0 , last = 0 , ant = i ;

		if( i <= right )
		{
			rad = min( p[2*center-i] , right - i + 1 ) ;
			last = trie.goUp( myAd[2*center-i] , p[2*center-i] - rad ) ;
			ant = rad + i ;
		}


		while( i - rad >= 0 && i + rad < n && str[i-rad] == str[i+rad] )
			{ rad ++ ; }

		myAd[i] = trie.add(str, ant , i+rad-1 , last ) ;
		p[i] = rad ;

		debug("With center %d, we have a ratio of %d and the last I used was %d and it became %d\n\n" , i , rad  , last , myAd[i]) ;

		if( i+rad-1 > right )
		{
			center = i ;
			right = i+rad-1 ;
		}

	}


}

// ----------------------------------------------------


int main()
{

	char difStr[MAXN] ;

	scanf(" %s" , difStr ) ;

	if( strlen(difStr) <= 100 ) 
	{
		callPartial(difStr) ;
		return 0 ;
	}

	n = strlen(difStr)*2 - 1 ;

	lp(i,0,n)
	{

		if( i%2 == 0 ) str[i] = difStr[i/2] ;

		else str[i] = '|' ;

	}

	manacher() ;

	findAns() ;

	printf("%lld\n" , trie.ans ) ;

}

Compilation message

palindrome.cpp: In member function 'int Trie::add(char*, int, int, int)':
palindrome.cpp:95:19: warning: array subscript has type 'char' [-Wchar-subscripts]
    if( trie[cur][c] == 0 )
                   ^
palindrome.cpp:97:16: warning: array subscript has type 'char' [-Wchar-subscripts]
     trie[cur][c] = ++ nodes ;
                ^
palindrome.cpp:101:21: warning: array subscript has type 'char' [-Wchar-subscripts]
    cur = trie[cur][c] ;
                     ^
palindrome.cpp: In member function 'long long int Trie::dfs(int, int, bool)':
palindrome.cpp:136:31: warning: left operand of comma operator has no effect [-Wunused-value]
   debug("%d -> %lld %lld\n" , cur , tempAns , multiplier ) ;
                               ^~~
palindrome.cpp:136:37: warning: right operand of comma operator has no effect [-Wunused-value]
   debug("%d -> %lld %lld\n" , cur , tempAns , multiplier ) ;
                                     ^~~~~~~
palindrome.cpp:136:47: warning: right operand of comma operator has no effect [-Wunused-value]
   debug("%d -> %lld %lld\n" , cur , tempAns , multiplier ) ;
                                               ^~~~~~~~~~
palindrome.cpp: In function 'void manacher()':
palindrome.cpp:195:99: warning: left operand of comma operator has no effect [-Wunused-value]
   debug("With center %d, we have a ratio of %d and the last I used was %d and it became %d\n\n" , i , rad  , last , myAd[i]) ;
                                                                                                   ^
palindrome.cpp:195:103: warning: right operand of comma operator has no effect [-Wunused-value]
   debug("With center %d, we have a ratio of %d and the last I used was %d and it became %d\n\n" , i , rad  , last , myAd[i]) ;
                                                                                                       ^~~
palindrome.cpp:195:110: warning: right operand of comma operator has no effect [-Wunused-value]
   debug("With center %d, we have a ratio of %d and the last I used was %d and it became %d\n\n" , i , rad  , last , myAd[i]) ;
                                                                                                              ^~~~
palindrome.cpp:195:123: warning: right operand of comma operator has no effect [-Wunused-value]
   debug("With center %d, we have a ratio of %d and the last I used was %d and it became %d\n\n" , i , rad  , last , myAd[i]) ;
                                                                                                                           ^
palindrome.cpp: In function 'int main()':
palindrome.cpp:216:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf(" %s" , difStr ) ;
  ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 73 ms 84856 KB Output is correct
2 Correct 73 ms 85112 KB Output is correct
3 Correct 86 ms 84856 KB Output is correct
4 Correct 72 ms 84856 KB Output is correct
5 Correct 73 ms 84984 KB Output is correct
6 Correct 77 ms 84884 KB Output is correct
7 Correct 77 ms 84940 KB Output is correct
8 Correct 73 ms 84856 KB Output is correct
9 Correct 79 ms 85000 KB Output is correct
10 Correct 83 ms 84840 KB Output is correct
11 Correct 80 ms 84856 KB Output is correct
12 Correct 73 ms 84984 KB Output is correct
13 Correct 73 ms 84968 KB Output is correct
14 Correct 84 ms 85076 KB Output is correct
15 Correct 76 ms 84956 KB Output is correct
16 Correct 79 ms 84984 KB Output is correct
17 Correct 85 ms 84856 KB Output is correct
18 Correct 79 ms 84892 KB Output is correct
19 Correct 73 ms 84956 KB Output is correct
20 Correct 75 ms 84856 KB Output is correct
21 Correct 73 ms 84956 KB Output is correct
22 Correct 74 ms 84984 KB Output is correct
23 Correct 74 ms 84984 KB Output is correct
24 Correct 74 ms 84984 KB Output is correct
25 Correct 75 ms 84940 KB Output is correct
26 Correct 73 ms 84984 KB Output is correct
27 Correct 74 ms 84956 KB Output is correct
28 Correct 74 ms 84892 KB Output is correct
29 Correct 74 ms 84932 KB Output is correct
30 Correct 73 ms 84984 KB Output is correct
31 Correct 73 ms 84984 KB Output is correct
32 Correct 77 ms 84884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 84944 KB Output is correct
2 Correct 88 ms 85112 KB Output is correct
3 Correct 78 ms 85112 KB Output is correct
4 Correct 79 ms 84956 KB Output is correct
5 Correct 74 ms 84984 KB Output is correct
6 Correct 75 ms 85068 KB Output is correct
7 Correct 73 ms 84932 KB Output is correct
8 Correct 73 ms 84984 KB Output is correct
9 Correct 75 ms 84984 KB Output is correct
10 Correct 86 ms 84856 KB Output is correct
11 Correct 74 ms 84984 KB Output is correct
12 Correct 74 ms 84984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 85496 KB Output is correct
2 Correct 77 ms 85424 KB Output is correct
3 Correct 77 ms 85496 KB Output is correct
4 Correct 88 ms 85496 KB Output is correct
5 Correct 75 ms 85496 KB Output is correct
6 Correct 75 ms 85368 KB Output is correct
7 Correct 76 ms 85340 KB Output is correct
8 Correct 74 ms 85056 KB Output is correct
9 Correct 74 ms 85124 KB Output is correct
10 Correct 88 ms 85316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 91232 KB Output is correct
2 Correct 108 ms 89652 KB Output is correct
3 Correct 109 ms 91256 KB Output is correct
4 Correct 109 ms 90048 KB Output is correct
5 Correct 103 ms 90316 KB Output is correct
6 Correct 114 ms 88568 KB Output is correct
7 Correct 119 ms 88184 KB Output is correct
8 Correct 78 ms 86904 KB Output is correct
9 Correct 98 ms 87852 KB Output is correct
10 Correct 103 ms 89948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 104032 KB Output is correct
2 Correct 176 ms 96068 KB Output is correct
3 Correct 184 ms 103928 KB Output is correct
4 Correct 174 ms 96888 KB Output is correct
5 Correct 175 ms 102500 KB Output is correct
6 Correct 161 ms 95256 KB Output is correct
7 Correct 166 ms 94892 KB Output is correct
8 Correct 99 ms 90580 KB Output is correct
9 Correct 95 ms 90656 KB Output is correct
10 Correct 156 ms 102112 KB Output is correct
11 Correct 206 ms 104028 KB Output is correct
12 Correct 103 ms 91768 KB Output is correct