Problem:
Problem G:
Repeated Subsequences
You are a treasure hunter traveling around the world. Finally, you’ve got an ancient text
indicating the place where the treasure was hidden. The ancient text looks like a meaningless
string of characters at first glance. Actually, the secret place is embedded as the
longest repeated
subsequence
of the text.
Well, then, what is the
longest repeated subsequence
of a string? First, you split the given string
S
into two parts
F
and
R
. Then, you take the longest common subsequence
L
of
F
and
R
(longest string
L
that is a subsequence of both
F
and
R
). Since there are many possible ways
to split
S
into two parts, there are many possible
L
's. The longest repeated subsequence is the
longest one among them. For example, the longest repeated subsequence of “ABCABCABAB”
is “ABAB”, which is obtained when you split “ABCABCABAB” into “ABCABC” and “ABAB”.
Now your task is clear. Please find the longest repeated subsequence and get the hidden treasure!
Input
The input consists of multiple data sets. Each data set comes with a single line that contains
one string of up to 300 capital letters. It is guaranteed that there is at least one repeated
subsequence in each string.
The end of input is indicated by a line that contains “#END”. This line should not be processed.
Output
For each data set, print the longest repeated subsequence on a line. If there are multiple longest
subsequence, print any one of them.
Sample Input
ABCABCABAB
ZZZZZZZZZZZZ
#END
Output for the Sample Input
ABAB
ZZZZZZ

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int dp[1010][1010];

string get(string a, string b)
{
    memset(dp, 0, sizeof dp);

    int n = a.size(), m = b.size();
    a = '#' + a;
    b = '#' + b;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            if (a[i] == b[j])
                dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
            else
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    int p1 = n, p2 = m;
    // cout << a << ' ' << b << endl;
    // cout << dp[n][m] << endl;
    string res = "";
    while (1)
    {
        // cout << 2 << ' ' << p1 << ' ' << p2 << ' ' << res << ' ' << dp[n - 1][m - 1] << endl;

        if (a[p1] == b[p2])
        {
            res += a[p1];
            --p1, --p2;
        }
        else if (dp[p1 - 1][p2] == dp[p1][p2])
            --p1;
        else
            --p2;
        if (res.size() == dp[n][m])
            break;
    }
    reverse(res.begin(), res.end());
    return res;
}
string f(string a)
{
    string res = "";
    for (int i = 0; i < a.size() - 1; i++)
    {
        // cout << '\t' << i << endl;
        string f1 = a.substr(0, i + 1);
        string f2 = a.substr(i + 1);
        if (get(f1, f2).size() > res.size())
            res = get(f1, f2);
    }
    return res;
}
int main()
{
    string a;
    while (cin >> a)
    {
        if (a[0] == '#')
            break;
        string ans = f(a);
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 5520
memory_kb: 7372
final_rank: 18

code_snippet: #include <cstdio>
#include <cstring>
#include <algorithm>
using std::max;

int LCS(char* s1, char* s2,char *res)
{
    int i,j,M,N,k=0,upbd;
    M=strlen(s1);
    N=strlen(s2);
    int **dp;
    dp=new int* [M+2];
    for(i=0;i<M+2;i++)
        dp[i]=new int [N+2];
    for(i=0;i<N+2;i++)
        dp[0][i]=0;
    
    for(i=1;i<M+1;i++)
    {
        dp[i][0]=0;
        for(j=1;j<N+1;j++)
        {
            if(s1[i-1]==s2[j-1])
                dp[i][j]=dp[i-1][j-1]+1;
            else
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
    }
    k=dp[M][N];

    i=M;
    j=N;
    upbd=k;
    res[k]='\0';
    while(i>0 && j>0)
    {
        if(dp[i-1][j]==dp[i-1][j-1] && dp[i-1][j-1]==dp[i][j-1] )
        {
            if (dp[i][j]==dp[i-1][j-1]+1)
                res[--upbd]=s1[i-1];
            i--;
            j--;
        }
        else
        {
            if(dp[i-1][j]==dp[i][j])
                i--;
            else
                j--;
        }
    }
    for(i=0;i<M+2;i++)
        delete [] dp[i];
    delete [] dp;

    return k;
}

void solve(char *s)
{
    char *s1,*s2,*res,*target;
    int N=strlen(s),i,d,maxVal=-1;
    s1=new char [N+1];
    s2=new char [N+1];
    res=new char [N+1];
    target=new char [N+1];
    for(i=1;i<N;i++)
    {
        strcpy(s1,s);
        s1[i]=0;
        strcpy(s2,s+i);

        d=LCS(s1,s2,res);
        if(d>maxVal)
        {
            maxVal=d;
            strcpy(target,res);
        }
    }
    printf("%s\n",target);
    delete [] s1;
    delete [] s2;
    delete [] res;
    delete [] target;
}

#define MAXLENGTH 303
int main(void)
{
    char s[MAXLENGTH];
    while(scanf("%s",s)!=EOF)
    {
        if(strcmp("#END",s)==0)
            break;
        solve(s);

    }

    return 0;
}
accuracy: 1
time_ms: 970
memory_kb: 3188
final_rank: 3

code_snippet: #include <bits/stdc++.h>

std::string lcm(std::string &a, std::string &b) {
    int n = a.size(), m = b.size();
    std::vector<std::vector<int> > dp(n, std::vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i] == b[j]) {
                dp[i][j] = (i > 0 && j > 0 ? dp[i - 1][j - 1] : 0) + 1;
            } else {
                dp[i][j] = std::max(i > 0 ? dp[i - 1][j] : 0, j > 0 ? dp[i][j - 1] : 0);
            }
        }
    }
    std::string ans;
    int i = n - 1, j = m - 1;
    while (i > -1 || j > -1) {
        if (i > -1 && j > -1 && (i > 0 && j > 0 ? dp[i - 1][j - 1] : 0) + 1 == dp[i][j] && a[i] == b[j]) {
            ans += a[i];
            i--, j--;
        } else if (i > -1 && (i > 0 ? dp[i - 1][j] : 0) == dp[i][j]) {
            i--;
        } else {
            j--;
        }
    }
    std::reverse(ans.begin(), ans.end());
    return ans;
}

