|
|
back to boardwhy WA #3 Posted by blo 2 Jul 2008 21:56 who can help me? #include <iostream> #include <string> #include <vector> using namespace std; int main() { int length; cin>>length; string word; vector<string> result; int max_count = 1000; while(max_count-->0 && cin>>word) { int word_length = word.size(); int tot = 0; int *g = new int[word_length]; int *a = new int[word_length]; int no_of_1 = 0; for(int i = 0;i<word_length;i++) { if(word[i] == '1') { a[i] = word_length-i; tot +=a[i];
g[word_length-i-1] = no_of_1; no_of_1 +=1; } else { a[i] = 0; g[word_length-i-1] = no_of_1; } } if(word_length == length) { if( !(tot%(length+1))) { result.push_back(word); } else { for (int i = 0;i<word_length;i++) { if( a[i] &&(tot-a[i])%(length+1) == 0) result.push_back(word.replace(i,1,"0")); } } } else if(word_length<length) { //try to insert at the last if(word[word_length-1] == '1') { //try to insert 1 if((tot+g[0]+2)%(length+1) ==0) { result.push_back(word.insert(word_length,"1")); continue; } //try 0 else if((tot+g[0]+1)%(length+1) ==0) { result.push_back(word.insert(word_length,"0")); continue; } } else if(word[word_length-1] == '0') { //try to insert 1 if((tot+g[0]+1)%(length+1) ==0) { result.push_back(word.insert(word_length,"1")); continue; } //try 0 else if((tot+g[0])%(length+1) ==0) { result.push_back(word.insert(word_length,"0")); continue; } }
// from right to left, try to insert on the left for(int i = 0; i<word_length;i++) { //try insert 1 if((tot+g[i]+i+2)%(length+1) ==0) { result.push_back(word.insert(word_length-1-i,"1")); break; } // try insert 0 else if((tot + g[i])%(length+1) ==0) { result.push_back(word.insert(word_length-i-1,"0")); break; } } } else { for(int i = 0; i<word_length;i++) { if(word[word_length-1-i] == '1'&& (tot-g[i]-a[word_length-1-i])%(length+1) ==0) { result.push_back(word.erase(word_length-1-i,1)); break; } else if( word[word_length-1-i] =='0' &&(tot-g[i])%(length+1) ==0) { result.push_back((word.erase(word_length-1-i,1))); break; } } } delete []a; delete []g; } for (int i = 0; i<result.size();i++) { cout<<result[i]<<"\n"; } return 0; } |
|
|