/* 这题我的思路是这样的,假设有串a和串b ①先固定a,a从第一个元素开始,b从第一个元素开始(from=1),一一比较直到遇到失配的情况(进入②)或者某串结束的情况(进入③),每次匹配,会有same++,表示在这个位置,它们是符合容器高度的上限3的; ②表示位置的from++,b从from开始,a从第一个开始,再来一一匹配,情况处理同① ③某串结束以后,匹配也就结束了,那么固定a移动b的情况下,总长度至少是(len1 + len2 - same) ④再换固定b,移动a,处理方式和固定a一样,从①开始走一次流程即可...两种情况的总长度最小值,就是答案 其实这题中i就是same,也可不定义这个变量,直接用i 一个小细节: 这题是字符串不是int型数组,别忘了是 == '2',2的ASCII码而不是数字2...太粗心了,唉 */
#include <iostream> #include <cstring> using namespace std; const int N = 105; char a[N], b[N]; int main() { while (cin >> a >> b) { int len1 = strlen(a), len2 = strlen(b), i, j, same, from; //same记录当前时的匹配情况,from记录可以移动的串的开始点 int sum1 = len1 + len2, sum2 = len1 + len2; for (i = 0, j = 0, same = 0, from = 0; i < len1 && j < len2; ) { if (!(a[i] == '2' && b[j] == '2')) i++, j++; else from++, i = 0, j = from; } // cout << "test: " << i << endl; sum1 -= i; for (i = 0, j = 0, same = 0, from = 0; i < len2 && j < len1; ) { if (!(b[i] == '2' && a[j] == '2')) i++, j++; else from++, i = 0, j = from; } sum2 -= i; cout << min(sum1, sum2) << endl; } return 0; }