Zipper  OpenJ_Bailian - 2192  JAVA 超时

92 篇文章 0 订阅
75 篇文章 0 订阅
该博客讨论了一种名为Zipper的问题,其中需要判断第三个字符串是否可以由前两个字符串混合组成,保持原始顺序。作者提到了在使用JAVA实现时遇到的超时问题,并指出C++的解决方案更快。为了解决超时,他们引入了剪枝思想,包括选择性DFS(只在可能构造出结果的情况下进行)和记忆化搜索来减少重复过程。此外,还提及了一个优化技巧,即在确认字符串顺序正确后再进行DFS。
摘要由CSDN通过智能技术生成

W - Zipper

 OpenJ_Bailian - 2192 

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

JAVA超时,同样的C++就过了

剪枝思想很优秀,1.选择性DFS,只有当前i/j位置上的c符合能构造成结果的才DFS

2.记忆化搜索 DFS的过程中有大量的重复过程,造成了时空的浪费

dp[i][j]==1 就是当前的i,j是可以构造出(一部分)解的

3.注释部分也是一种优化,确定对s[0]/s[1]的顺序是正确的再DFS

import java.util.Scanner;
public class Main{
	private static boolean ans;
	static int[][] dp;
	static String s[];
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int p = 0;
		sc.nextLine();
		while(p++!=n) {
			s = sc.nextLine().split(" ");
//			ans = true;
//			int temp=0,now=0;
//			for (int i = 0; i < s[0].length() &&ans; i++) {
//				temp = s[2].indexOf(s[0].charAt(i),now);
//				if(temp>=now) now = temp;
//				else ans=false;
//			}
//			temp=0;now=0;
//			for (int i = 0; i < s[1].length() &&ans; i++) {
//				temp = s[2].indexOf(s[1].charAt(i),now);
//				if(temp>=now) now = temp;
//				else ans=false;
//			}
//			if(ans) {
//				ans=false;
//				dfs(s,0,0);
//			}
			dp = new int[505][505];
			for (int i = 0; i < dp.length; i++) {
				for (int j = 0; j < dp[i].length; j++) {
					dp[i][j]=-1;
				}
			}
			if(1==dfs(0,0)) 
				System.out.printf("Data set %d: yes\n",p);
			else
				System.out.printf("Data set %d: no\n",p);
		}
	}

	private static int dfs(int i, int j) {
		if(i==s[0].length()&&j==s[1].length()) {return 1;}
		if(dp[i][j]!=-1) {return dp[i][j];}
		if(i<s[0].length()&&s[0].charAt(i)==s[2].charAt(i+j))		//选择性DFS
			if(1==dfs(i+1,j))
			return dp[i][j] = 1;
		if(j<s[1].length()&&s[1].charAt(j)==s[2].charAt(i+j))
			if(1==dfs(i,j+1))
			return dp[i][j] = 1;
		return 0;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值