pages:
  • 1
AhUrA SAYS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>
#include <map>
using namespace std;

struct MyData
{
    char str[2000];
    deque<char*> dq;

    void calc()
    {
        char *p = strtok(str, " ");
        while ( p ) {
            dq.push_back(p);
            p = strtok(NULL, " ");
        }
    }

    void erase(const char* name) {
        dq.erase(remove_if(dq.begin(), dq.end(), not1(bind2nd(ptr_fun(strcmp), name))), 
            dq.end());
    }

    void clear() {
        dq.clear();
    }
};

bool myOpr(const char* l, const char* r)
{
    return (strcmp(l, r) < 0);
}

int main()
{
    freopen("in.txt", "r", stdin);

    int candidates, votes;
    static MyData myarr[1000];

    map<char*, int, bool(*)(const char*, const char*)> mp(myOpr);

    while ( scanf("%i %i", &candidates, &votes) != EOF && candidates && votes )
    {
        getchar();

        for (int i = 0; i < votes; ++i) {
            myarr[i].clear();

            gets(myarr[i].str);
            myarr[i].calc();
        }

        while ( true )
        {
            int emptyVotes = 0;
            mp.clear();

            for (int i = 0; i < votes; ++i) 
            {
                if ( myarr[i].dq.empty() ) {
                    ++emptyVotes;
                    continue;
                }

                ++mp[myarr[i].dq.front()];
                myarr[i].dq.pop_front();
            }

            int mx = -1, mn = 900000000;
            map<char*, int, bool(*)(const char*, const char*)>::iterator mItr;

            for (mItr = mp.begin(); mItr != mp.end(); ++mItr) {
                if ( mItr->second > mx )
                    mx = mItr->second;
                if ( mItr->second < mn )
                    mn = mItr->second;
            }

            const char* ans;
            int counter = 0;

            for (mItr = mp.begin(); mItr != mp.end(); ++mItr) {
                if ( mItr->second == mx ) {
                    ans = mItr->first;
                    ++counter;
                }

                if ( mItr->second == mn ) {
                    for (int i = 0; i < votes; ++i)
                        myarr[i].erase(mItr->first);
                }
            }           

            if ( votes == emptyVotes ) {
                puts("There is no winner");
                goto lbl;
            }

            if ( mx > (votes - emptyVotes) / 2 && counter == 1 ) {
                puts(ans);
                goto lbl;
            }
        }

lbl:;
    }

    return 0;
}

in code man hastesh aslan nemitunam motevajeh sham kojaye code man moshkel dare ! age kasi lotf kone va yek negahi be code man bendaze va komak kone besyar besyar mamnunesh misham . in maslale va inke daghighan moshkelesh kojast baraye man kheyly moheme

AMiR SAYS

Reading the whole code is a little hard! Please explain your algorithm or write some comments for you code first.

AhUrA SAYS

dadashi dorostesh kardam dastet dard nakone . rastesh enghadr STL mizanam ke khodamam bazi vaghta nemifahmam chi kardam !