1. 程式人生 > >noiopenjudge8469:特殊密碼鎖 貪心

noiopenjudge8469:特殊密碼鎖 貪心

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;

#define MAXN (35)
char s[MAXN], t[MAXN], s2[MAXN];
int cnt = 0;

void an_s(int i)
{
    ++ cnt;
    if(s[i-1] == '0') s[i-1] = '1';
    else s[i-1] = '0';
    if(s[i] == '0') s[i] = '1'
; else s[i] = '0'; if(s[i+1] == '0') s[i+1] = '1'; else s[i+1] = '0'; } void an_t(int i) { ++ cnt; if(t[i-1] == '0') t[i-1] = '1'; else t[i-1] = '0'; if(t[i] == '0') t[i] = '1'; else t[i] = '0'; if(t[i+1] == '0') t[i+1] = '1'; else t[i+1] = '0'; } #define INF (1e9) int
main() { scanf("%s", s+1); scanf("%s", s2+1); int n = strlen(s+1); int ans = INF; for(int i = 1; i <= n; ++ i) t[i] = s[i]; for(int i = 2; i <= n; ++ i) { if(s[i-1] == s2[i-1]) continue; an_s(i); } if(s[n] != s2[n]) ans = INF; else ans = cnt; cnt = 0
; an_t(1); for(int i = 2; i <= n; ++ i) { if(t[i-1] == s2[i-1]) continue; an_t(i); } if(t[n] != s2[n]) cnt = INF; ans = min(ans, cnt); if(ans >= INF) puts("impossible"); else cout << ans << endl; return 0; } /* 0101 1010 */