ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1007. Code Words

PLZ HELP! (WA#1)
Posted by v13 [Kungur] 30 Oct 2018 16:07
My program works with tests like this:

4

0 00 0

10 11

And my program give right answers but WA#1. That my code, whats wrong??

#include <bits/stdc++.h>

using namespace std;

int n;
string ans,q;

int main()
{
cin >> n;

while (!cin.eof()) {                                     //while not eof reading
getline(cin,q);                                      //read 1 line
ans = "";

for (int i = 0; i < q.length(); i++)                 //remove all except 0 and 1
if (q[i] == '0' || q[i] == '1')
ans += q[i];

if (ans.length() < n-1 || ans.length() > n+1)         //if its empty continue
continue;

int pos_sum = 0;

for (int i = 0; i < ans.length(); i++) {              //count sum of positions
if (ans[i] == '1') {
pos_sum += i+1;
}
}

if (ans.length() > n) {                              //if there 1 unnecessary element check all
bool fg = false;                                 //elements and remove him

for (int i = 0; i < ans.length(); i++) {
int loc_pos_sum = 0;

if (fg) {
cout << ans[i];
continue;
}

for (int j = 0; j < ans.length(); j++) {
if (j == i)
continue;

if (ans[j] == '1')
loc_pos_sum += j+1 - int(j >= i);
}

if (loc_pos_sum % (n+1) == 0) {
fg = true;
} else {
cout << ans[i];
}
}
} else if (ans.length() < n) {                                 //if there 1 removed element
bool fg = false;                                           //check all positions for him

for (int i = 0; i <= ans.length(); i++) {
int loc_pos_sum = 0;

if (fg) {
cout << ans[i];
continue;
}

for (int j = 0; j < ans.length(); j++) {
if (ans[j] == '1') {
loc_pos_sum += j+1 + int(j >= i);
}
}

if (loc_pos_sum % (n+1) == 0) {
cout << 0;
fg = true;
}

if ((i+1+loc_pos_sum) % (n+1) == 0) {
cout << 1;
fg = true;
}

cout << ans[i];
}
} else {                                                          //if '0' replaced to '1' check all
bool fg = false;                                              //elements and replace
for (int i = 0; i < ans.length(); i++) {
if (ans[i] == '1' && (pos_sum-i-1)%(n+1) == 0 && !fg) {
cout << 0;
fg = true;
} else {
cout << ans[i];
}
}
}

cout << "\n";
}
return 0;
}

Edited by author 30.10.2018 16:07