signed main() {
    std::string s;
    while (std::cin >> s) {
        if (s == "#END") {
            return 0;
        }
        std::string a = s.substr(0, 1), b = s.substr(1, s.size() - 1);
        std::string ans = lcm(a, b);
        for (int i = 1; i < s.size() - 1; i++) {
            a += s[i];
            b.erase(b.begin());
            std::string cur = lcm(a, b);
            if ((int) cur.size() > (int) ans.size()) {
                ans = cur;
            }
        }
        std::cout << ans << "\n";
    }
}
accuracy: 1
time_ms: 1750
memory_kb: 3184
final_rank: 6

code_snippet: #include<iostream>
#include<cstring>
using namespace std;
int dp[310][310];
string dpstr[310][310];
int main()
{
    string str;
    while(cin>>str&&str!="#END")
    {
        int maxstr=0,flag;
        for(int k=1;k<str.length();k++)
        {
            string str1=str.substr(0,k);
            string str2=str.substr(k);
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=str1.length();i++)
            {
                for(int j=1;j<=str2.length();j++)
                {
                    if(str1[i-1]==str2[j-1])
                        dp[i][j]=dp[i-1][j-1]+1;
                    else
                        dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
                }
            }
            if(maxstr<dp[str1.length()][str2.length()])
            {
                maxstr=dp[str1.length()][str2.length()];
                flag=k;
            }
        }
        string str1=str.substr(0,flag);
        string str2=str.substr(flag);
        for(int i=1;i<=str1.length();i++)
        {
            for(int j=1;j<=str2.length();j++)
            {
                if(str1[i-1]==str2[j-1])
                    dpstr[i][j]=dpstr[i-1][j-1]+str1[i-1];
                else
                {
                    if (dpstr[i-1][j].size() <= dpstr[i][j-1].size())
                        dpstr[i][j] = dpstr[i][j-1];
					if (dpstr[i-1][j].size() > dpstr[i][j-1].size())
                        dpstr[i][j] = dpstr[i-1][j];
                }
            }
        }
        cout<<dpstr[str1.size()][str2.size()]<<endl;
        /*for(int i=1;i<str.length();i++)
        {
            for(int j=1;j<str.length();j++)
                cout<<dp[i][j]<<' ';
            cout<<endl;
        }*/
    }
	return 0;
}
accuracy: 1
time_ms: 1780
memory_kb: 6856
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int dp[301][301];
int maxn, maxm, maxlen;
string str;

