題目 Day3T3 Hja 擁有一套時(shí)光穿梭技術(shù),能把字符串以超越光速的速度傳播,但是唯一 題解 略惡心的模擬: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define MAXN 2005 int n,num[MAXN]; char s[MAXN]; bool check1(char *s){ int len = strlen(s+1); if(len != n) return false; int Sum = 0; for(int i=1; i<=n; ++i) if(s[i] == '1') Sum += i; for(int i=1; i<=n; ++i) if(s[i] == '1'){ Sum -= i; if(Sum % (n+1) == 0){ s[i] = '0'; printf('%s\n',s+1); return true; } Sum += i; } return false; } bool check4(char *s){ int len = strlen(s+1); if(len != n) return false; int Sum = 0; for(int i=1; i<=len; ++i) if(s[i] == '1') Sum += i; if(Sum % (n+1) == 0){ printf('%s\n',s+1); return true; } return false; } bool check3(char *s){//多了一位 ---- > 消掉 int len = strlen(s+1); if(len != n+1) return false; int Sum = 0; for(int i=1; i<=len; ++i) { num[i] = num[i-1]; if(s[i] == '1') Sum += i,++num[i]; } for(int i=1; i<=len; ++i){ Sum -= num[len] - num[i]; if(s[i] == '1') Sum -= i; if(Sum % (n+1) == 0){ for(int j=1; j<i; ++j) printf('%c',s[j]); for(int j=i+1; j<=len; ++j) printf('%c',s[j]); printf('\n'); return true; } Sum += num[len] - num[i]; if(s[i] == '1') Sum += i; } return false; } bool check2(char *s){ int len = strlen (s+1); if(len != n-1) return false; int Sum = 0; for(int i=1; i<=len; ++i){ num[i] = num[i-1]; if(s[i] == '1') Sum += i,++num[i]; } for(int i=1; i<=n; ++i){ Sum += num[len] - num[i-1]; if(Sum % (n+1) == 0){ for(int j=1; j<i; ++j) printf('%c',s[j]); printf('0'); for(int j=i; j<=len; ++j) printf('%c',s[j]); printf('\n'); return true; } Sum += i; if(Sum % (n+1) == 0){ for(int j=1; j<i; ++j) printf('%c',s[j]); printf('1'); for(int j=i; j<=len; ++j) printf('%c',s[j]); printf('\n'); return true; } Sum -= i + num[len] - num[i-1]; } return false; } int main(int argc,char *argv[]){ freopen('a.in','r',stdin); freopen('a.out','w',stdout); scanf('%d',&n); while(~scanf('%s',s+1)){ bool ok = false; ok = check4(s); if(ok) continue; ok = check1(s); if(ok) continue; ok = check2(s); if(ok) continue; ok = check3(s); if(ok) continue; printf('-1\n'); } fclose(stdin);fclose(stdout); return 0; } |
|