답안 #17521

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
17521 2015-12-25T12:50:48 Z gs14004 원형 문자열 (IZhO13_rowords) C++14
100 / 100
296 ms 46580 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
typedef complex<double> pnt;

string s1, s2;

int dp[4005][2005];
int nxt[4005][2005];
int n, m;

void reroot(int px){
	int py = 1;
	while(py <= m && nxt[px][py] != 2) py++;
	nxt[px][py] = 1;
	while(px < 2 * n && py < m){
		if(nxt[px+1][py] == 3){
			px++;
			nxt[px][py] = 1;
		}
		else if(nxt[px+1][py+1] == 2){
			px++;
			py++;
			nxt[px][py] = 1;
		}
		else py++;
	}
	while(px < 2 * n && nxt[px+1][py] == 3){
		px++;
		nxt[px][py] = 1;
	}
}

int track(int x, int y, int e){
	int ret = 0;
	while(y != 0 && x != e){
		if(nxt[x][y] == 1) y--;
		else if(nxt[x][y] == 2) ret += (s1[x] == s2[y]), x--, y--;
		else if(nxt[x][y] == 3) x--;
	}
	return ret;
}

int solve(string a, string b){
	s1 = a, s2 = b;
	n = s1.size(), m = s2.size();
	s1 = s1 + s1;
	s1 = '#' + s1;
	s2 = '#' + s2;
	for(int i=0; i<=2*n; i++){
		for(int j=0; j<=m; j++){
			if(j == 0){
				nxt[i][j] = 3;
				continue;
			}
			if(i == 0){
				nxt[i][j] = 1;
				continue;
			}
			dp[i][j] = -1;
			if(dp[i][j] < dp[i][j-1]){
				dp[i][j] = dp[i][j-1];
				nxt[i][j] = 1;
			}
			if(dp[i][j] < dp[i-1][j-1] + (s1[i] == s2[j])){
				dp[i][j] = dp[i-1][j-1] + (s1[i] == s2[j]);
				nxt[i][j] = 2;
			}
			if(dp[i][j] < dp[i-1][j]){
				dp[i][j] = dp[i-1][j];
				nxt[i][j] = 3;
			}
		}
	}
	int ret = dp[n][m];
	for(int i=1; i<n; i++){
		reroot(i);
		ret = max(ret, track(n+i, m, i));
	}
	return ret;
}

int main(){
	string a, b;
	cin >> a >> b;
	int ret = solve(a, b);
	reverse(b.begin(), b.end());
	ret = max(ret, solve(a, b));
	cout << ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 3 ms 1144 KB Output is correct
5 Correct 3 ms 1016 KB Output is correct
6 Correct 35 ms 17912 KB Output is correct
7 Correct 46 ms 31736 KB Output is correct
8 Correct 112 ms 31736 KB Output is correct
9 Correct 115 ms 31736 KB Output is correct
10 Correct 108 ms 31736 KB Output is correct
11 Correct 116 ms 34936 KB Output is correct
12 Correct 82 ms 38008 KB Output is correct
13 Correct 159 ms 38096 KB Output is correct
14 Correct 148 ms 36088 KB Output is correct
15 Correct 151 ms 39160 KB Output is correct
16 Correct 131 ms 34168 KB Output is correct
17 Correct 145 ms 36588 KB Output is correct
18 Correct 188 ms 44792 KB Output is correct
19 Correct 102 ms 31708 KB Output is correct
20 Correct 169 ms 40184 KB Output is correct
21 Correct 142 ms 31608 KB Output is correct
22 Correct 193 ms 36472 KB Output is correct
23 Correct 198 ms 39160 KB Output is correct
24 Correct 239 ms 41340 KB Output is correct
25 Correct 296 ms 46580 KB Output is correct