全部评论
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @file: 扑克游戏
* @author: Ash
* @date: 2019/9/1 15:15
* @description:
* 3
* 123
* 3
* 123
* 321
* 45
* 67
*
* {
* d d l
* d d r
* }
* {
* l l l
* r l l
* }
* {
* }
* @since:
*/
public class 扑克游戏 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int s = Integer.parseInt(in.nextLine());
for (int i = 0; i < s; i++) {
String str1 = in.nextLine();
String str2 = in.nextLine();
int n = str1.length();
System.out.println("{");
List<String> res = new ArrayList<>();
dfs("", str2, str1, 0, n, "", res);
for (String st : res) {
System.out.println(st.trim());
}
System.out.println("}");
}
}
in.close();
}
private static void dfs(String cmd, String goal, String ori, int depth, int n, String s, List<String> res) {
if (depth == n && !goal.equals(s) || depth > n || s.length() > goal.length()) {
return;
}
if (depth == n && goal.equals(s)) {
res.add(cmd);
} else {
// d
dfs(cmd + " " + "d", goal, ori, depth + 1, n, s, res);
// l
dfs(cmd + " " + "l", goal, ori, depth + 1, n, ori.charAt(depth) + s, res);
// r
dfs(cmd + " " + "r", goal, ori, depth + 1, n, s + ori.charAt(depth), res);
}
}
}
原本以为dfs肯定时间不够,还想要剪枝,但一看题目已经放水了,N、M是[1, 8]。我来个python版本的AC代码。 import sys
def str2list(string):
res = []
while string != '':
res.append(int(string[0]))
string = string[1:]
return res
def dfs(mei, create, temp):
if create == bai and not mei:
res.append(temp)
elif not mei:
return
else:
# d
dfs(mei[1:], create, temp + ['d'])
# l
dfs(mei[1:], [mei[0]] + create, temp + ['l'])
# r
dfs(mei[1:], create + [mei[0]], temp + ['r'])
data = sys.stdin.readlines()
num = len(data)
max_len = int(data[0].strip())
for i in range((num - 1) // 2):
res = []
flag = True
mei = str2list(data[1 + i * 2].strip())
bai = str2list(data[2 + i * 2].strip())
print('{')
for b in bai:
if b not in mei:
flag = False
if not flag:
print('}')
continue
dfs(mei, [], [])
for j in range(len(res)):
out = ''
for s in res[j]:
out += s + ' '
print(out[:-1])
print('}')
求助大佬😂
似乎是最短编辑距离的思路。
我是暴力穷举解出来的
求助为什么输出会有两行呀,不是只有小梅可以操作吗?而且丢弃最左边,意思是其他的都加入新序列?
package company.pinduoduo;
import java.util.*;
public class Main02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
int s = sc.nextInt(); //进行的次数
for (int i = 1; i<=s ; ++i) {
String mei = sc.next();
String bai = sc.next();
List<String> res = new ArrayList<>();
List<String> list = dfs(mei,bai,"",0,res
,"");
Collections.sort(list);
System.out.println("{");
for(String ls: list){
for (int j = 0; j <ls.length() ; j++) {
if(j!=ls.length()-1) System.out.print(ls.charAt(j)+" ");
else {
System.out.print(ls.charAt(j));
}
}
System.out.println();
}
System.out.println("}");
}
}
}
public static List<String> dfs(String mei,String bai,String newStr,
int i,List<String> res,String way){
if(bai.equals(newStr)){
res.add(way);
return res;
}
if(i==mei.length()) return res;
way+="d";
dfs(mei,bai,newStr,i+1,res,way);
way = way.substring(0,way.length()-1);
String tmp1 = newStr;
newStr = mei.substring(i,i+1) + newStr;
way+="l";
dfs(mei,bai,newStr,i+1,res,way);
way = way.substring(0,way.length()-1);
newStr = tmp1;
String tmp2 = newStr;
newStr = newStr + mei.substring(i,i+1);
way+="r";
dfs(mei,bai,newStr,i+1,res,way);
way = way.substring(0,way.length()-1);
newStr = tmp2;
return res;
}
}
哎,做的时候理解错了,一直以为在原牌堆交换得到新牌堆,结果是原牌堆一张张打出去放到新牌堆。。😓😓😓
相关推荐
11-24 14:20
北京大学 Java CoderEcho:虽然我没去大厂,但是待过俩家初创,我的感觉是厉害的初创很舍得给钱,也很看你的产出,产出不行直接把你开了。当然大厂也可能有这个问题。建议还是去大厂,如果被裁员了最起码还有大厂的title,这年头不确定的事儿太多了。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
//鲨鱼辣椒:什么什么都作废了,如同一场大雪覆盖了前半生的荒唐
点赞 评论 收藏
分享