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 1542. Autocompletion

Help!!!!!!!!Wa on test 11!!!!!!!
Posted by Eazy jobb 31 May 2012 14:25
/*
My program got WA on test 11!!!
What's wrong with my code?
Anyone can help me!!!
*/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>

#define FI            first
#define SE            second
#define LSON(x)            (x<<1)
#define RSON(x)            ((x<<1)|1)
#define Benefit(x,y)        x=max(x,y)
#define CS            const static
#define SORT_CMP(s,l,r,cmp)    sort(s+(l),s+(r)+1,cmp)
#define SORT(s,l,r)        sort(s+(l),s+(r)+1)
#define MP(x,y)            make_pair(x,y)
#define Randomize        srand( (unsigned int) time ( NULL ) )
#define INOUT(x,y)        freopen(x,"r",stdin),freopen(y,"w",stdout)

using namespace std;

typedef char name[20];

CS int MaxN = 100010 ;

struct word
{
    name x;
    int val,len;
    word()    {x[0] = 0; val = 0;}
};

struct Trie
{
    Trie * next[26];
    word * ans[10];
    Trie()
    {
        for(int i=0;i<26;i++)    next[i] = NULL ;
        for(int i=0;i<10;i++)    ans[i] = NULL ;
    }
} * Tree;

word p[MaxN];

name ask;

int n,m,asklen;

void Insert(Trie * x,int _y,int pos)
{
    int i;    word & y = p[_y];
    for(i=0;i<10;i++)
        if((x->ans[i]==NULL)||(x->ans[i]->val<y.val))    break;
    if(i<10)
    {
        for(int j=9;j>i;j--)
            x -> ans[j] = x->ans[j-1];
        x -> ans[i] = p + _y;
    }
    if(pos!=y.len)
    {
        int u = y.x[pos] - 'a';
        if(!x->next[u])    x -> next[u] = new Trie ;
        Insert(x->next[u],_y,pos+1);
    }
}

void Init()
{
    scanf("%d\n",&n);    Tree = new Trie ;
    for(int i=1;i<=n;i++)
    {
        scanf("%s %d\n",p[i].x,&p[i].val);    p[i].len=strlen(p[i].x);
        Insert(Tree,i,0);
    }
}

void Query(Trie * x,int pos)
{
    if(pos == asklen)
    {
        for(int i=0;i<10;i++)
            if(x->ans[i])    printf("%s\n",x->ans[i]->x);
            else        break;
        return ;
    }
    int u = ask[pos] - 'a';
    if(x->next[u])    Query(x->next[u],pos+1);
}

void Solve()
{
    scanf("%d\n",&m);
    while(m--)
    {
        scanf("%s\n",ask);    asklen = strlen(ask) ;
        Query(Tree,0);    if(m)    printf("\n");
    }
}

int main()
{
    Init();    Solve();
    return 0;
}

Edited by author 31.05.2012 14:40
Re: Help!!!!!!!!Wa on test 11!!!!!!!
Posted by Nikolay 18 Nov 2014 21:40
you are an idiot. why so many inclusions? and your code is delirium.
Re: Help!!!!!!!!Wa on test 11!!!!!!!
...wrote Nikolay, 43 solved problems and 6000+-th place, to blablabla, 280 solved problems and place within top 1000.
P.S. And the code style is not great, but quite okay.

Edited by author 19.11.2014 12:32