HDU 5707 Combine String (DP,LCS变形)

题意:给定三个字符串,问你第三个是不是由第一个和第二个组成的。

析:当时比赛是没有做出来啊。。。一直WA,就是没有判断长度,第一个和第二个和是不是和第三个一样,这个忘记。。。

我们用d[i][j]表示第一个字符串匹配到 i, 第二个匹配到第 j 个,然后只要判断能不能由上一个得到就好。这个主要是d[i][j]==1则表示可以成功匹配

d[i][j]==0则表示无法成功匹配,那么剩下的就简单了。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
//int n, m;
char s[maxn], t[maxn], ss[maxn];
int d[maxn][maxn];
int main(){
//    int T;  cin >> T;
    while(scanf("%s", s+1) == 1){
        scanf("%s", t+1);
        scanf("%s", ss+1);
        memset(d, 0, sizeof(d));
        int n = strlen(s+1);
        int m = strlen(t+1);
        int ans = 0;
        if(n+m != strlen(ss+1)){
            printf("No\n");
            continue;
        }
        d[0][0] = 1;

        for(int i = 0; i <= n; ++i){
            for(int j = 0; j <= m; ++j){
                if(ss[i+j] == s[i] && i)  d[i][j] |= d[i-1][j];
                if(ss[i+j] == t[j] && j)  d[i][j] |= d[i][j-1];
            }
        }

        printf("%s\n", d[n][m] ? "Yes" : "No");
    }
    return 0;
}
时间: 2024-08-05 17:56:40

HDU 5707 Combine String (DP,LCS变形)的相关文章

HDU 5707 Combine String(动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5707 题意: 给你三个字符串 S1, S2, S3, 让你判断 S3 是否恰好由字符串 S1 和 S2组成, S1 为 S3 的子串, S2 也为 S3 的子串, 可以不连续. 思路: 设 dp[i][j] 表示字符串 S3 的前 i + j 位是否可以由字符串 S1 的前 i 位以及字符串 S2 的前 j 位组成. dp[i][j] = 1 表示可以, dp[i][j] = 0 则表示不可以. 则

UVA-1625-Color Length(DP LCS变形)

Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的尾部. https://odzkskevi.qnssl.com/a68cbd3e27f46b4f02ea12b7b1a1abca 然后产生的新序列中,对于每一个颜色c,都有出现的位置,L(c)表示最小位置和最大位置之差,求L(c)总和最小的新序列. 分析 LCS 是公共上升子序列,在动态转移的过程中,考虑

poj1080--Human Gene Functions(dp:LCS变形)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17206   Accepted: 9568 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four

POJ 1159-Palindrome(DP/LCS变形)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 53770   Accepted: 18570 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a

HDU 1069 Monkey and Banana LCS变形

点击打开链接题目链接 Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7617    Accepted Submission(s): 3919 Problem Description A group of researchers are designing an experiment to test

hdu 5311 Hidden String dp o(n)算法 深搜

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 857    Accepted Submission(s): 322 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets

HDU 1503 Advanced Fruits (LCS,DP)

题意:给你两字符串s1,s2,用最短的字符串表示他们(公共字串输出一次). Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch dp[i][j] : 第一个字符串的前 i 个 ,和第二个字符串的前 j 个最短组合的长度 . pre[i][j] : 第一个字符串的第 i 个 ,和第二个字符串的第 j 个字符的状态. #include<cstdio> #include<

poj1080——lcs变形,dp

poj1080——lcs变形,dp Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17610   Accepted: 9821 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simpl

poj 1080 (LCS变形)

Human Gene Functions 题意: LCS: 设dp[i][j]为前i,j的最长公共序列长度: dp[i][j] = dp[i-1][j-1]+1;(a[i] == b[j]) dp[i][j] = max(dp[i][j-1],dp[i-1][j]); 边界:dp[0][j] = 0(j<b.size) ,dp[i][0] = 0(i< a.size); LCS变形: 设dp[i][j]为前i,j的最大价值: value(x, y)为比较价值: dp[i][j] = max(d