hdu1501 记忆化搜索。。。

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

首先说关于字符串匹配寻找的基本思想  将 a b两个数字的位数依次和c中的比较  如果匹配合适的话 a/b 以及c的坐标前移一位 搜索的终点为字符串的结尾‘\0’

这里用的dfs的参数有三个  分别为a b c三个数组的坐标(当a b的某位和c相同的时候 便出现了分支 也正是由于分支的存在 所以标记数组就可以出现用来剪枝啦)

由于到达坐标 i j(由于c数组的长度为 a b之和 所以k没有必要作为参数 一个二维数组就可以搞定了) 的情况有很多种 我们的目标是去匹配字符  当坐标为i j的时候

a b c剩下的字符都是一样的 所以不论怎么匹配到 i j 情况都是一样的 为了避免超时  这里就可以用一个标记数组进行记忆搜索啦

贴上代码

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
string a,b,c;
int vis[501][500],flag;
void dfs(int i,int j,int k)
{
 if(vis[i][j]) return;// 对标记过的次数直接忽视

vis[i][j]=1; // 标记
 if(c[k]==‘\0‘)
 {
   flag=1;
   return; 
 }
 if(a[i]==c[k]&&b[j]==c[k])
 {
  dfs(i+1,j,k+1);
  dfs(i,j+1,k+1);
 }else if(a[i]==c[k]&&b[j]!=c[k])
 {
  dfs(i+1,j,k+1);
 }else if(a[i]!=c[k]&&b[j]==c[k])
 {
  dfs(i,j+1,k+1);
 }
}
int main()
{
 int n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
     cin>>a>>b>>c;
     flag=0;
     memset(vis,0,sizeof(vis));
        dfs(0,0,0);
        printf("Data set %d: ",i);
  if(flag==1) cout<<"yes"<<endl;
  else cout<<"no"<<endl;     
    }
 return 0;
}

时间: 2024-10-10 07:01:24

hdu1501 记忆化搜索。。。的相关文章

HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加字符的个数. 题解1(LCS): 很神奇的做法. 先求s和s的反串的LCS,也就是原串中已经满足回文性质的字符个数. 然后要变成回文串的话,只需要为剩下的每个落单的字符,相应地插入一个和它相同的字符即可. 所以答案是:s.size()-LCS(s,rev(s)) 另外,求LCS时只会用到lcs[i-

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

题目链接:uva 1076 - Password Suspects 题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出. 解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串. #include <cstdio> #include <cstring> #include <queue> #include <string> #include <

poj 1579(动态规划初探之记忆化搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17843   Accepted: 9112 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b

记忆化搜索,FatMouse and Cheese

1.从gird[0][0]出发,每次的方向搜索一下,每次步数搜索一下 for(i=0; i<4; i++) { for(j=1; j<=k; j++) { int tx=x+d[i][0]*j; int ty=y+d[i][1]*j; if(tx>=0&&tx<n&&ty>=0&&ty<n&&grid[x][y]<grid[tx][ty]) { int temp=memSearch(tx,ty); i

LightOJ1417 Forwarding Emails(强连通分量+缩点+记忆化搜索)

题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以看到信息. 然后SCC缩点后就形成DAG,直接记忆化搜索,d(u)搜索从u点出发开始传最多能传多少人. 最后就是找答案了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namesp

POJ1088(记忆化搜索)

经典记忆化搜索题目.当 从每个点一次进行搜索时要采用 记忆化搜索 #include"cstdio" #include"algorithm" using namespace std; const int MAXN=105; int g[MAXN][MAXN]; int t[MAXN][MAXN]; int maxn; int n,m; int by,bx; int ans; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; int

BZOJ 1079: [SCOI2008]着色方案 记忆化搜索

1079: [SCOI2008]着色方案 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1079 Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得很难看,所以你希望统计任意两个相邻木块颜色不同的

Codeforces Div.301D Bad Luck Island(概率dp+记忆化搜索)

一道概率dp问题. 题目链接:http://codeforces.com/contest/540/problem/D 题目大意:一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j][k]来存储i个石头,j个剪刀,k个布时,某物种的存活概率,共dp三次,算出三个物种分别的概率. 首先,我们需要把对应想求的物种概率初始化,这里以石头为例,那么对于i从1到r,不难理解dp[i][0][0]=

POJ 2704 Pascal&#39;s Travels (基础记忆化搜索)

Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5328   Accepted: 2396 Description An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from t