POJ-2192 Zipper-順序合成串匹配
Zipper
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17283 | Accepted: 6145 |
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".
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.
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.
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
Source
Pacific Northwest 2004
//DP
#include<stdio.h>
#include<string.h>
#define MAX 201
int dp[MAX][MAX];
char A[MAX],B[MAX],C[MAX*2];
int main()
{
int T;
int i,j;
scanf("%d",&T);
A[0]=B[0]=C[0]='0';
for(int cas=1; cas<=T; cas++)
{
scanf("%s%s%s",A+1,B+1,C+1);//這裡都從下標為1開始
int a=strlen(A)-1,b=strlen(B)-1;
for(i=1; i<=a; i++) //邊界處理
if(A[i]==C[i]) dp[i][0]=1;
else dp[i][0]=0;
for(i=1; i<=b; i++) //邊界處理
if(B[i]==C[i]) dp[0][i]=1;
else dp[0][i]=0;
for(i=1; i<=a; i++)
for(j=1; j<=b; j++)
dp[i][j]=((dp[i-1][j] && A[i]==C[i+j])||(dp[i][j-1] && B[j]==C[i+j]));
printf("Data set %d: ",cas);
if(dp[a][b])
printf("yes\n");
else
printf("no\n");
}
return 0;
}
/*dp[i][j] = (A[i] == c[i+j] && dp[i-1][j] == 1) || (B[j] == c[i+j] && dp[i][j-1]==1)*/
//DFS
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define maxn 10000
using namespace std;
char word1[maxn],word2[maxn],word3[maxn];
int pos1,pos2,pos3,c;
bool flag;
void dfs(int wa,int wb,int wc)
{
if(wa+1==0&&wb+1==0)
{
flag=true;
return;
}
if(!flag&&word1[wa]==word3[wc]&&(wa+1))
dfs(wa-1,wb,wc-1);
if(!flag&&word2[wb]==word3[wc]&&(wb+1))
dfs(wa,wb-1,wc-1);
return ;
}
int main()
{
int loop;
cin>>loop;
int cns=1;
while(loop--)
{
cin>>word1>>word2>>word3;
printf("Data set %d: ",cns++);
flag=false;
pos1=strlen(word1);
pos2=strlen(word2);
pos3=strlen(word3);
if(word1[pos1-1]==word3[pos3-1]||word2[pos2-1]==word3[pos3-1])
dfs(pos1-1,pos2-1,pos3-1);
printf(flag?"yes\n":"no\n");
}
return 0;
}
相關文章
- nginx的location匹配順序、優先順序,location對映衝突排查Nginx
- 談Nginx的Location匹配優先順序Nginx
- Nginx中的一些匹配順序Nginx
- 無順序約束的字串匹配問題字串匹配
- CSS font 複合屬性順序CSS
- 引數匹配順序——Python學習之引數(三)Python
- Nginx配置指令location匹配符優先順序和安全問題Nginx
- 順序表
- python運算子及優先順序順序Python
- 第2章 順序表及其順序儲存
- 一款免費使用的PDF檔案批次合併工具,可操作pdf檔案順序,按順序合併pdf檔案。
- 一款免費使用的PDF檔案批量合併工具,可操作pdf檔案順序,按順序合併pdf檔案。
- 順序表應用5:有序順序表歸併
- 順序表應用6:有序順序表查詢
- 順序查詢
- UML順序圖
- 順序結構
- 靜態順序表和動態順序表 對比
- 列定義的順序和列儲存的順序
- 42. nginx 匹配的 try_file 與前端路由的優先順序問題Nginx前端路由
- 順序結構儲存串實現串萬用字元匹配的演算法字元演算法
- 適合零基礎初學者學習的Java順序Java
- RMQ——支援合併和優先順序的訊息佇列MQ佇列
- SpringBoot配置檔案優先順序載入順序Spring Boot
- 順序容器初探(上)
- HTML 屬性順序HTML
- DNS查詢順序DNS
- Sql執行順序SQL
- CSS優先順序CSS
- javascript載入順序JavaScript
- 初識順序容器
- 微信聊天表情順序設定方法 微信表情順序怎麼調?
- 【Oracle】-【插入讀取順序】-插入讀取之間的順序關係Oracle
- oracle pfile spfile啟動順序 優先順序 spfilesid 〉 spfile 〉 initsidOracle
- THREE.js渲染順序JS
- js物件遍歷順序JS物件
- CSS 屬性宣告順序CSS
- 佇列-順序儲存佇列