ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1007. Code Words

What can be wrong here? Help me, if you can...
Posted by Denis 18 Jan 2008 02:00
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int n;
string S;
int arr[1010] = {0};
char s[1010];

int main ()
{
    //freopen ("a.in", "r", stdin);
    //freopen ("a.out", "w", stdout);
    int i, j, k, t, sum;
    int tsum;
    scanf ("%d", &n);
    i = 0;
    S = "";
    char c;
    while (scanf ("%c", &c) == 1)
    {
        //cin>>S;
        //cout<<i<<" ";
        if (c == '\n' && S != "")
        {
            //S = "";
            /*for (j = 0; s[j] > 0; ++j)
            {
                if (s[j] == '0' || s[j] == '1')
                {
                    S += s[j];
                }
            }*/
            if (i > 0)
            {
                cout<<endl;
            }
            ++i;
            sum = 0;
            tsum = 0;
            if (S.length() == n)
            {
                for (j = 0; j < n; ++j)
                {
                    if (S[j] == '1')
                    {
                        sum += (j+1);
                    }
                }
                sum %= (n+1);
                if (sum == 0)
                {
                    cout<<S;
                    S = "";
                }
                else
                {
                    S[sum-1] = '0';
                    //S = "";
                    cout<<S;
                    S = "";
                }
            }
            else if (S.length() > n)
            {
                t = 0;
                for (j = 0; j < S.length(); ++j)
                {
                    if (S[j] == '1')
                    {
                        sum += (j+1);
                        ++t;
                    }
                    arr[j] = sum%(n+1);
                }
                sum %= (n+1);
                /*for (j = S.length()-1; j >= 0; --j)
                {
                    if (S[j] == '1')
                    {
                        tsum += (j+1);
                    }
                    arr[j][1] = tsum%(n+1);
                }*/
                if ((sum+n+1-t)%(n+1) == 0)
                {
                    S = S.substr (1, n);
                    cout<<S;
                    S = "";
                    continue;
                }
                t = 0;
                tsum = 0;
                for (j = S.length()-1; j > 0; --j)
                {
                    if ((tsum-t+arr[j-1])%(n+1) == 0)
                    {
                        for (k = 0; k < j; ++k)
                        {
                            cout<<S[k];
                        }
                        for (k = j+1; k < S.length(); ++k)
                        {
                            cout<<S[k];
                        }
                        //cout<<endl;
                        //continue;
                        S = "";
                        break;
                    }
                    if (S[j] == '1')
                    {
                        ++t;
                        tsum += (j+1);
                    }
                }
            }
            else
            {
                sum = 0, tsum = 0, t = 0;
                int add;
                for (j = 0; j < S.length(); ++j)
                {
                    if (S[j] == '1')
                    {
                        sum += (j+1);
                    }
                    arr[j] = sum%(n+1);
                }
                sum %= (n+1);
                //arr[n-1] = sum;
                if (sum == 0)
                {
                    cout<<S<<0;
                    S = "";
                    continue;
                }
                else if ((sum+n)%(n+1) == 0)
                {
                    cout<<S<<1;
                    S = "";
                    continue;
                }
                for (j = S.length()-1; j >= 0; --j)
                {
                    if ((arr[j]+tsum+t)%(n+1) == 0)
                    {
                        for (k = 0; k <= j; ++k)
                        {
                            cout<<S[k];
                        }
                        cout<<0;
                        for (k = j+1; k < S.length(); ++k)
                        {
                            cout<<S[k];
                        }
                        //cout<<endl;
                        S = "";
                        break;
                    }
                    if ((arr[j]+tsum+t+j+1)%(n+1) == 0)
                    {
                        for (k = 0; k <= j; ++k)
                        {
                            cout<<S[k];
                        }
                        cout<<1;
                        for (k = j+1; k < S.length(); ++k)
                        {
                            cout<<S[k];
                        }
                        S = "";
                        //cout<<endl;
                        break;
                    }
                    if (S[j] == '1')
                    {
                        ++t;
                        tsum += (j+1);
                    }
                }
            }
            S = "";
        }
        else
        {
            if (c == '0' || c == '1')
            {
                S += c;
            }
        }
    }
    return 0;
}