码迷,mamicode.com
首页 > 其他好文 > 详细

百练OJ:2804词典

时间:2017-08-10 23:42:44      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:word   ops   城市   空行   ++   content   理解   lin   get   

题目是这样的:

2804:词典

总时间限制: 
3000ms
 
内存限制: 
65536kB
描述

你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。

输入
首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。
输出
在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。
样例输入
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
样例输出
cat
eh
loops
提示
输入比较大,推荐使用C语言的I / O函数。
输入文档时还好说,输入词典时就为了如何能在输出一个空行之后退出折磨了好长时间。。。刚开始用scanf输入,忘了字符串中间是有空格的,导致又是只接收了前半段,调试时才发现。。。换成gets吧,他后面又接不了getchar,因为那样的话回回得输入一个字符判断一下。。。闹了半天不知道怎么整,上网查一下别人的吧,发现主要矛盾并不是这里。。。人家都是用排序+二分做的,还都用到了一个奇怪的函数sscanf(),有点不解,我这么做不也行吗(线性查找),于是使用学到的判断遇到回车符退出的办法:在gets后面用if(!strcmp(temp,"")) break;这个,就很方便了!原来scanf会把回车符放在输入缓冲区里,需要用getchar吸收,而gets只会读取一行的数据,忽略掉换行符。结果终于本地过了,一提交,超时。。。我才意识到时间复杂度出了问题,那么庞大的数据,是不应该用线性查找的,转而二分查找,那就先得排序,然后才发现,我用strstr()找字符串的方法,根本不适合二分。。。跟谁去比啊。。于是,半拉可叽的代码终于写不下去了,,换算法,用个结构体定义吧。。.........

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char dic[100000][22];
char txt[100000][22];
bool cmp(char *x,char *y)
{
return strcmp(x,y)<0;
}
int main()
{
int cnt;
long i=0,j=0;
// char c;
while(gets(dic[i])){
if(strcmp(dic[i++],"")==0) break;
}
sort(dic,dic+i,cmp);
while(gets(txt[j])&&j<=100000){
//getchar();
int beg=0,end=i;
while(beg<=end){
int mid=(beg+end)/2;
if()
}
for(cnt=0;cnt<i;cnt++){
if(strstr(dic[cnt],txt[j])!=NULL){
*(strstr(dic[cnt],txt[j])-1)=‘\0‘;
break;
}
}
if(cnt>=i)
printf("eh\n");
else
printf("%s\n",dic[cnt]);
j++;
}
return 0;
}

以上是我的半拉可叽的代码,下面是AC的网友代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
 
#define MAX_WORD_LEN 11
#define MAX_DICTION_ITEM (100000 + 10)
 
using std::sort;
 
struct Dictionary
{
char szWord[MAX_WORD_LEN];
char szEnglish[MAX_WORD_LEN];
};
 
Dictionary diction[MAX_DICTION_ITEM];
 
bool CmpDictionItem(Dictionary one, Dictionary two)
{
return strcmp(one.szWord, two.szWord) < 0;
}
 
int FindEnglish(char* pszWord, int nItemNum)
{
int nBeg = 0, nEnd = nItemNum - 1;
int nCmp = 0;
 
while (nBeg <= nEnd)
{
int nMid = (nBeg + nEnd) / 2;
nCmp = strcmp(pszWord, diction[nMid].szWord);
if (nCmp == 0)
{
return nMid;
}
else if (nCmp < 0)
{
nEnd = nMid - 1;
}
else
{
nBeg = nMid + 1;
}
}
 
return -1;
}
 
int main()
{
char szStr[30];
char szWord[MAX_WORD_LEN];
int nCount = 0;
int nAnsItem = 0;
 
while (fgets(szStr, 29, stdin), szStr[0] != ‘\n‘)
{
sscanf(szStr, "%s%s", diction[nCount].szEnglish, diction[nCount].szWord);
++nCount;
}
sort(diction, diction + nCount, CmpDictionItem);
while (scanf("%s", szWord) == 1)
{
if ((nAnsItem = FindEnglish(szWord, nCount)) != -1)
{
printf("%s\n", diction[nAnsItem].szEnglish);
}
else
{
printf("eh\n");
}
}
 
return 0;
}
学习了。

百练OJ:2804词典

标签:word   ops   城市   空行   ++   content   理解   lin   get   

原文地址:http://www.cnblogs.com/cjf1699-dut/p/7341769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!