hdu1075 What Are You Talking About(map的get读入)

这题因为用getline读一直WA,直接导致我将近一天不想做题。。。。。。。。

 

话说为毛不能用getline啊,可能是我还有别的问题,但我真的是检查了好几遍了,虽然时间快接近2000ms了,但并不是超时,而是WA啊。

我真的不想用get,因为lrj老师说的原因,而且每次还要用次getchar(),但我tm真的没法子了。

哎,自己的困难终究还是要自己克服。。。

 

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cctype>
#include <string.h>
using namespace std;

const int N = 3010;
const int INF = 10000000;

map <string, string> M;

int main()
{
  //  freopen("in.txt", "r", stdin);
    string s0, key, val;
    cin >> s0;
    while(1)
    {
        cin >> key;
        if(!key.compare("END")) break;
        cin >> val;
        M[val] = key;
    }
    map <string, string> :: iterator it;
    cin >> s0;
    getchar();
    char line[N];
    while(1)
    {
        gets(line);
        if(!strcmp(line, "END")) break;
        int len = strlen(line);
        string word;
        for(int i = 0; i < len; i ++)
        {
            if(isalpha(line[i]))
            {
                word += line[i];
            }
            else
            {
                if(M.count(word))
                {
                    it = M.find(word);
                    word = it -> second;
                    cout << word;
                }
                else cout << word;
                word.clear();
                cout << line[i];
            }
        }
        cout << endl;
    }
    return 0;
}


失败的getline:

 

 

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cctype>
#include <string.h>
using namespace std;

const int N = 3010;
const int INF = 10000000;

map <string, string> M;

int main()
{
  //  freopen("in.txt", "r", stdin);
    string s0, key, val, line, buf, buf0;
    cin >> s0;
    while(1)
    {
        cin >> key;
        if(!key.compare("END")) break;
        cin >> val;
        M[val] = key;
    }
    map <string, string> :: iterator it;
    cin >> s0;
    while(1)
    {
        getline(cin, line);
        if(line == "") continue;
        if(!line.compare("END")) break;
        stringstream s0(line);
        int n = 0;
        while(s0 >> buf0)
        {
            n ++;
        }
        int n0 = 0;
        stringstream ss(line);
        while(ss >> buf)
        {
            n0 ++;
            int flag = 0;
            int len = buf.size();
            char c[N];
            if(!isalpha(buf[len - 1]))
            {
                for (int i = 0; i < len; i ++)
                {
                    c[i] = buf[i];
                }
                buf.erase(len - 1);
                flag = 1;
            }
            if(M.count(buf))
            {
                it = M.find(buf);
                buf = it -> second;
            }
            cout << buf;
            if(flag) printf("%c", c[len - 1]);
            if(n0 != n) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值