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

Solution in JAVA (judge verdict: Memory Limit Exceeded)

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	MyScanner sc = new MyScanner();
	Scanner sc2 = new Scanner(System.in);
	final int MOD = 1000000007;
	int[] dx = { 1, -1, 1, -1 };
	int[] dy = { 1, 1, -1, -1 };

	void run() {
		for (;;) {
			String in = sc.next();
			if (in.equals("#END")) {
				return;
			}
			String ans = "";
			for (int i = 1; i < in.length(); i++) {
				String lcs = lcs(in.substring(0, i).toCharArray(), in
						.substring(i, in.length()).toCharArray());
				if (lcs.length() > ans.length()) {
					ans = lcs;
				}
			}
			System.out.println(ans);
		}
	}

	String lcs(char[] s, char[] t) {
		int n = s.length;
		int m = t.length;
		int[][] dp = new int[n + 1][m + 1];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (s[i] == t[j]) {
					dp[i + 1][j + 1] = dp[i][j] + 1;
				} else {
					dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i][j + 1]);
				}
			}
		}
		char[] res = new char[dp[n][m]];
		while (n > 0 && m > 0) {
			if (dp[n][m] == dp[n - 1][m])
				n--;
			else if (dp[n][m] == dp[n][m - 1])
				m--;
			else {
				res[dp[n - 1][m - 1]] = s[n - 1];
				n--;
				m--;
			}
		}
		return String.valueOf(res);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}

	void debug2(int[][] array) {
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				System.out.print(array[i][j]);
			}
			System.out.println();
		}
	}

	void debug2(long[][] array) {
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				System.out.print(array[i][j]);
			}
			System.out.println();
		}
	}

	boolean inner(int h, int w, int limH, int limW) {
		return 0 <= h && h < limH && 0 <= w && w < limW;
	}

	void swap(int[] x, int a, int b) {
		int tmp = x[a];
		x[a] = x[b];
		x[b] = tmp;
	}

	// find minimum i (a[i] >= border)
	int lower_bound(int a[], int border) {
		int l = -1;
		int r = a.length;

		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (border <= a[mid]) {
				r = mid;
			} else {
				l = mid;
			}
		}
		// r = l + 1
		return r;
	}

	boolean palindrome(String s) {
		for (int i = 0; i < s.length() / 2; i++) {
			if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
				return false;
			}
		}
		return true;
	}

	class MyScanner {
		int nextInt() {
			try {
				int c = System.in.read();
				while (c != '-' && (c < '0' || '9' < c))
					c = System.in.read();
				if (c == '-')
					return -nextInt();
				int res = 0;
				do {
					res *= 10;
					res += c - '0';
					c = System.in.read();
				} while ('0' <= c && c <= '9');
				return res;
			} catch (Exception e) {
				return -1;
			}
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		String next() {
			try {
				StringBuilder res = new StringBuilder("");
				int c = System.in.read();
				while (Character.isWhitespace(c))
					c = System.in.read();
				do {
					res.append((char) c);
				} while (!Character.isWhitespace(c = System.in.read()));
				return res.toString();
			} catch (Exception e) {
				return null;
			}
		}

		int[] nextIntArray(int n) {
			int[] in = new int[n];
			for (int i = 0; i < n; i++) {
				in[i] = nextInt();
			}
			return in;
		}

		int[][] nextInt2dArray(int n, int m) {
			int[][] in = new int[n][m];
			for (int i = 0; i < n; i++) {
				in[i] = nextIntArray(m);
			}
			return in;
		}

		double[] nextDoubleArray(int n) {
			double[] in = new double[n];
			for (int i = 0; i < n; i++) {
				in[i] = nextDouble();
			}
			return in;
		}

		long[] nextLongArray(int n) {
			long[] in = new long[n];
			for (int i = 0; i < n; i++) {
				in[i] = nextLong();
			}
			return in;
		}

		char[][] nextCharField(int n, int m) {
			char[][] in = new char[n][m];
			for (int i = 0; i < n; i++) {
				String s = sc.next();
				for (int j = 0; j < m; j++) {
					in[i][j] = s.charAt(j);
				}
			}
			return in;
		}
	}
}