#include #include #include #define N (300) #define min(a,b) (((a)<(b))?(a):(b)) #define max(a,b) (((a)>(b))?(a):(b)) static char str[N+2], ans[N+2]; static int nstr, nans; void lcs(int n) { static int table[N+1][N+1]; int i, j, m; char *p; m = nstr - n; for(i = 0; i <= n; i++) { table[i][0] = 0; } for(j = 0; j <= m; j++) { table[0][j] = 0; } for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { if(str[i] == str[n + j]) { table[i+1][j+1] = table[i][j] + 1; } else { table[i+1][j+1] = max(table[i+1][j], table[i][j+1]); } } } if(nans >= table[n][m]) { return; } nans = table[n][m]; i = n - 1; j = m - 1; p = ans + nans; *p = '\0'; while(i >= 0 && j >= 0) { if(str[i] == str[n + j]) { *(--p) = str[i]; i--; j--; } else { if(table[i+1][j] > table[i][j+1]) { j--; } else { i--; } } } } int main(void) { int i; while(1) { fgets(str, sizeof(str), stdin); nstr = strlen(str); str[--nstr] = '\0'; if(strcmp(str, "#END") == 0) { return 0; } nans = 0; for(i = 0; i < nstr; i++) { lcs(i); } printf("%s\n", ans); } }