zoukankan      html  css  js  c++  java
  • HDU 1057 What Are You Talking About trie树 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=1075

    题意 : 给一个单词表然后给一些单词,要求翻译单词表中有的单词,没有则直接输出原单词.
    翻译文段部分getchar()输入即可,需要仔细一点,但是很简单不恶心
    代码
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<queue>
     7 using namespace std;
     8 const int maxn=100010;
     9 const double eps=1e-8;
    10 const long long modn=1000;
    11 struct tri{
    12     bool exist;
    13     int next[26];
    14     char ch[20];
    15 }e[maxn*10];
    16 int tot=0;
    17 char a[20]={},b[20]={},cc;
    18 char st[20]={'S','T','A','R','T'};
    19 char ed[20]={'E','N','D'};
    20 void doit(int x,int k,int j){
    21     if(k<j){
    22         e[x].exist=1;
    23         strcpy(e[x].ch,b);
    24         return;
    25     }
    26     int z=a[j]-'a';
    27     if(e[x].next[z]){
    28         doit(e[x].next[z],k,j+1);
    29     }else{
    30         e[x].next[z]=++tot;
    31         doit(tot,k,j+1);
    32     }
    33 }
    34 bool getit(int x,int k,int j){
    35     if(k<j){
    36         if(e[x].exist){
    37             printf("%s",&e[x].ch);
    38             return 1;
    39         }
    40         return 0;
    41     }
    42     int z=a[j]-'a';
    43     if(e[x].next[z]){
    44         return getit(e[x].next[z],k,j+1);
    45     }else{
    46         return 0;
    47     }
    48 }
    49 int main(){
    50     while(scanf("%s",&b)){
    51         if(strcmp(b,st)==0){
    52             continue;
    53         }if(strcmp(b,ed)==0){
    54             break;
    55         }
    56         scanf("%s",&a);
    57         doit(0,strlen(a)-1,0);
    58     }
    59     scanf("%s",&b);
    60     a[0]=getchar();
    61     a[0]=getchar();
    62     int i=0;
    63     while(a[0]!='E'){
    64         while(a[i]<='z'&&a[i]>='a'){
    65             i++;
    66             a[i]=getchar();
    67         }
    68         cc=a[i];
    69         a[i]=0;
    70         if(i>0){
    71             if(!getit(0,i-1,0)){
    72                 for(int j=0;j<i;j++){
    73                     printf("%c",a[j]);
    74                 }
    75             }
    76         }
    77         printf("%c",cc);
    78         i=0; 
    79         a[0]=getchar();
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    简单计算器
    dfs(通过控制点的编号来得出每个点的坐标)
    dfs(通过控制点的编号来得出每个点的坐标)
    神奇的 组合数
    神奇的 组合数
    B. Light It Up
    B. Light It Up
    Python 字符串
    Python radians() 函数
    Python degrees() 函数
  • 原文地址:https://www.cnblogs.com/137shoebills/p/7786491.html
Copyright © 2011-2022 走看看