Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

字典树1A,贴个代码,不喜勿喷== 输入

Posted by blackRats at 2013-08-01 01:51:31 on Problem 2503
#include <cstdio>
#include <cstring>

struct Node
{
    int next[26];
    char str[12];
} dic[200000];
int index;

void insert(char* s,char* t)
{
    int len=strlen(s);
    int now=0;
    for(int i=0;i<len;i++)
    {
        int next=s[i]-'a';
        if(dic[now].next[next]==0)
            dic[now].next[next]=++index;
        now=dic[now].next[next];
    }
    memcpy(dic[now].str,t,strlen(t));
}

void query(char* s)
{
    int len=strlen(s);
    int now=0;
    for(int i=0;i<len;i++)
    {
        int next=s[i]-'a';
        if(dic[now].next[next]==0)
        {
            printf("eh\n");
            return;
        }
        now=dic[now].next[next];
    }
    printf("%s\n",dic[now].str);
}

int main()
{
 //   freopen("in.txt","r",stdin);
    char str[30];
    while(gets(str),str[0])
    {
        int pos=0;
        while(str[pos++]!=' ');
        str[pos-1]=0;
        insert(str+pos,str);
    }
    while(gets(str))
        query(str);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator