pku2503 Babelfish
生活随笔
收集整理的這篇文章主要介紹了
pku2503 Babelfish
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
額,跟hdu的1075很像,不過比那題簡單多了,就是讀入時有點麻煩,用到了一個函數(shù)
具體看代碼吧,這題沒什么,不過我的內(nèi)存開了太多了,不解……
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node {node *next[26];int v;char str[15]; }*tree,t; tree root; void insert(char *s,char *s1) {tree p=root,newnode;for(;*s1!='\0';s1++){int d=*s1-'a';if(p->next[d]!=NULL)p=p->next[d];else {newnode=(tree)malloc(sizeof(t));for(int i=0;i<26;i++)newnode->next[i]=NULL;newnode->v=-1;newnode->str[0]=0;p->next[d]=newnode;p=newnode;}}p->v=1;strcpy(p->str,s); } void search(char *s) {tree p=root;for(;*s!='\0';s++){int d=*s-'a';if(p->next[d]!=NULL)p=p->next[d];}if(p->v==1)puts(p->str);else printf("eh\n");return ;} int main() {char s1[15],s2[15],s3[15];char ch[25];root=(tree)malloc(sizeof(t));for(int i=0;i<26;i++)root->next[i]=NULL;root->v=-1;root->str[0]=0;while(gets(ch)!=NULL && ch[0]) {sscanf(ch, "%s%s", s1, s2);//用到了這個函數(shù),簡化了許多insert(s1, s2);}while(gets(s3)){if(strcmp(s3,"")==0)break;//puts(s3);search(s3);}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/nanke/archive/2011/05/14/2046189.html
總結(jié)
以上是生活随笔為你收集整理的pku2503 Babelfish的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下获取时间的函数
- 下一篇: 一般做一次输卵管造影的费用是多少钱?