제출 #1248083

#제출 시각아이디문제언어결과실행 시간메모리
1248083nhnguyen14Palindromic Partitions (CEOI17_palindromic)C++17
60 / 100
706 ms2532 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

template<class X,class Y>
	bool minimize(X &x,Y y){
		if (x>y) return x=y,true; return false;
	}
template<class X,class Y>
	bool maximize(X &x,Y y){
		if (x<y) return x=y,true; return false;
	}

const int N=(int)1e4+1;
const int inf = (int)1e9+7;
	int dp[N+2];
	int n;
		
int p[N+2]={} , h[N+2]= {};
const int base = 256;
const int MOD = 1e9+9;
	string s;
	
	int Get(int l,int r){
		return ((LL)h[r] - (LL)p[r-l+1] * h[l - 1] + (LL)MOD*MOD) % MOD;
	}
	
void solve(){
	cin>>s; s = '#' + s;
	n = s.size()  - 1;
	assert(n<=1e4);
	for(int i = 0 ; i <= n + 1; ++i) dp[i] = -inf;
	p[0] = 1;
	for(int i=1;i<=n;++i) {
		p[i] = (LL)p[i-1]*base % MOD;
		h[i] = ((LL)h[i-1]*base+s[i]) % MOD;
	}

	dp[0] = 0;
	int ans = 0;
	for(int start = 1; start <= n; ++start){
		for(int finish = start;finish<=n;++finish){
			int match_finish = n - start + 1 , match_start = n - finish + 1;		
			if (match_start == start) {
				maximize(ans , dp[start - 1] + 1);
			}
			if (match_start <= finish) continue;
			if (Get(start,finish)!=Get(match_start,match_finish)) continue;
			maximize(dp[finish] , dp[start-1] + 2);
			maximize(ans , dp[finish]);
		}	
	}
	cout<<ans<<'\n';
	return ;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0) ; cout.tie(0) ;
	#define name "main"
		if (fopen(name".inp","r")){
			freopen(name".inp","r",stdin);
			freopen(name".out","w",stdout);
		}
		
		int test; cin>>test;
		while(test--) solve();
		
		
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

palindromic.cpp: In function 'int main()':
palindromic.cpp:62:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |                         freopen(name".inp","r",stdin);
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:63:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |                         freopen(name".out","w",stdout);
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...