void lcs(int n, int m, bool rest = true) {
    for (int i = 0; i < n; i++) fill(dp[i], dp[i]+m+1, 0);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (str[i] == str[n+j]) {
                dp[i+1][j+1] = dp[i][j]+1;
            }
            else {
                dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
            }
        }
    }
    if (rest && dp[n][m] > maxlen) {
        maxlen = dp[n][m];
        maxn = n, maxm = m;
    }
}

void getSubseq() {
    char seq[maxlen+1];
    seq[maxlen] = '\0';
    string x = str.substr(0, maxn);
    string y = str.substr(maxn);
    lcs(maxn, maxm, false);
    int i = maxn, j = maxm;
    int index = maxlen;

    while (i > 0 && j > 0) {
        if (x[i-1] == y[j-1]) {
            seq[index-1] = x[i-1];
            i--; j--; index--;
        }
        else if (dp[i-1][j] > dp[i][j-1]) i--;
        else j--;
    }
    cout << seq << endl;
}



int main()
{
    for (; ;) {
        cin >> str;
        if (str == "#END") break;
        maxlen = 0;
        int size = str.size();
        for (int i = 1; i < str.size(); i++) {
            lcs(i, size-i);
        }
        getSubseq();
    }
    return 0;
}
accuracy: 1
time_ms: 1270
memory_kb: 3456
final_rank: 5

code_snippet: #include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<utility>

using namespace std;


const int MAXL = 301;


pair<string, int> getLCS(string s1, string s2) {
  int L1 = (int) s1.size(), L2 = (int) s2.size();
  s1 = " " + s1;
  s2 = " " + s2;
  vector<vector<int> > D(s1.size(), vector<int>(s2.size(), 0));
  vector<vector<string> > common_subs(s1.size(), vector<string>(s2.size(), ""));

  for (size_t i = 1; i < s1.size(); ++i) {
    for (size_t j = 1; j < s2.size(); ++j) {
      if (s1[i] == s2[j]) {
        D[i][j] = D[i-1][j-1] + 1;
        common_subs[i][j] = common_subs[i-1][j-1] + s1[i];
      }
      else {
        D[i][j] = max(D[i-1][j], D[i][j-1]);
        if (D[i-1][j] > D[i][j-1]) common_subs[i][j] = common_subs[i-1][j];
        else common_subs[i][j] = common_subs[i][j-1];
      }
    }
  }

  return make_pair(common_subs[L1][L2], D[L1][L2]);
}


void solve(string s) {
  int len = (int) s.size();
  
  string ans = "";
  int max_l = 0;
  for (int k = 1; k < len; ++k) {
    string sub1 = s.substr(0, k);
    string sub2 = s.substr(k, len-k);

    if ((int) min(sub1.size(), sub2.size()) <= max_l) continue;

    pair<string, int> res;
    res = getLCS(sub1, sub2);
    if (res.second > max_l) {
      ans = res.first;
      max_l = res.second;
    }
  }

  printf("%s\n", ans.c_str());
}


int main() {
  char s[MAXL];

  while (scanf("%s", s) != EOF) {
    if (strcmp(s, "#END") == 0) break;

    solve(s);
  }
  return 0;
}
accuracy: 1
time_ms: 6870
memory_kb: 5364
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

string dp2[310][310];
int dp[310][310];

