POJ2503(有delete函数)
来源:互联网 发布:matlab 结构体数组 编辑:程序博客网 时间:2024/06/09 20:17
//字典树的一个应用#include <iostream>#include<string.h>#include<stdio.h>#include<cstdlib>#include<string>#include<cstring>using namespace std;const int maxnum=26;struct trie{ bool flag; trie *next[maxnum]; char translation[30];};trie *root;void init(){ root=(trie *)malloc(sizeof(trie)); for(int i=0;i<maxnum;i++) { root->next[i]=NULL; } root->flag=false;}void insert(char *word,char *t){ int i; trie *temp=root; while(*word!='\0') { if(temp->next[*word-'a']==NULL) { trie *cur=(trie *)malloc(sizeof(trie)); for(i=0;i<maxnum;i++) cur->next[i]=NULL; temp->next[*word-'a']=cur; cur->flag=false; } temp=temp->next[*word-'a']; word++; } temp->flag=true; strcpy(temp->translation,t); // memcpy(temp->translation,t,strlen(t));//之前因为这个函数WA了很多发}void query(char *word){ int i; //cout<<"hah"<<endl; trie *temp=root; for(i=0;word[i]!='\0';i++) { if(temp==NULL||temp->next[word[i]-'a']==NULL) { printf("eh\n"); return ; } temp=temp->next[word[i]-'a']; } //cout<<temp->flag<<"zheng"<<endl; if(temp->flag==true) { printf("%s\n",temp->translation); } else printf("eh\n");}void Delete(trie *root){ for(int i = 0; i < 26; i++) if(root->next[i] != NULL) Delete(root->next[i]); delete root;}int main(){ char s1[30],s[30],s2[30]; init(); while(gets(s),s[0]) { int pos=0,k=0; while(s[pos++]!=' ') s1[pos-1]=s[pos-1]; s1[pos-1]='\0'; //cout<<pos<<endl; for(int i=pos;s[i]!='\0';i++) s2[k++]=s[i]; s2[k]='\0'; insert(s2,s1); } while(scanf("%s",s)!=EOF) { //cout<<"haha"<<endl; query(s); } Delete(root); return 0;}
//看看字典树就知道了
下面主要来看下memcpy这个函数
c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
函数原型
void *memcpy(void *dest, const void *src, size_t n);
按道理来说应该是没问题的呀,想不通
按道理来说应该是没问题的呀,想不通
0 0
- POJ2503(有delete函数)
- 函数 qsort 和 bsearch 的用法(poj2503)
- POJ2503 Babelfish(hash)
- POJ2503--(STLmap应用)
- poj2503(简单hash)
- poj2503(字符串hash)
- POJ2503 Babelfish (map)
- POJ2503
- poj2503
- poj2503
- POJ2503
- poj2503
- poj2503
- poj2503
- poj2503
- poj2503
- poj2503
- poj2503
- iOS开发-AFNetworking之上传图片音乐文件
- arm-linux-gcc:没有那个文件或目录
- 《MonkeyRunner原理剖析》第九章-MonkeyImage实现原理 - 第二节 - ADB截屏服务
- 一篇ACM奋斗史
- Ajax介绍
- POJ2503(有delete函数)
- 一个失败者的自我反省
- Mysql启动失败问题解决
- JavaScript快速入门(三)——JavaScript语句
- mysql用户创建及权限分配
- GC学习笔记
- Objective - C基础: 第四天 - 7.类的加载过程
- sqlserver命令行操作
- Ibox 4412 重启 usb不识别 解决