Zipper OpenJ_Bailian - 2192 JAVA 超时

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

 

你可能感兴趣的:(OJ,Test,水题,JAVA,搜索-dfs,模拟,剪枝优化)