Codeforces 346 B. Lucky Common Subsequence


LCS+一维

KMP构造转移+记忆化搜索。。。。

B. Lucky Common Subsequence
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF.

You are given two strings s1s2 and another string called virus. Your task is to find the longest common subsequence of s1 and s2, such that it doesn't contain virus as a substring.

Input

The input contains three strings in three separate lines: s1s2 and virus (1 ≤ |s1|, |s2|, |virus| ≤ 100). Each string consists only of uppercase English letters.

Output

Output the longest common subsequence of s1 and s2 without virus as a substring. If there are multiple answers, any of them will be accepted.

If there is no valid common subsequence, output 0.

Sample test(s)
input
AJKEQSLOBSROFGZ
OVGURWZLWVLUXTH
OZ
output
ORZ
input
AA
A
A
output
0

/**
 * Created by ckboss on 14-9-4.
 */

import java.util.*;

public class LuckyCommonSubsequence {

    static String s1, s2, virus;
    static int[][][] dp = new int[200][200][200];
    static int[][] ch = new int[200][30];
    static int[] fail = new int[200];
    static int n, x, y;
    static boolean[][][] vis = new boolean[200][200][200];

    static void init() {
        fail[0] = fail[1] = 0;
        for (int i = 1; i < n; i++) {
            int j = fail[i];
            while (j != 0 && virus.charAt(i) != virus.charAt(j)) j = fail[j];
            fail[i + 1] = (virus.charAt(i) == virus.charAt(j)) ? j + 1 : 0;
        }

        for (int i = 0; i < n; i++) ch[i][virus.charAt(i) - 'A'] = i + 1;
        for (int i = 0; i < n; i++) {
            for (int c = 0; c < 26; c++) {
                if (ch[i][c] == 0)
                    ch[i][c] = ch[fail[i]][c];
            }
        }
    }

    static int dfs(int i, int j, int k) {
        if (vis[i][j][k] == true)
            return dp[i][j][k];
        vis[i][j][k] = true;
        if (k >= n)
            return dp[i][j][k] = -999999999;
        if (i >= x || j >= y)
            return dp[i][j][k] = 0;

        dp[i][j][k] = Math.max(dfs(i + 1, j, k), dfs(i, j + 1, k));

        if (s1.charAt(i) == s2.charAt(j)) {
            dp[i][j][k] = Math.max(dp[i][j][k], dfs(i + 1, j + 1, ch[k][s1.charAt(i) - 'A']) + 1);
        }

        return dp[i][j][k];
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        s1 = in.next().trim();
        s2 = in.next().trim();
        virus = in.next().trim();

        x = s1.length();
        y = s2.length();
        n = virus.length();
        init();

        if (dfs(0, 0, 0) != 0) {
            int i = 0, j = 0, k = 0;
            String ans = "";
            while (i < x && j < y) {
                if (dfs(i, j, k) == dfs(i + 1, j, k))
                    i++;
                else if (dfs(i, j, k) == dfs(i, j + 1, k))
                    j++;
                else {

                    ans += s1.charAt(i);
                    k = ch[k][s1.charAt(i) - 'A'];
                    i++;
                    j++;
                }
            }
            System.out.println(ans);
        } else {
            System.out.println("0");
        }
    }
}




你可能感兴趣的:(Codeforces 346 B. Lucky Common Subsequence)