也是uva 1588
这种题目不禁让人想到KMP
可是这个长度才100啊想怎么暴力就怎么暴力 用的就是传说中的BF算法(brute-force)
把短串加到长串的两边 然后匹
swap和strncpy用的飞起不枉我重修了一遍计导和c语言
poj题目链接:http://poj.org/problem?id=3158
#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath> #include <set> #include <queue> #include <stack> #include <map> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; const int maxn = 310; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); char a[maxn], b[maxn], c[maxn]; while(scanf("%s%s", a, b) == 2) { int alen = strlen(a); int blen = strlen(b); if(alen < blen) { swap(a, b); swap(alen, blen); } int clen = 2*blen + alen; fill(c, c + blen, '1'); strncpy(c+blen, a, alen); fill(c + blen + alen,c + 2*blen + alen, '1'); int ans = alen + blen; for(int j = 0; j < clen-blen; j++) { bool flag = true; int tmp; for(int i = 0; i < blen && flag; i++) { if(c[j+i] - '0' + b[i] - '0' > 3) flag = false; } if(flag) { if(blen <= j && j <= alen) { ans = alen; break; } else if(j < blen) { tmp = alen + blen - j; if(tmp < ans) ans = tmp; } else if(j > alen) { tmp = j; if(tmp < ans) ans = tmp; } } } printf("%d\n", ans); } return 0; }