题目描述
小明正在玩一个“翻硬币”的游戏。
桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。
比如,可能情形是:oo*oooo
如果同时翻转左边的两个硬币,则变为:oooo***oooo
现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?
我们约定:把翻动相邻的两个硬币叫做一步操作
输入
两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度<1000
输出
一个整数,表示最小操作步数
样例输入
**********
o****o****
样例输出
5
package pro;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cnt = 0;
String str = in.next();
String string = in.next();
char ch1[] = str.toCharArray();
char ch2[] = string.toCharArray();
// Queue<Node> queue = new LinkedList<>();
// queue.add(new Node(str,0));
// Map<String,Integer> map = new HashMap<>();
// map.put(str, 1);
// while(!queue.isEmpty()) {
// Node node = queue.poll();
//
// for(int i = 0; i < str.length()-1; i++) {
// char arr[] = node.s.toCharArray();
// if(arr[i]=='*') arr[i] = 'o';
// else arr[i] = '*';
// if(arr[i+1]=='*') arr[i+1] = 'o';
// else arr[i+1] = '*';
//
// Node node2 = new Node(String.valueOf(arr),node.step+1);
//
// if(node2.s.compareTo(string)==0) {
// flag = 1;
// System.out.println(node2.step);
// break;
// }
//
// if(map.containsKey(node2.s)) continue;
// queue.add(node2);
// map.put(node2.s, 1);
// }
// if(flag == 1) break;
// }
for(int i = 0; i < str.length(); i++) {
if(ch1[i] != ch2[i]) {
if(ch1[i] == '*') ch1[i] = 'o';
else ch1[i] = '*';
if(ch1[i+1] == '*') ch1[i+1] = 'o';
else ch1[i+1] = '*';
cnt++;
}
}
System.out.println(cnt);
}
}
class Node{
String s;
int step;
public Node(String s, int step) {
super();
this.s = s;
this.step = step;
}
}
贪心
广搜超时