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 1067. Disk Tree

Help! Crash (stack overflow) on TEST 10
Posted by Exia 6 Feb 2012 18:37
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
struct node
{
    int old,child,xiong,last;
}tree[200000];
char word[200000][10],s[110];
char w[20];
int how=0;
int child[20000];
int find(int lao,char wo[20])
{
    for (int i=1;i<=how;i++)
        if (strcmp(word[i],wo)==0 && tree[i].old==lao) return i;
    how++;
    strcpy(word[how],wo);
    return how;
}
void qsort(int l,int r)
{
    int i=l,j=r;
    char x[20]="";
    strcpy(x,word[child[(l+r)/2]]);
    do{
        while (strcmp(word[child[i]],x)<0) i++;
        while (strcmp(x,word[child[j]])<0) j--;
        if (i<=j)
        {
            int t=child[i]; child[i]=child[j]; child[j]=t;
            i++; j--;
        }
    }while (i<=j);
    if (i<r) qsort(i,r);
    if (l<j) qsort(l,j);
    return;
}
void search(int kg,int x)
{
    int a[20000];
    if (x!=0)
    {
        for (int i=1;i<=kg;i++) printf(" ");
        printf("%s\n",word[x]);
    }
    memset(a,0,sizeof(a));
    memset(child,0,sizeof(child));
    int y=tree[x].child;
    while (y!=-1)
    {
        a[0]++;
        a[a[0]]=y;
        y=tree[y].xiong;
    }
    for (int i=1;i<=a[0];i++) child[i]=a[i];
    qsort(1,a[0]);
    for (int i=1;i<=a[0];i++) a[i]=child[i];
    for (int i=1;i<=a[0];i++)
        search(kg+1,a[i]);
}
int main()
{
    int t;
    memset(tree,255,sizeof(tree));
    memset(word,0,sizeof(word));
    scanf("%d",&t);
    getchar();
    while (t-->0)
    {
        memset(s,0,sizeof(s));
        gets(s);
        s[strlen(s)]='\\';
        int len=-1,old=0;
        int ss=strlen(s);
        for (int i=0;i<ss;i++)
        {
            if (s[i]!='\\')
            {
                len++;
                w[len]=s[i];
            }
            else if (s[i]=='\\')
            {
               int x;
               x=find(old,w);
               if (old!=-1)
               {
                     if (tree[x].old==-1)
                     {
                         tree[x].old=old;
                         if (tree[old].child==-1) tree[old].child=x;
                         else tree[tree[old].last].xiong=x;
                         tree[old].last=x;
                     }
               }
               old=x;
               len=-1;
               memset(w,0,sizeof(w));
            }
        }
    }
    search(-1,0);
    return 0;
}


Edited by author 06.02.2012 18:42