小明正在玩一个“翻硬币”的游戏。
桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。
比如,可能情形是:**oo***oooo
如果同时翻转左边的两个硬币,则变为:oooo***oooo
现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?
我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:
两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度<1000
一个整数,表示最小操作步数。
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 using namespace std; 5 6 char str1[1010], str2[1010]; 7 int num[1010]; 8 9 int main() { 10 while(cin >> str1 >> str2) { 11 int len = strlen(str1); 12 memset(num, 0, sizeof(num)); 13 14 for (int i=0; i<len; ++i) { 15 if (str1[i] != str2[i]) { 16 num[i] = 1; 17 } 18 } 19 20 int ans = 0; 21 int st; 22 bool exit = false; 23 for (int i=0; i<len; ++i) { 24 if (num[i] == 1 && exit) { 25 ans += i - st; 26 exit = false; 27 } 28 else if (num[i] == 1 && !exit) { 29 st = i; 30 exit = true; 31 } 32 } 33 34 cout << ans << endl; 35 } 36 return 0; 37 }
原文:http://www.cnblogs.com/icode-girl/p/5261375.html