ZOJ 1109 Language of FatMouse

来源:互联网 发布:微信支付网页授权域名 编辑:程序博客网 时间:2024/05/19 23:02
//用map写的,不用的话也可以排序后二分查找//map #include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<map>using namespace std;map<string,string>entry;map<string,string>::iterator location,pos;int main(){    char line[30];    char english[11],mouse[11];    string value,key;    while(gets(line))    {        if(strlen(line)==0) break;        sscanf(line,"%s%s",english,mouse);        key = mouse;        value = english;        entry[key] = value;    }    while(cin>>line)    {       location = entry.find(line);       if(location != entry.end())          cout<<entry[line]<<endl;       else  cout<<"eh"<<endl;     }   return 0;}//字典树//http://www.cnblogs.com/DreamUp/archive/2010/07/23/1783410.html#include <stdio.h>#include <stdlib.h>#include <iostream>#include <string.h>#define MAX 100006using namespace std;typedef struct Trie{Trie *child[26];int data;}Trie;Trie node[120000];  // I hate it!!! The size of the node should biger than 100006 = =. WA N times.int cou;void init(){cou = 1;memset(node,'/0',sizeof(node));}void Build( char *a, int num)  //Build the trie, magic ~{Trie *head = &node[0];int len = strlen(a);for(int i=0; i<len; i++){if( head->child[a[i]-'a'] == NULL )head->child[a[i]-'a'] = &node[cou++];head = head->child[a[i]-'a'];}head->data = num;}int Find( char *a )  // Find the data in the trie.{Trie *head = &node[0];int len = strlen(a);for(int i=0; i<len; i++){head = head->child[a[i]-'a'];if( head == NULL )  return 0;}return head->data;}char eng[MAX][50],mice[MAX][50];char tofind[50];char str[50];int main(void){int count = 1;init();while( gets(str) && strlen(str) ){sscanf(str,"%s%s",eng[count],mice[count]); // = = . Fire station 's input format...count++;}for( int i=1; i<count; i++ )Build( mice[i], i );while( scanf("%s",tofind) != EOF ){int ind = Find( tofind );if( ind == 0 )printf("eh\n");elseprintf("%s\n",eng[ind]);}return 0;}/*错误代码#include<map>#include<string>#include<cstring>#include<cstdio>#include<iostream>using namespace std;int main(){    char a[50];    map<char*,char*>tt;    char s1[20],s2[20];    while(gets(a))    {        int i,j,k;        if(strcmp(a,"\n")==0)break;//strcm返回值:a>b >0;  a=b 0;  a<b  <0        if(a[0]==0)break;//即空字符NUL(null)        k=0;        for(i=0;i<strlen(a);i++)        {            if(a[i]==' ')break;            s1[k++]=a[i];        }        s1[k]='\0';        k=0;        for(j=i+1;j<strlen(a);j++)        {            s2[k++]=a[j];        }        s2[k]='\0';        tt[s2]=s1;    }    while(scanf("%s",s2)!=EOF)    {        if(tt[s2]==NULL)printf("eh\n");        else printf("%s\n",tt[s2]);    }    return 0;}*/

0 0
原创粉丝点击