首页 > 其他 > 详细

HDU 1075 What Are You Talking About

时间:2014-02-20 06:45:07      阅读:299      评论:0      收藏:0      [点我收藏+]

第一次码字典树,各种辛苦……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdio>
#include <cstring>
using namespace std;
struct node{int son[26]; char hash[12];}trie[1000000];
int cnt,n;
char s[100000],s1[12],ans[12];
void insert(char *s,char *s1){
    for(int l=strlen(s),x=0,i=0;i<l;i++){
        if(!trie[x].son[s[i]-‘a‘])trie[x].son[s[i]-‘a‘]=++cnt;
        x=trie[x].son[s[i]-‘a‘];
        if(i==l-1)strcpy(trie[x].hash,s1);
    }
}
int find(char *s){
    for(int l=strlen(s),x=0,i=0;i<l;i++){
        if(!trie[x].son[s[i]-‘a‘])return 0;
        x=trie[x].son[s[i]-‘a‘];
        if(i==l-1){
            strcpy(ans,trie[x].hash);
            return strlen(ans)?1:0;
        }
    }
}
int main(){
    while(~scanf("%s",&s)){
        if(strcmp(s,"START")==0)continue;
        if(strcmp(s,"END")==0)break;
        scanf("%s",&s1);
        insert(s1,s);
    }
    gets(s);
    while(gets(s)){
        if(strcmp(s,"START")==0)continue;
        if(strcmp(s,"END")==0)break;
        int i=0,j=0,len=strlen(s);
        while(i<len){
            if(s[i]<‘a‘||s[i]>‘z‘){
                if(find(s1))printf("%s",ans);
                else printf("%s",s1);
                printf("%c",s[i++]);
                j=0;
                memset(s1,0,sizeof s1);
                memset(ans,0,sizeof ans);
            }else s1[j++]=s[i++];
        }
        printf("\n");
    }
    return 0;
}

HDU 1075 What Are You Talking About

原文:http://www.cnblogs.com/forever97/p/3556552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!