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

Help me please... why WA 1?
Posted by Winner 19 Oct 2008 20:18
var
n: integer;
s: string;

function Go(s: string): string;
var
sum: integer;
i: integer;
j: integer;
stmp: string;
tmp: integer;
begin
s:=trim(s);

sum:=0;
for i:=1 to length(s) do
if s[i]='1' then sum:=sum+i;

if (length(s)=n) and (sum mod (n+1) = 0) then
begin
result:=s;
exit;
end;

if (length(s)=n-1) then
begin
if sum mod (n+1) = 0 then
result:=s+'0' else
result:=s+'1';
exit;
end;

if (length(s)=n+1) then
begin
for i:=1 to length(s) do
begin
stmp:=s;
Delete(stmp,i,1);
tmp:=0;

for j:=1 to length(stmp) do
if stmp[j]='1' then tmp:=tmp+j;

if tmp mod (n+1) = 0 then
begin
result:=stmp;
exit;
end;
end;
end;
for i:=length(s) downto 1 do
if (s[i]='1') then
if
((sum-i) mod (n+1) = 0)
then begin s[i]:='0'; break; end else
else if ((sum+i) mod (n+1) = 0)
then begin s[i]:='1'; break; end;
result:=s;
end;

begin
//  assign(input,'input.txt'); reset(input);

while not eof do
begin

writeln(Go(s));
end;

// sleep(12121);
end.
Re: Help me please... why WA 1?
Posted by Marc 2 Mar 2010 02:05

#include <iostream>
#include <string>

using namespace std;

short N;
char word[2001][1001];

int main(){
int i=0,j,n,s=0,c=0,temp;
cin>>N;
while(cin>>word[i])i++;
n=i;
for(i=0;i<n;i++){
////////////////////// CASE 1 (REPLACED)//////////////////////
if(strlen(word[i])==N){
for(j=0;j<N;j++)
if(word[i][j]=='1')s+=j+1;
s%=(N+1);
word[i][s-1]='0';
}
////////////////////// CASE 2 (REMOVED)//////////////////////
else if(strlen(word[i])==N-1){
for(j=0;j<N-1;j++)
if(word[i][j]=='1'){
s+=j+1;
c++;
}
s%=(N+1);
if(s!=0)s=N+1-s;
if(s<=c){    // Removed '0'
for(j=N-2;s!=0;j--){
word[i][j+1]=word[i][j];
if(word[i][j]=='1')s--;
}
word[i][j+1]='0';
}
else {    // Removed '1'
short mod=s;
c=0;
for(j=s;j<N-1;j++)
if(word[i][j]=='1')c++;
while(s+c!=mod){
s--;
if(word[i][s-1]=='1')c++;
}
for(j=N-1;j>=s;j--)word[i][j]=word[i][j-1];
word[i][s-1]='1';
}
}
////////////////////// CASE 3 (INSERTED)//////////////////////
else if(strlen(word[i])==N+1){
for(j=0;j<N+1;j++)
if(word[i][j]=='1')s+=(j+1);
s%=(N+1);
for(j=N;j>=0 && c!=s;j--)
if(word[i][j]=='1')c++;
if(j>=0 && word[i][j]=='0'){ // Inserted '0'
for(;j<N;j++)word[i][j]=word[i][j+1];
word[i][j]=0;
}
else {    // Inserted '1'
if(s==0)s=N+1;
short mod=s;
c=0;
for(j=s;j<N+1;j++)
if(word[i][j]=='1')c++;
while(s+c!=mod){
s--;
if(word[i][s]=='1')c++;
}
for(j=s-1;j<N+1;j++)word[i][j]=word[i][j+1];
word[i][j]=0;
}
}
s=c=0;
}
for(i=0;i<n;i++)cout<<word[i]<<endl;
return 0;
}