3.14牛客2021年度训练联盟热身训练赛第二场B.g2g c u l8r[map & substr函数]

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述 

According to the national statistics, a teenager sends/receives 100+ text messages a day. Dr. Orooji’s teenage children are no exception but the problem is Dr. O (an old-fashioned, face-to- face communicator) has difficulty reading text messages full of abbreviations (short-hands) sent to him by his children. Dr. O needs your help reading these text messages.

 

The Problem:

 

Given the list of abbreviations and a paragraph, you are to expand the text (paragraph) so that Dr. O can read it easily.

输入描述:

 

The first input linecontains an integer,n (1 ≤ n ≤ 20),indicating the number of abbreviations. Theseabbreviations are on the followingninputlines, one per line. Each input linestarts in column 1 and containsan abbreviation (1-5 characters, consisting of only lowercaseletters and/or digits). The abbreviation is followed by exactly one space, and this isfollowed by the expanded version ofthe abbreviation (1-50 characters, consisting of only lowercase letters and spaces; assume the expandedversion does not start or end with aspace and contains no multiple consecutive spaces between words).Assume that all abbreviations are distinct, i.e., no duplicates.

 

The list of abbreviations isfollowed by a positive integer,p,indicating the number of input lines containingthe paragraph to be expanded. Theparagraph is on the followingpinputlines. Assume these input linesdo not exceed column 50, donot start or end with a space, and each line contains at least one word. The paragraph will contain only lowercaseletters, digits, and spaces. Assume that there will not be multipleconsecutive spaces in theinput paragraph.

 

A word is defined as aconsecutive sequence of letters/digits.  Assume that a word will be entirely on oneinput line, i.e., a word isnot broken over two or more lines.

输出描述:

 

Each line of the inputparagraph must be on one line of output. Theinput line must be printed in theoutput exactly the same (spacing). Theonly exception is that each abbreviation must be replaced by its expanded version, i.e., when an abbreviation isfound in the input, its expanded version must beoutput.

 

Note that an abbreviationmust match a word completely and not just part of a word. For example,if u is an abbreviation for “you”, then u must appear as a word by itself in the paragraph

 

in order to be replaced,i.e., if the abbreviation is part of a word in the paragraph (e.g.,the paragraph containsthe word buy or ugly or you),the u in these words should not be replaced.

示例1

输入

复制

8
g2g got to go
g good
c see
l8 late
l8r later
d i am done
u you
r are
6
hi
how r u
you tell me
you are l8
d
c u l8r

输出

复制

hi
how are you
you tell me
you are late
i am done
see you later

✕就很奇怪,\n放在字符串里在牛客就可以正常输出,我自己敲的endl只能输出一行

☀很神奇的是map不仅可以存储单词还可以存储带空格的句子

☀在这种严格控制输入的题目里,getchar函数必不可少

☀如何将带空格的string句子拆分出单词?

        使用substr函数

        int l=0;
        for(int i=0;i<=s.size();i++)
        {
            if(s[i]==' '||i==(s.size()))
            {
                string t=s.substr(l,i-l);
                l=i+1;
            }
        }

wa代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
#define Inf 0xfffffff
#define N 501000
#define eps 1e-7
using namespace std;
 
map<string,string>mp;
 
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        string a,b;
        cin>>a;
        getchar();
        getline(cin,b);
        mp[a]=b;
    }
    int m;
    cin>>m;
    getchar();
    while(m--)
    {
        string s;
        getline(cin,s);
        int l=0;
        string ou;
        for(int i=0;i<=s.size();i++)
        {
            if(s[i]==' '||i==(s.size()))
            {
                string t=s.substr(l,i-l);
                if(mp.count(t))
                    ou+=mp[t];
                else
                    ou+=t;
                //cout<<"测试"<<t<<"实验"<<mp[t]<<endl;
                ou+=s[i];
                l=i+1;
            }
        }
        cout<<ou<<endl;
    }
    return 0;
}

ac代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
#define Inf 0xfffffff
#define N 501000
#define eps 1e-7
using namespace std;

map<string,string>mp;

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        string a,b;
        cin>>a;
        getchar();
        getline(cin,b);
        mp[a]=b;
    }
    int m;
    cin>>m;
    getchar();
    while(m--)
    {
        string s;
        getline(cin,s);
        s+='\n';
        int l=0;
        string ou;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]==' '||s[i]=='\n')
            {
                string t=s.substr(l,i-l);
                if(mp.count(t))
                    ou+=mp[t];
                else
                    ou+=t;
                //cout<<"测试"<<t<<"实验"<<mp[t]<<endl;
                ou+=s[i];
                l=i+1;
            }
        }
        cout<<ou;
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值