HDU-1075-What Are You Talking About

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1075

做这题,开始输入没有输出没有考虑好,想着用%s输入,处理的时候不好处理,数据过了,但一直WA,

思维角度有问题,到网上看了看,有人用map做,很好,我按照他的处理字符方式用我的方法做,A了。

做这题还是有点收获的,对map,string有了一点了解,

我开始的想法:   暴力肯定超时。

用一个结构体(strcut)存储英语,和火星语(mars)对mars 按从小到大排序,再用二分搜索。

这思路是对的。处理出了问题,导致WA

看WA代码 

#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;

struct node
{
char mt[20];
char en[20];
}dictionary[3010];

int len;

bool cmp(const node &a,const node &b)
{
if(strcmp(a.mt,b.mt)<0)
return 1;
else
return 0;
}

void Search(char words[])
{
int l,r;
l=0; r=len-1;
//printf("len=%d ",len);
while(l<=r)
{
int mid=(l+r)/2;
if(strcmp(words,dictionary[mid].mt)<0)
{
r=mid-1;
}
else if(strcmp(words,dictionary[mid].mt)>0)
{
l=mid+1;
}
else
{
printf("%s",dictionary[mid].en);
return ;
}
//printf("%d %d ",l,r);
}
printf("%s",words);
return ;
}

int main(void)
{
int i,j,k;
int ok=0;
char words[20];
char c;
while(scanf("%s",words)!=EOF)
{
if(strcmp(words,"START")==0&&ok==0)
{
ok=1;
k=0;
while(scanf("%s",words)==1&&strcmp(words,"END")!=0)
{
strcpy(dictionary[k].en,words);
scanf("%s",words);
strcpy(dictionary[k++].mt,words);
}
sort(dictionary,dictionary+k,cmp);
len=k;
}
else if(strcmp(words,"START")==0&&ok==1)
{
ok=0;
while(scanf("%s",words)==1&&strcmp(words,"END")!=0)
{
if(ok)
printf(" ");
k=strlen(words);
if(words[k-1]>='a'&&words[k-1]<='z')
{
Search(words);
ok=1;
}
else
{
c=words[k-1];
words[k-1]='';
Search(words);
printf("%c",c);
ok=1;
if(c!=',')
{
printf(" ");
ok=0;
}
}
}
break;
}
}
return 0;
}

代码写的太繁琐了,直接敲到底,还用什么ok==0或ok==1或ok==2,来判断干嘛,那些没用东西可以直接覆盖掉。

看下面的代码,还是那个思路,明显简洁很多,还是那样,敲代码之前,多想想其他的方法,看有没有更简洁的,

代码

#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;

struct node
{
char mt[20];
char en[20];
}dictionary[10000010];

int len;

bool cmp(const node &a,const node &b)
{
if(strcmp(a.mt,b.mt)<0)
return 1;
else
return 0;
}

void Search(char words[])
{
int l,r;
l=0; r=len-1;
while(l<=r)
{
int mid=(l+r)/2;
if(strcmp(words,dictionary[mid].mt)<0)
{
r=mid-1;
}
else if(strcmp(words,dictionary[mid].mt)>0)
{
l=mid+1;
}
else
{
printf("%s",dictionary[mid].en);
return ;
}
}
printf("%s",words);
return ;
}

int main(void)
{
int i,j,k;
int ok=0;
char str[3005];
char a[20];
char c;
gets(a);
k=0;
while(scanf("%s",a)==1&&strcmp(a,"END")!=0)
{
strcpy(dictionary[k].en,a);
scanf("%s",a);
strcpy(dictionary[k++].mt,a);
}
len=k;
sort(dictionary,dictionary+k,cmp);
scanf("%s",a);
//gets(a);//这里有点语法问题,暂时不明白。
getchar();//吃掉回车。为什么。应该是下面有一个gets()的原因吧!
while(1)
{
gets(str);
if(strcmp(str,"END")==0)
break;
int chang=strlen(str);
k=0;
for(i=0;i<chang;i++)
{
if(!(str[i]>='a'&&str[i]<='z'))
{
a[k]='';
Search(a);
k=0;
printf("%c",str[i]);
}
else
{
a[k++]=str[i];
}
}
printf(" ");
}
return 0;
}

网上用一种代码

用map string 来做,我觉得很好,可以说是做这题的额外的收获吧。很简洁,不过就是有点耗时间。

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;

map<string,string>M;

int main()
{
string a,b;
cin>>a;//"START"
while(cin>>a && a!="END")
{
cin>>b;
M[b] = a;
}
cin>>a;//"START"
getchar();//吃回车
char tmp[3005];
while(1)
{
gets(tmp);//用这个比getline()强
if(strcmp(tmp,"END") == 0 )
break;
int i,len;
len = strlen(tmp);
b = "";
for(i=0;i<len;i++)
{
if(!(tmp[i]>='a' && tmp[i]<='z'))//非小写字母
{
if(M[b]!="")//存在这个单词
cout<<M[b];
else
cout<<b;
b="";
cout<<tmp[i];
}
else //小写字母
b=b+tmp[i];
}
cout<<endl;
}
return 0;
}

原文地址:https://www.cnblogs.com/liudehao/p/4135186.html