zoukankan      html  css  js  c++  java
  • poj 2503 Babelfish

    题目就是间接的让你翻译单词的,输入的每一行有两个单词,其中有一个可以认为是字典,另一个就是要翻译成的单词了

    可能是我想的复杂了,我是用trie树做的。先定义一个结构体,存储输入的字符串  

     1 #include<stdio.h>
    2 #include<string.h>
    3 #include<iostream>
    4 #define N 20
    5 #define M 100001
    6 using namespace std;
    7 struct node
    8 {
    9 int k;
    10 node *key[26];
    11 node()
    12 {
    13 for(int i=0;i<26;i++)
    14 key[i]=NULL;
    15 k=0;
    16 }
    17 };
    18 struct str
    19 {
    20 char s[N];
    21 char b[N];
    22 }a[M];
    23 node *root;
    24 void insert(char *S,int n)
    25 {
    26 int i,len=strlen(S),t;
    27 node *p;
    28 p=root;
    29 for(i=0;i<len;i++)
    30 {
    31 t=S[i]-'a';
    32 if(p->key[t] != NULL)
    33 p = p->key[t];
    34 else
    35 {
    36 p->key[t] = new node;
    37 p = p->key[t];
    38 }
    39 }
    40 p->k=n; //这里将字符串的位置标记下来,便于查找的时候输出另个单词
    41 }
    42 int find(char *S)
    43 {
    44 int i,len = strlen(S),t;
    45 node *p;
    46 p = root;
    47 for(i = 0;i < len;i ++)
    48 {
    49 t = S[i] - 'a';
    50 if(p->key[t] != NULL) p = p->key[t];
    51 else return -1;
    52 }
    53 return p->k;
    54 }
    55 int main()
    56 {
    57 int i;
    58 root = new node; //这里也要注意,老是忘记申请空间
    59 char t[100];
    60 i=0;
    61 while(gets(t)&&strcmp(t,""))
    62 {
    63 sscanf(t,"%s%s",a[i].s,a[i].b);
    64 //cout<<a[i].s<<a[i].b;
    65 insert(a[i].b,i);
    66 i++;
    67 }
    68 while(cin>>t)
    69 {
    70 //cout<<t<<endl;
    71 int ans=find(t);
    72 if(ans==-1) cout<<"eh\n";
    73 else cout<<a[ans].s<<endl;
    74 }
    75 return 0;
    76 }


    然后建立trie树,查找就行了

  • 相关阅读:
    Cs231n课堂内容记录-Lecture1 导论
    Linux实时查询GPU使用命令
    导出excel的简单方法
    正则表达式的一些应用
    4、Python语法之变量
    javaScript中对typeof 和 instanceof 的使用及理解
    JavaScrpt 变量作用域
    真香的flex弹性布局
    css调试与样式优先级
    css3新样式
  • 原文地址:https://www.cnblogs.com/fxh19911107/p/2380887.html
Copyright © 2011-2022 走看看