程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU 1501 Zipper (DFS)

HDU 1501 Zipper (DFS)

編輯:關於C++
Problem 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

 

題意:給你3個單詞,要你判斷第三個單詞是否由前2個單詞以任意的排列(這2個單詞的順序不能改變)組合而成

思路:數據太小了,直接DFS吧!用a,b分別表示2個單詞出現到了哪個字母,如果最終能出現完這2個單詞,那麼說明是滿足條件的

 

AC代碼:

 

#include
#include
#include
using namespace std;

char str1[205];
char str2[205];
char str[405];
int vis[205][205];

int len1,len2,len;

int flag;

void dfs(int a,int b,int l)
{
    if(vis[a][b])
        return ;

    if(a==len1&&b==len2)
    {
        flag=1;
    }
    if(flag==1)
        return ;
    vis[a][b]=1;
    if(str[l]==str1[a]&&a<len1) {="" a++;="" dfs(a,b,l+1);="" a--;="" }="" if(str[l]="=str2[b]&&b

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved