ACM POJ 2192 Zipper

题目大意:输入字符串a,b,c 要求判断c是否有a,b中的个字符保持原有顺序组合而成。

算法思想:

DP

用dp[i][j]表示a的前0~i-1共i个字符和b的前0~j-1共j个字符是否构成c[i+j-1].

状态转换方程:

if(i>=1&&c[i+j-1]==a[i-1])

dp[i][j]=dp[i][j]||dp[i-1][j]

if(j>=1&&c[i+j-1]==b[j-1])

dp[i][j]=dp[i][j]||dp[i][j-1]

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        char a[250];
        char b[250];
        char c[550];
    scanf("%s%s%s",a,b,c);
    int la=strlen(a);
    int lb=strlen(b);
    int lc=strlen(c);
    int dp[250][250];
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    if(a[0]==c[0])dp[1][0]=1;
    if(b[0]==c[0])dp[0][1]=1;
    for(int j=0;j<=la;j++){
        for(int k=0;k<=lb;k++){
            if(k>=1&&c[k+j-1]==b[k-1])
                dp[j][k]=dp[j][k]||dp[j][k-1];
            if(j>=1&&c[k+j-1]==a[j-1])
                dp[j][k]=dp[j][k]||dp[j-1][k];
        }
    }
    if(dp[la][lb])cout<<"Data set "<<i<<": yes"<<endl;
    else
       cout<<"Data set "<<i<<": no"<<endl;
    }
    return 0;
}
时间: 2024-10-26 23:11:53

ACM POJ 2192 Zipper的相关文章

poj 2192 Zipper(区间dp)

题目链接:http://poj.org/problem?id=2192 思路分析:该问题可以看做dp问题,同时也可以使用dfs搜索求解,这里使用dp解法: 设字符串StrA[0, 1, …, n]和StrB[0,1, .., m]构成字符串Str[0, 1, … , m + n + 1]; 1)状态定义:dp[i, j]表示字符串StrA[0, 1, …, i-1]和字符串StrB[0, 1, .., j-1]构成字符串Str[0, 1, …, i+j-1]: 2)状态转移:如果dp[i-1][

poj 2192 Zipper

题目链接:http://poj.org/problem?id=2192 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18658   Accepted: 6651 Description Given three strings, you are to determine whether the third string can be formed by combining the characters in the fi

POJ 2192 Zipper (dp)

链接: http://poj.org/problem?id=2192 题意:就是给定三个字符串A,B,C:判断C能否由AB中的字符组成,同时这个组合后的字符顺序必须是A,B中原来的顺序,不能逆序:例如:A:mnl,B:xyz:如果C为mnxylz,就符合题意:如果C为mxnzly,就不符合题意,原因是z与y顺序不是B中顺序. DP求解:定义dp[i][j]表示A中前i个字符与B中前j个字符是否能组成C中的前 (i+j) 个字符,如果能,标记1,如果不能,标记0: 有了这个定义,我们就可以找出状态

POJ - 2192 - Zipper (简单DP)

题目传送:Zipper 思路:设状态dp[i][j]为字符串A前i个字符和B前j个字符能否组成C的前i+j个字符,边界为dp[0][0] = 1能则为true,否则false AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

[ACM] POJ 1068 Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

[ACM] POJ 3295 Tautology (构造)

Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3549 Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s,

[ACM] POJ 3295 Ubiquitous Religions (并查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted: 11379 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi