题目描述
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.
输入
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
AC的C++代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <sstream>
using namespace std;
int main()
{
// 定义一个map对象
map<string,string>mapStr;
int N;
cin>>N;
// 输入缩写
while(N--)
{
string sf,str;
cin>>sf; //按空格分割(相当于读入第一个词)
getline(cin,str); //按分行符分割(相当于读入每行剩余的字符串)
string::iterator it;
it = str.begin();
str.erase(it);
mapStr[sf] = str; //添加到map中
}
// 原文本处理
int M;
cin >> M;
string temp;
getline(cin,temp);
while(M--)
{
string str,sTmp;
getline(cin , str) ; //按行输入
istringstream istr(str);
while( !istr.eof() )
{
istr >> sTmp ; //get a word
if(mapStr[sTmp]=="") //如果存在缩写
cout<<sTmp<<" ";
else
cout<<mapStr[sTmp]<<" ";
}
cout << "\n";
}
}