Codeforces 346 B. Lucky Common Subsequence

70 篇文章 0 订阅


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");
        }
    }
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值