博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2503-Babelfish
阅读量:4320 次
发布时间:2019-06-06

本文共 2167 字,大约阅读时间需要 7 分钟。

这是一道字符串处理的题。有点写字典,查字典的意思,首先要解决的是输入字典和查字典时输入单词中间的那

一个空行,我是用gets输入前面的东西,所以只需一个判断就可以跳出输入字典的循环。然后是输入单词翻译,

因为字典当中的单词比较多,我又不会hash,所以用了二分查找。

#include
#include
#include
#define M 100003 typedef struct dictionary {
char f[15]; char n[15]; }D; D dic[M]; /* 按照后一个单词的字典序升序排序 */ int cmp( const void *_p, const void *_q) {
D *q = (D *)_q; D *p = (D *)_p; return strcmp( p->n, q->n); } int main() {
char s[30]; int i = 0; while( gets(s) != NULL) {
if( s[0] == '\0') break; //结束字典的输入 sscanf( s, "%s%s", dic[i].f, dic[i].n); i ++; } qsort( dic, i, sizeof( dic[0]), cmp); int x, y; while( gets(s) != NULL) {
x = 0, y = i; //让x,y分别等于第一个和最后一个下标 while( x < y) //二分查找 {
int m = x + (y - x) / 2; if( strcmp( dic[m].n, s) == 0) {
printf( "%s\n", dic[m].f); break; } else if( strcmp( dic[m].n, s) > 0) y = m; else x = m + 1; } if( x >= y) printf( "eh\n"); } return 0; }

 

今天看了字典树原理,顺便将这道题再写了一遍。

(字典树)

字典树的基本功能是用来查询某个单词(前缀)在所有单词中出现次数的一种数据结构,

它的插入和查询复杂度都为O(len),Len为单词(前缀)长度,但是它的空间复杂度却非

常高,如果字符集是26个字母,那每个节点的度就有26个,典型的以空间换时间结构。

/*Accepted    13604K    235MS    C++    904B    2012-08-02 10:33:31*/#include
#include
#include
typedef struct{ int next[26]; char eng[11];}Trie;Trie t[200000];char en[11], fr[11];int tp;void insert(char *x, int site){ if(*x) { if(!t[site].next[*x - 'a']) t[site].next[*x - 'a'] = ++ tp; insert(x + 1, t[site].next[*x - 'a']); } else strcpy(t[site].eng, en);}void search(char *x, int site){ if(*x) { if(!t[site].next[*x - 'a']) { printf("eh\n"); return; } search(x + 1, t[site].next[*x - 'a']); } else printf("%s\n", t[site].eng);}int main(){ char s[25]; while(gets(s) && s[0] != '\0') { sscanf(s, "%s%s", en, fr); insert(fr, 0); } while(scanf("%s", fr) == 1) search(fr, 0); return 0;}

转载于:https://www.cnblogs.com/Yu2012/archive/2011/11/03/2234375.html

你可能感兴趣的文章
银行客户流失预测
查看>>
PDA手持扫描资产标签,盘点完成后将数据上传到PC端,固定资产系统查看盘点结果...
查看>>
[deviceone开发]-do_ImageView实现正圆的示例
查看>>
sql over(partition by) 开窗函数的使用
查看>>
100以内与7有关的数
查看>>
修改数据表中某字段的信息
查看>>
Mac下terminal中无法显示中文的问题
查看>>
1-库表查看及常用数据类型
查看>>
第六次java作业
查看>>
BNUOJ 4101(线性表的插入与删除)
查看>>
关于C++动态数组的若干问题
查看>>
Why Normal--牛人都干啥去了?
查看>>
[转载]MySQL concat函数的使用
查看>>
go: writing stat cache:, permission denied
查看>>
Xcode7.0.1(ios9)的部分适配问题
查看>>
hdu---(2604)Queuing(矩阵快速幂)
查看>>
关于Easyui的窗口和tab页面不执行JS的原因说明
查看>>
机器学习入坑指南(七):机器学习的知识结构
查看>>
又是企鹅惹的祸
查看>>
基于jQuery点击圆形边框弹出图片信息
查看>>