int main()
{
	string s;
	while (cin >> s){
		if (s == "#END")return 0;
		int res = 0, id = 0;
		for (int k = 1; k < s.length(); ++k){
			string s1 = s.substr(0, k);//从头到k的子序列
			string s2 = s.substr(k);//从k往后的子序列
			memset(dp, 0, sizeof dp);
			for (int i = 0; i < s1.length(); ++i){
				for (int j = 0; j < s2.length(); ++j){
					if (s1[i] == s2[j])dp[i + 1][j + 1] = dp[i][j] + 1;
					else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
				}
			}
			if (res < dp[s1.length()][s2.length()]){
				res = dp[s1.length()][s2.length()];
				id = k;
			}
		}
		//从id处分隔字符串
		string s1 = s.substr(0, id);
		string s2 = s.substr(id);
		for (int i = 0; i <= s1.length(); ++i){
			for (int j = 0; j <= s2.length(); ++j)dp2[i][j] = "";
		}
		for (int i = 0; i < s1.length(); ++i){
			for (int j = 0; j < s2.length(); ++j){
				if (s1[i] == s2[j])dp2[i + 1][j + 1] = dp2[i][j] + s1[i];
				else{
					if (dp2[i][j + 1].size() <= dp2[i + 1][j].size())dp2[i + 1][j + 1] = dp2[i + 1][j];
					if (dp2[i][j + 1].size() > dp2[i + 1][j].size())dp2[i + 1][j + 1] = dp2[i][j + 1];
				}
			}
		}
		cout << dp2[s1.size()][s2.size()] << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 1960
memory_kb: 6700
final_rank: 16

code_snippet: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int dp[300][300];
string ans;

int getLCSlen(string a, string b){
    memset(dp, 0, sizeof(dp));
    int ret = 0;

    for(int j = 0; j < b.length(); j++) if(a[0] == b[j])    dp[0][j] = 1, ret = 1;
    for(int i = 0; i < a.length(); i++) if(a[i] == b[0])    dp[i][0] = 1, ret = 1;
    
    // add these if you want to get longest common subsequence
    for(int i = 1; i < a.length(); i++) dp[i][0] = max(dp[i][0], dp[i-1][0]);
    for(int j = 1; j < b.length(); j++) dp[0][j] = max(dp[0][j], dp[0][j-1]);

    for(int i = 1; i < a.length(); i++){
        for(int j = 1; j < b.length(); j++){
            if(a[i] == b[j]){
                dp[i][j] = dp[i-1][j-1]+1;
                ret = max(ret, dp[i][j]);
            }
            dp[i][j] = max(dp[i][j], max(dp[i-1][j], dp[i][j-1]));
            ret = max(ret, dp[i][j]);
        }
    }
    return ret;
}

void solve(string s){
    ans = "";
    for(int x = 1; x < s.length(); x++){
        int len = getLCSlen(s.substr(0,x), s.substr(x));
        if(len > ans.length()){
            string tmp = "";
            int i = x-1, j = s.length()-x-1;
            while(i >= 0 && j >= 0 && dp[i][j]>0){
                while(i > 0 && dp[i-1][j]==dp[i][j])    i--;
                while(j > 0 && dp[i][j-1]==dp[i][j])    j--;
                tmp += s[i];
                i--, j--;
            }
            ans = tmp;
        }
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
}

int main(){
    string s;
    while(cin >> s, s != "#END"){
        solve(s);
    }
    return 0;
}
accuracy: 1
time_ms: 2200
memory_kb: 3380
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pi = pair<int,int>;
const int INF = 101010;

string LCS(const string &x, const string &y){
    int X = x.size(), Y = y.size();

    vector<vector<int>> dp(X+1, vector<int>(Y+1,-INF));
    vector<vector<pi>> par(X+1, vector<pi>(Y+1));

    dp[0][0] = 0;
    rep(i,X+1)rep(j,Y+1){
        if(i<X && j<Y && x[i]==y[j]){
            if(dp[i+1][j+1] < dp[i][j]+1){
                dp[i+1][j+1] = dp[i][j]+1;
                par[i+1][j+1] = {-1,-1};
            }
        }
        if(i<X){
            if(dp[i+1][j] < dp[i][j]){
                dp[i+1][j] = dp[i][j];
                par[i+1][j] = {-1,0};
            }
        }
        if(j<Y){
            if(dp[i][j+1] < dp[i][j]){
                dp[i][j+1] = dp[i][j];
                par[i][j+1] = {0,-1};
            }
        }
    }

    string ret = "";
    int px = X, py = Y;
    while(px!=0 || py!=0){
        pi p = par[px][py];
        if(p == pi(-1,-1)) ret += x[px-1];
        px += p.fi;
        py += p.se;
    }
    reverse(all(ret));
    return ret;
}

int main(){
    string s;
    while(cin >>s,(s!="#END")){
        int n = s.size();

        string ans = "";
        for(int i=1; i<n; ++i){
            string F = s.substr(0,i), R = s.substr(i);
            string t = LCS(F,R);
            if(t.size() > ans.size()) ans = t;
        }

        cout << ans << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 3680
memory_kb: 3328
final_rank: 14

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

char buf[301],right_buf[301],ans[301];
int dp[301][301],memo[301][301];

void func(){

	int length,right_length;
	for(length = 0; buf[length] != '\0'; length++){
		right_buf[length] = buf[length];
	}

	int ans_length = 0,right_start;

	for(int left_length = 1; left_length <= length-1; left_length++){

		right_length = length - left_length;
		right_start = left_length;

		if(left_length <= ans_length || right_length <= ans_length)continue;

		for(int a = 0; a <= max(right_length,left_length); a++){
			for(int b = 0; b <= max(right_length,left_length); b++)dp[a][b] = 0;
		}

		for(int i = 0; i < left_length; i++){
			for(int k = 0; k < right_length; k++){
				if(buf[i] == right_buf[right_start+k]){
					dp[i+1][k+1] = dp[i][k]+1;
					memo[i+1][k+1] = 0;
				}else{
					if(dp[i][k+1] >= dp[i+1][k]){
						dp[i+1][k+1] = dp[i][k+1];
						memo[i+1][k+1] = 1;
					}else{
						dp[i+1][k+1] = dp[i+1][k];
						memo[i+1][k+1] = -1;
					}
				}
			}
		}


		if(ans_length < dp[left_length][right_length]){
			ans_length = dp[left_length][right_length];

			stack<char> S;

			for(int i = left_length,k = right_length; i > 0 && k > 0;){

				if(memo[i][k] == 1){
					i--;
				}else if(memo[i][k] == -1){
					k--;
				}else{ //memo[i][k] == 0
					S.push(buf[i-1]);
					i--;
					k--;
				}
			}

			int index = 0;
			while(!S.empty()){
				ans[index++] = S.top();
				S.pop();
			}
		}
	}

	for(int i = 0; i < ans_length; i++)printf("%c",ans[i]);
	printf("\n");
}

int main(){

	while(true){
		scanf("%s",buf);
		if(buf[0] == '#')break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 3736
final_rank: 4

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

char buf[301],left_buf[301],right_buf[301],ans[301];
int dp[301][301],memo[301][301];

void func(){

	int length,right_length;
	for(length = 0; buf[length] != '\0'; length++){
		left_buf[length] = buf[length];
		right_buf[length] = buf[length];
	}

	int ans_length = 0,right_start;

	for(int left_length = 1; left_length <= length-1; left_length++){

		right_length = length - left_length;
		right_start = left_length;

		for(int a = 0; a <= max(right_length,left_length); a++){
			for(int b = 0; b <= max(right_length,left_length); b++)dp[a][b] = 0;
		}

		for(int i = 0; i < left_length; i++){
			for(int k = 0; k < right_length; k++){
				if(left_buf[i] == right_buf[right_start+k]){
					dp[i+1][k+1] = dp[i][k]+1;
					memo[i+1][k+1] = 0;
				}else{
					if(dp[i][k+1] >= dp[i+1][k]){
						dp[i+1][k+1] = dp[i][k+1];
						memo[i+1][k+1] = 1;
					}else{
						dp[i+1][k+1] = dp[i+1][k];
						memo[i+1][k+1] = -1;
					}
				}
			}
		}


		if(ans_length < dp[left_length][right_length]){
			ans_length = dp[left_length][right_length];

			stack<char> S;

			for(int i = left_length,k = right_length; i > 0 && k > 0;){

				if(memo[i][k] == 1){
					i--;
				}else if(memo[i][k] == -1){
					k--;
				}else{ //memo[i][k] == 0
					S.push(left_buf[i-1]);
					i--;
					k--;
				}
			}

			int index = 0;
			while(!S.empty()){
				ans[index++] = S.top();
				S.pop();
			}
		}
	}

	for(int i = 0; i < ans_length; i++)printf("%c",ans[i]);
	printf("\n");
}

int main(){

	while(true){
		scanf("%s",buf);
		if(buf[0] == '#')break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 1410
memory_kb: 3832
final_rank: 7

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

char buf[301],left_buf[301],right_buf[301],ans[301];
int dp[301][301],memo[301][301];

void func(){

	int length,right_length;
	for(length = 0; buf[length] != '\0'; length++);

	int ans_length = 0;

	for(int left_length = 1; left_length <= length-1; left_length++){
		left_buf[left_length-1] = buf[left_length-1];
		right_length = length - left_length;
		for(int i = 0; left_length+i < length; i++){
			right_buf[i] = buf[left_length+i];
		}

		for(int a = 0; a <= length; a++){
			for(int b = 0; b <= length; b++)dp[a][b] = 0;
		}

		for(int i = 0; i < left_length; i++){
			for(int k = 0; k < right_length; k++){
				if(left_buf[i] == right_buf[k]){
					dp[i+1][k+1] = dp[i][k]+1;
					memo[i+1][k+1] = 0;
				}else{
					if(dp[i][k+1] >= dp[i+1][k]){
						dp[i+1][k+1] = dp[i][k+1];
						memo[i+1][k+1] = 1;
					}else{
						dp[i+1][k+1] = dp[i+1][k];
						memo[i+1][k+1] = -1;
					}
				}
			}
		}


		if(ans_length < dp[left_length][right_length]){
			ans_length = dp[left_length][right_length];

			stack<char> S;

			for(int i = left_length,k = right_length; i > 0 && k > 0;){

				if(memo[i][k] == 1){
					i--;
				}else if(memo[i][k] == -1){
					k--;
				}else{ //memo[i][k] == 0
					S.push(left_buf[i-1]);
					i--;
					k--;
				}
			}

			int index = 0;
			while(!S.empty()){
				ans[index++] = S.top();
				S.pop();
			}
		}
	}

	for(int i = 0; i < ans_length; i++)printf("%c",ans[i]);
	printf("\n");
}

int main(){

	while(true){
		scanf("%s",buf);
		if(buf[0] == '#')break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 1810
memory_kb: 3784
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int dp[301][301],f[301][301];
string ch(string s,string t){
    memset(dp,0,sizeof(dp));
    memset(f,0,sizeof(f));
    s="@"+s,t="@"+t;
    int m=s.size(),n=t.size();
    for(int i=1;i<m;i++)
     for(int j=1;j<n;j++){
      if(s[i]==t[j]){
        dp[i][j]=dp[i-1][j-1]+1;
        f[i][j]=3;
      }
      else{
        if(dp[i-1][j]<dp[i][j-1]){
          dp[i][j]=dp[i][j-1];
          f[i][j]=2;
        }
        else{
          dp[i][j]=dp[i-1][j];
          f[i][j]=1;
        }
      }
    }
    int idx=dp[m-1][n-1];
    string ans;
    for(int i=m-1,j=n-1;i>0&&j>0&&idx;){
      if(f[i][j]==1)i--;
      else if(f[i][j]==2)j--;
      else i--,j--,idx--,ans+=s[i+1];
    }
    reverse(ans.begin(),ans.end());
    return ans;
}
main(){
  string s;
  while(cin>>s,s!="#END"){
    string ans;
    for(int i=1;i<s.size();i++){
      string s1=s.substr(0,i);
      string s2=s.substr(i);
      string s3=ch(s1,s2);
      if(ans.size()<s3.size())ans=s3;
    }
    cout<<ans<<endl;
  }
}
accuracy: 1
time_ms: 2290
memory_kb: 3920
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

const int Max = 303;

int main() {

  for(string s_; cin >> s_ && s_ != "#END";) {
    const int N = s_.size();

    string max_string;
    int max_length = 0;

    REP(k, 1, N) {
      const int sn = k;
      const int tn = N - k;
      string s = string(s_.begin(), s_.begin() + sn);
      string t = string(s_.begin() + sn, s_.end());
      int dp[Max][Max];
      int trans[Max][Max];

      zero(dp);
      minus(trans);

      rep(i, sn) {
        rep(j, tn) {
          if(s[i] == t[j]) {
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
            trans[i + 1][j + 1] = 0;
          }
          else if(dp[i + 1][j] > dp[i][j + 1]) {
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
            trans[i + 1][j + 1] = 1;
          }
          else {
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
            trans[i + 1][j + 1] = 2;
          }
        }
      }

      if(dp[sn][tn] > 0) {
        int i = sn, j = tn;
        string res;
        while(trans[i][j] >= 0) {
          if(trans[i][j] == 2) i--;
          else if(trans[i][j] == 1) j--;
          else {
            i--, j--;
            res.push_back(s[i]);
          }
        }

        if(max_length < res.size()) {
          reverse(res.begin(), res.end());
          max_length = res.size();
          max_string = res;
        }
      }
    }

    cout << max_string << endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 2730
memory_kb: 3884
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

const int Max = 303;

int main() {

  for(string s_; cin >> s_ && s_ != "#END";) {
    const int N = s_.size();

    string max_string;
    int max_length = 0;

    REP(k, 1, N) {
      const int sn = k;
      const int tn = N - k;
      string s = string(s_.begin(), s_.begin() + sn);
      string t = string(s_.begin() + sn, s_.end());
      int dp[Max][Max];
      int trans[Max][Max];

      zero(dp);
      minus(trans);

      rep(i, sn) {
        rep(j, tn) {
          if(s[i] == t[j]) {
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
            trans[i + 1][j + 1] = 2;
          }
          else if(dp[i + 1][j] > dp[i][j + 1]) {
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
            trans[i + 1][j + 1] = 0;
          }
          else {
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
            trans[i + 1][j + 1] = 1;
          }
        }
      }

      if(dp[sn][tn] > 0) {
        int i = sn, j = tn;
        string res;
        while(trans[i][j] >= 0) {
          if(trans[i][j] == 1) i--;
          else if(trans[i][j] == 0) j--;
          else {
            i--, j--;
            res.push_back(s[i]);
          }
        }

        if(max_length < res.size()) {
          reverse(res.begin(), res.end());
          max_length = res.size();
          max_string = res;
        }
      }
    }

    cout << max_string << endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 2720
memory_kb: 3892
final_rank: 12

code_snippet: #include<iostream>
#include<string>
using namespace std;
int n; string S, dp[320][320];
int dp2[320][320];
inline string LCS(string &a1, string &a2) {
	for (int i = 0; i <= a1.size(); i++) {
		for (int j = 0; j <= a2.size(); j++)dp2[i][j] = -1;
	}
	dp2[0][0] = 0;
	for (int i = 0; i <= a1.size(); i++)dp[i][0] = "";
	for (int i = 0; i <= a2.size(); i++)dp[0][i] = "";
	for (int i = 0; i <= a1.size(); i++) {
		for (int j = 0; j <= a2.size(); j++) {
			if (i < a1.size() && j < a2.size() && a1[i] == a2[j]) {
				if (dp2[i + 1][j + 1] < dp2[i][j] + 1) {
					dp[i + 1][j + 1] = dp[i][j] + a1[i];
					dp2[i + 1][j + 1] = dp2[i][j] + 1;
				}
			}
			if (dp2[i + 1][j] < dp2[i][j]) { dp[i + 1][j] = dp[i][j]; dp2[i + 1][j] = dp2[i][j]; }
			if (dp2[i][j + 1] < dp2[i][j]) { dp[i][j + 1] = dp[i][j]; dp2[i][j + 1] = dp2[i][j]; }
		}
	}
	return dp[a1.size()][a2.size()];
}
int main() {
	while (cin >> S) {
		for (int i = 0; i < 102400; i++)dp[i / 320][i % 320] = "";
		if (S == "#END")break;
		n = S.size(); string V = "";
		for (int i = 1; i < n; i++) {
			string T = S.substr(0, i), U = S.substr(i, n - i);
			string W = LCS(T, U);
			if (V.size() < W.size())V = W;
		}
		cout << V << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 7170
memory_kb: 9460
final_rank: 20

code_snippet: #include <string>
#include <iostream>
using namespace std;
int n; string s, dp[309][309];
int main() {
	while (cin >> s, s != "#END") {
		n = s.size();
		string ret;
		for (int i = 1; i < n; i++) {
			string l = s.substr(0, i), r = s.substr(i, n - i);
			for (int j = 1; j <= i; j++) {
				for (int k = 1; k <= n - i; k++) {
					if (l[j - 1] == r[k - 1]) dp[j][k] = dp[j - 1][k - 1] + r[k - 1];
					else if (dp[j][k - 1].size() > dp[j - 1][k].size()) dp[j][k] = dp[j][k - 1];
					else dp[j][k] = dp[j - 1][k];
				}
			}
			if (ret.size() < dp[i][n - i].size()) ret = dp[i][n - i];
		}
		cout << ret << endl;
	}
}
accuracy: 1
time_ms: 6030
memory_kb: 9124
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
string LCS(string &a,string &b){
	const int LMAX=300;
	int m=a.size(),n=b.size();
	int dp[LMAX+1][LMAX+1],from[LMAX+1][LMAX+1];
	rep(i,m+1)dp[i][0]=0,from[i][0]=-1;
	rep(i,n+1)dp[0][i]=0,from[0][i]=-1;
	rep(i,m)rep(j,n){
		if(a[i]==b[j]){
			dp[i+1][j+1]=dp[i][j]+1;
			from[i+1][j+1]=2;
		}else{
			if(dp[i][j+1]<dp[i+1][j]){
				dp[i+1][j+1]=dp[i+1][j];
				from[i+1][j+1]=1;
			}else{
				dp[i+1][j+1]=dp[i][j+1];
				from[i+1][j+1]=0;
			}
		}
	}
	int idx=dp[m][n];
	string out(idx,'!');
	for(int i=m,j=n;~from[i][j];){
		switch(from[i][j]){
			case 0:i--;break;
			case 1:j--;break;
			case 2:i--;j--;idx--;out[idx]=a[i];break;
		}
	}
	return out;
}
int main(){
	string s;
	while(cin>>s,s!="#END"){
		int out=0;
		string ans="";
		rep(i,s.size()-1){
			string a=s.substr(0,i+1);
			string b=s.substr(i+1);
			string t=LCS(a,b);
			if(t.size()>out){
				out=t.size();
				ans=t;
			}
		}
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 1600
memory_kb: 1912
final_rank: 2

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
#define MAX 301
  
int dp[MAX][MAX];
int arr[MAX][MAX];
  
void init(int N,int M){
    for(int i = 0 ; i <= N ; i++){
	for(int j = 0 ; j <= M ; j++){
	    dp[i][j] = 0;
	    arr[i][j] = MAX;
	}
    }
}
 
string getStr(string &L,string &R,int l,int r){
    for(int i = 0 ; i < l ; i++){
	for(int j = 0 ; j < r ; j++){
	    if(L[i] == R[j]){
		dp[i+1][j+1] = dp[i][j]+1;
		arr[i+1][j+1] = 0;
	    }else{
		if(dp[i][j+1] < dp[i+1][j]){
		    dp[i+1][j+1] = dp[i+1][j];
		    arr[i+1][j+1] = -1;
		}else{
		    dp[i+1][j+1] = dp[i][j+1];
		    arr[i+1][j+1] = 1;
		}
	    }
	}
    }
    string res;
    for(int i = l, j = r ; i > 0 && j > 0 ; ){
	if(!arr[i][j]){
	    res += L[i-1];
	    i--; j--;
	}else if(arr[i][j] == 1){
	    i--;
	}else{
	    j--;
	}
    }
    reverse(res.begin(),res.end());
    return res;
}
 
int main(){
    string str;
    while(cin >> str, str != "#END"){
	int len = str.size(),res = 0;
	string res_str;
	for(int i = 1 ; i < len ; i++){
	    string L = str.substr(0,i);
	    string R = str.substr(i);
	    int l = L.size(),r = R.size();
	    init(l,r);
	    string repS = getStr(L,R,l,r);
	    l = repS.size();
	    if(res < l){
		res = l;
		res_str = repS;
	    }
	}
	cout << res_str << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 1940
memory_kb: 3848
final_rank: 10

code_snippet: //2090
#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

const int MAX_LEN=300;

char S[MAX_LEN+1];
int len;

int dp[MAX_LEN+1][MAX_LEN+1];
char out[MAX_LEN+1];

int lcs(int sp){
	int len1=sp;
	int len2=len-sp;
	FOR(i,1,len1+1){
		FOR(j,1,len2+1){
			if (S[i-1]==S[sp+j-1]){
				dp[i][j]=dp[i-1][j-1]+1;
			}
			else{
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}
	return dp[len1][len2];
}

int main(){
	do{
		scanf("%s",S);
		if (S[0]!='#'){
			len=strlen(S);
			int ans=0;
			int sp;
			FOR(i,1,len){
				int l=lcs(i);
				if (l>ans){
					ans=l;
					sp=i;
				}
			}
			lcs(sp);
			int i=sp,j=len-sp;
			stack<char> st;
			while (ans>0){
				if (S[i-1]==S[sp+j-1]){
					ans--;
					st.push(S[i-1]);
					i--;
					j--;
				}
				else{
					if (dp[i-1][j]==dp[i][j]){
						i--;
					}
					else{
						j--;
					}
				}
			}
			while (!st.empty()){
				printf("%c",st.top());
				st.pop();
			}
			printf("\n");
		}
	}while(S[0]!='#');
	return 0;
}
accuracy: 1
time_ms: 1130
memory_kb: 1496
final_rank: 1