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 1002. Phone Numbers

My program(for 1002) can pass all test data from CEOI99 but it doesn't work here ?
Posted by linda 10 Dec 2001 00:24

/* Phone Number
 * from http://acm.timus.ru/problem.asp?id=1002
 * written by Skywind Lin
 *
 *    1 ij    2 abc    3 def
 *    4 gh    5 kl    6 mn
 *    7 prs    8 tuv    9 wxy
 *    0 oqz
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char letter[26]={ 2,2,2, 3,3,3, 4,4, 1,1, 5,5, 6,6, 0,7,0,7,7,8,
8,8,9,9,9,0 };

#ifndef ONLINE_JUDGE
#define DEBUG_TEST
#define MAX 10000
#else
#define MAX 50000
#endif

char num[101],len;
int dcount;
typedef struct NODE
{
    short index;
    char len;
    unsigned int dynamic;
    struct NODE *dynamic_link;
    struct NODE *next;
} word;
word *words[100];
int result[100];

#define HASH_LEN 256
#define HASH(x) (x&(HASH_LEN-1))
typedef struct NODES
{
    char *word;
    unsigned short index;
    struct NODES *next;
} strings;
strings *htable[HASH_LEN];

FILE *fin;

int load(void)
{
    int h,i,j,k,f,slen;
    char line[200];
    char temp[51];
    long offset;
    word *nw;
    strings *ns,*sb,*sp;
    #ifdef DEBUG_TEST
    fscanf(fin,"%s",line);
    #else
    scanf("%s",line);
    #endif
    slen=strlen(line);
    if (len>100)
    {
        printf("Number len error\n");
        return -1;
    }
    result[0]=-1;
    dcount=0;
    len=0;
    for (i=0;i<100;i++) words[i]=NULL;
    for (i=0;i<HASH_LEN;i++) htable[i]=NULL;
    if (line[0]=='-'&&line[1]=='1') return 0;
    for (i=0,len=slen,num[len]=255;i<len;i++) num[i]=line[i]-
'0';
    #ifdef DEBUG_TEST
    fscanf(fin,"%d",&dcount);
    #else
    scanf("%d",&dcount);
    #endif
    for (j=0;j<dcount;j++)
    {
        #ifdef DEBUG_TEST
        fscanf(fin,"%s",temp);
        #else
        scanf("%s",temp);
        #endif
        slen=strlen(temp);
        for (i=0;i<slen;i++)
            k=temp[i],k=(k>='A'&&k<='Z')?(k-'A'):(k-
'a'),line[i]=letter[k];
        if (slen<=len)
        {
            for (k=0;k<=len-slen;k++)
            {
                for (i=0;num[k+i]==line[i];i++);
                if (i>=slen) /* find match */
                {
                    nw=(word*)malloc(sizeof
(word)); /* insert word data */
                    if (!nw) return -3;
                    nw->index=j;
                    nw->len=slen;
                    nw->dynamic=MAX;
                    nw->dynamic_link=NULL;
                    nw->next=words[k];
                    words[k]=nw;
                    h=HASH(j);
                    for (sb=sp=htable
[h];sp;sb=sp,sp=sp->next) if (sp->index>=j) break;
                    f=0; if (sp) if (j<sp-
>index) f=1;
                    if (!sp) f=1;
                    if (f)
                    {
                        ns=(strings*)
malloc(sizeof(strings));
                        if (!nw)
return -4;
                        ns->word=
(char*)malloc(slen+1);
                        if (!ns->word)
{ free(ns); return -5; }
                        strcpy(ns-
>word,temp);
                        ns->index=j;
                        ns->next=sp;
                        if (sb) sb-
>next=ns;
                        else htable[h]
=ns;
                    }
                }
            }
        }
    }
    return 1;
}

void dsearch(void)
{
    int i,j;
    unsigned int k;
    word *p,*t;
    for (i=len-1;i>=0;i--)
        for (p=words[i];p;p=p->next)
        {
            p->dynamic_link=NULL;
            if (i+p->len==len) p->dynamic=1; /* last
word */
            else {
                for (k=MAX,t=words[i+p-
>len];t;t=t->next)
                    if (t->dynamic<k) k=t-
>dynamic, p->dynamic_link=t;
                p->dynamic=k+1;
            }
        }
    for (p=t=words[0],k=MAX;p;p=p->next)
        if (p->dynamic<k) k=p->dynamic,t=p;
    if (t==NULL||k>=MAX) result[0]=-1;
    else {
        for (i=0;t&&i<len;t=t->dynamic_link,i++) result[i]
=t->index;
        result[i]=-1;
    }
}

char *rtable(unsigned int index)
{
    int hash=HASH(index);
    strings *p;
    for (p=htable[hash];p;p=p->next) if (index<=p->index) break;
    if (!p) return NULL;
    if (p->index>index) return NULL;
    return p->word;
}

int main(void)
{
    int ok,i,j,index;
It always get the wrong answer
Posted by linda 10 Dec 2001 00:26