poj 2503(map)

2 篇文章 0 订阅

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 38460 Accepted: 16404

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.

这道题难的地方就是怎么按照题目的意思输入,还有就是怎么将string用printf输出(参考了别人的代码改的)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cstdio>
using namespace std;

map<string,string> cnt;

int main()
{
    map<string,string>::iterator it;
    char a[20],b[20],c[20];
    while(1)
    {
        char t;
        t=getchar();
        if(t=='\n')break;
        else
        {
            a[0]=t;
            int i=1;
            while(1)
            {
                t=getchar();
                if(t==' ')
                {
                    a[i]='\0';
                    break;
                }
                else a[i++]=t;
            }
        }
        gets(b);
        cnt[string(b)]=string(a);//char转化成string
    }
    while(gets(c)&&c[0]!='\0')
    {
        if((it=cnt.find(string(c)))!=cnt.end())
                printf("%s\n",it->second.c_str());//用printf输出string的方法
        else printf("eh\n");
    }
    return 0;
}

第二种做法,不是我写的,学习借鉴了。。。

#include<cstdio>
#include<string.h>
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
  int l,t;
  string s,s1,s2;
  map<string,string> dic;
  getline(cin,s);
  l=s.length();
  while(l!=0)
  {
    t=s.find(' ',0);//string的搜索操作。s.find(c,pos):从s位置pos开始查找字符c。pos默认为0。若找到,则返回第一个匹配位置的下标,<span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif;">如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。</span>

    s1.assign(s,0,t);//<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">string &assign(const string &s,int start,int n);把字符串s中从start开始的n个字符赋给当前字符串</span>
    s2.assign(s,t+1,l-t-1);
    dic[s2]=s1;
    getline(cin,s);
    l=s.length();
  }
  while(cin>>s)
  {
    if(dic.find(s)!=dic.end()) cout<<dic[s]<<endl;
    else cout<<"eh"<<endl;
  }
}

今天练习赛又做到这题,最后还是只记得自己第一次写的代码啊==赶紧再拿出来复习一下,然后发现我自己以前参考别人写的另一个代码,那时候TE了,其实也能AC。。只要用不用G++改用C++提交就好了。。(当时以为错了试了各种方法,问了好多人,看了好多解题报告==)这个做法感觉是对我来说比较简单好理解的了

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;

map<string,string> cnt;

int main()
{
    int i=0,j;
    string a,b,c,e;
    while(getline(cin,a))
    {
        if(a.length()==0)break;
        for(i=0;a[i]!=' ';i++)b+=a[i];
        for(j=i+1;a[j]!='\0';j++)c+=a[j];
        cnt[c]=b;
        b="";//这样就可以清空string
        c="";
    }
    while(cin>>e)
    {
        if(cnt.count(e))cout<<cnt[e]<<endl;
        else cout<<"eh"<<endl;
    }
    return 0;
}

这次的练习赛啊。。怎么说,还是老样子吧。。。还退了一点,不可以习惯啊。。还是要相信自己

真的很有必要好好看看书和想想以前做的题了,题不在多。我希望我以后每次的解题报告都可以写成最简单的代码,用最简单的思路,用各种方法都试一试。

靠自己写出来的东西才能记住,所以看过别人的思路以后一定要自己理解了再写一遍==
























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值