zoukankan      html  css  js  c++  java
  • soj1010. Zipper

    1010. Zipper

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order. For example, consider forming "tcraete" from "cat" and "tree": String A: cat String B: tree String C: tcraete As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree": String A: cat String B: tree String C: catrtee Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".

    Input

    The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line. For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.

    Output

    For each data set, print: Data set n: yes if the third string can be formed from the first two, or Data set n: no if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.

    Sample Input

    3
    cat tree tcraete
    cat tree catrtee
    cat tree cttaree

    Sample Output

    Data set 1: yes
    Data set 2: yes
    Data set 3: no

    动态规划,开一个二维数组,dp[i][j] 表示 以s0s1s2.....s(i-1)与t0t1t2...t(j-1)构成c0c1...c(i+j-1)的可能性。

    基态:

    if(s[i] == c[i] ) dp[i+1][0] = 1;

    if(t[j] == c[j]) dp[0][j+1] = 1;

    递归态:

    if(dp[i-1][j] && s[i-1] == c[i+j-1]) dp[i][j] = 1;

    if(dp[i][j-1] && t[j-1] == c[i+j-1]) dp[i][j] = 1;

    代码如下:

    #include <iostream>
    #include <string>
    #include <memory.h>
    using namespace std;
    
    int dp[202][202];
    
    int main()
    {
    	int N;
    	cin >> N;
    	int count = 0;
    	while(N--)
    	{
    		count++;
    		memset(dp,0,sizeof(dp));
    		string ss,tt,cc;
    		cin >> ss >> tt >> cc;
    		int i,j;
    		for(i = 0;i < ss.size();i++)
    		{
    			if(ss[i] == cc[i])
    				dp[i+1][0] = 1;
    		}
    		for(j = 0;j < tt.size();j++)
    		{
    			if(tt[j] == cc[j])
    				dp[0][j+1] = 1;
    		}
    		for(i = 1;i <= ss.size();i++)
    		{
    			for(j = 1;j <= tt.size();j++)
    			{
    				if(dp[i-1][j] && ss[i-1] == cc[i+j-1])
    					dp[i][j] = 1;
    				if(dp[i][j-1] && tt[j-1] == cc[i+j-1])
    					dp[i][j] = 1;
    			}
    		}
    		int len1 = ss.size();
    		int len2 = tt.size();
    		if(dp[len1][len2])
    			cout << "Data set " << count << ": " << "yes" << endl;
    		else
    			cout << "Data set " << count << ": " << "no" << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    java,for穷举,经典题目,百马百担
    《DSP using MATLAB》Problem 5.27
    《DSP using MATLAB》Problem 5.24-5.25-5.26
    《DSP using MATLAB》Problem5.23
    《DSP using MATLAB》Problem 5.22
    《DSP using MATLAB》Problem 5.21
    《金婚》截图
    《DSP using MATLAB》Problem 5.20
    《DSP using MATLAB》Problem 5.19
    《DSP using MATLAB》Problem 5.18
  • 原文地址:https://www.cnblogs.com/sysu-blackbear/p/3211177.html
Copyright © 2011-2022 走看看