Zipper

http://acm.hdu.edu.cn/showproblem.php?pid=1501 

题意:给出三个串,问是否第三个能用前两个表示,且他们的顺序不变,可以打乱;

思路:dfs: 要记忆化搜索,标记s1,s2完成了匹配的位置vis[cnt1][cnt2],因为值两个位置前无论再怎么组合都可以匹配出两个串的两部分;

            dp:dp[i][j]=true,表示s1前i位,s2前j位能匹配;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))
inline void sc(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

ll power(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=ans*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ans;
}

string s1,s2,s3;
int len1,len2,len3;
bool f=0;
bool vis[1009][1009];
void dfs(int cnt1,int cnt2,int cnt3)
{
    if(f) return ;
    if(cnt2==len2&&len1==cnt1)
    {
        f=1;
        return ;
    }
    if(vis[cnt1][cnt2]) return ;
    if(s3[cnt3]==s2[cnt2])
    {
        vis[cnt1][cnt2]=1;
        dfs(cnt1,cnt2+1,cnt3+1);
        //vis[cnt1][cnt2]=0;
    }
    if(s3[cnt3]==s1[cnt1])
    {
        vis[cnt1][cnt2]=1;
        dfs(cnt1+1,cnt2,cnt3+1);
        //vis[cnt1][cnt2]=0;
    }
}
int main()
{
    //freopen("input.txt", "r", stdin);
    //FASTIO;
    int t;
    cin>>t;
    int cas=0;
    while(t--)
    {
        mem(vis,0);
        f=0;
        cas++;
        cin>>s1>>s2>>s3;
        len1=s1.length();
        len2=s2.length();
        len3=s3.length();
        dfs(0,0,0);
        if(f)
        {
            printf("Data set %d: yes\n",cas);
        }
        else
        {
            printf("Data set %d: no\n",cas);
        }
    }

    return 0;
}

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))
inline void sc(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

ll power(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=ans*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ans;
}

char s1[209],s2[209],s3[2009];
int len1,len2,len3;
bool f=0;
bool dp[2009][2009];
int main()
{
    //freopen("input.txt", "r", stdin);
    //FASTIO;
    int t;
    cin>>t;
    int cas=0;
    while(t--)
    {
        mem(dp,0);
        f=0;
        cas++;
        scanf("%s%s%s",s1+1,s2+1,s3+1);
        len1=strlen(s1+1);
        len2=strlen(s2+1);
        len3=strlen(s3+1);
        //dp[0][0]=1;
        for(int i=1;i<=len1;i++)
        {
            if(s1[i]==s3[i])
            {
                dp[i][0]=1;
            }
            else break;
        }
        for(int i=1;i<=len2;i++)
        {
            if(s2[i]==s3[i])
            {
                dp[0][i]=1;
            }
            else break;
        }
        for(int i=1;i<=len1;i++)
        {
            for(int j=1;j<=len2;j++)
            {
                dp[i][j]=(dp[i-1][j]&&s1[i]==s3[i+j])||(dp[i][j-1]&&s2[j]==s3[i+j]);
            }
        }
        if(dp[len1][len2])
        {
            printf("Data set %d: yes\n",cas);
        }
        else
        {
            printf("Data set %d: no\n",cas);
        }
    }

    return 0;
}

 

你可能感兴趣的:(dp,记忆化搜索,dfs)