背景
主要目的就是给诗排序然后输出 具体的看sample可以看懂,再不行就看下面的讲解~
Input Specification:
For each test case, the first line contains two positive integers Sn(≤1000) and Ln(≤100), which are the total numbers of segments and lines per segment, respectively. Then Sn×Ln lines follow, which arethe contents of Sn segments. Each line contains no more than 80 characters. It is guaranteed that the content of each overlapped line is unique.The input ends with Sn and Ln both being 0. That case must NOT be processed.
Output Specification:
For each test case, output to the standard output. First print a line saying"Scenario #k", where k is the number of the test case (starting from 1). Then, foreach case, print the complete poem out. There must be one blank line between theoutputs of two neighboring cases, but no extra blank line at the end of output.
Sample Input:
2 4
Return all courtesies that he affords; \\这个是其中的一句话
Drink to him, carve him, give him compliment;
This shall thy mistress more than thee torment.
And if thy rival be in presence too,
Seem not to mark, but do as others do;
Salute him friendly, give him gentle words,
Return all courtesies that he affords; \\重了,其实上面那段应该接在这个后面。
这个更明显~
2 3
This is the 3rd line. \\ 这句话
This is the 4th line.
This is the 5th line.
This is the 1st line.
This is the 2nd line.
This is the 3rd line. \\ 重复的 也就是接口~
0 0
Sample Output:
Scenario #1
And if thy rival be in presence too,
Seem not to mark, but do as others do;
Salute him friendly, give him gentle words,
Return all courtesies that he affords; \\这句话被当做接口了~ 链接两段,程序实现的就是查找这个并把它当接口把诗排序~
Drink to him, carve him, give him compliment;
This shall thy mistress more than thee torment.
Scenario #2
This is the 1st line.
This is the 2nd line.
This is the 3rd line.
This is the 4th line.
This is the 5th line.
主要目的就是给诗排序然后输出 具体的看sample可以看懂,再不行就看下面的讲解~
Input Specification:
For each test case, the first line contains two positive integers Sn(≤1000) and Ln(≤100), which are the total numbers of segments and lines per segment, respectively. Then Sn×Ln lines follow, which arethe contents of Sn segments. Each line contains no more than 80 characters. It is guaranteed that the content of each overlapped line is unique.The input ends with Sn and Ln both being 0. That case must NOT be processed.
Output Specification:
For each test case, output to the standard output. First print a line saying"Scenario #k", where k is the number of the test case (starting from 1). Then, foreach case, print the complete poem out. There must be one blank line between theoutputs of two neighboring cases, but no extra blank line at the end of output.
Sample Input:
2 4
Return all courtesies that he affords; \\这个是其中的一句话
Drink to him, carve him, give him compliment;
This shall thy mistress more than thee torment.
And if thy rival be in presence too,
Seem not to mark, but do as others do;
Salute him friendly, give him gentle words,
Return all courtesies that he affords; \\重了,其实上面那段应该接在这个后面。
这个更明显~
2 3
This is the 3rd line. \\ 这句话
This is the 4th line.
This is the 5th line.
This is the 1st line.
This is the 2nd line.
This is the 3rd line. \\ 重复的 也就是接口~
0 0
Sample Output:
Scenario #1
And if thy rival be in presence too,
Seem not to mark, but do as others do;
Salute him friendly, give him gentle words,
Return all courtesies that he affords; \\这句话被当做接口了~ 链接两段,程序实现的就是查找这个并把它当接口把诗排序~
Drink to him, carve him, give him compliment;
This shall thy mistress more than thee torment.
Scenario #2
This is the 1st line.
This is the 2nd line.
This is the 3rd line.
This is the 4th line.
This is the 5th line.
这个更明显~
我采取的方法主要是充分利用map的性质,便于查找的时候快速的查找到需要的值。也就是说找到了开头之后,
一段的结尾就是另外一个的开头,这个是一个重要的性质。
# include <iostream>
# include <vector>
# include <map>
# include <string>
using namespace std;
struct info
{
int headIndex;
int tailIndex;
bool inHead;
bool inTail;
info()
{
headIndex=-1;
tailIndex=-1;
inHead=false;
inTail=false;
}
};
vector<vector<string>> poem;
map<string,info> segInfo;
vector<string> all;
void clearLast()
{
poem.clear();
segInfo.clear();
all.clear();
}
int main()
{
int sn,ln;
cin>>sn>>ln;
int count=0;
getchar();
while (true)
{
if (sn==0&&ln==0)
{
break;
}
int i,j;
vector<string> seg;
string line;
for (i=0;i<sn;i++)
{
for (j=0;j<ln;j++)
{
getline(cin,line);
seg.push_back(line);
line.clear();
}
poem.push_back(seg);
seg.clear();
}
int sizeSeg=poem.size();
int lineSeg;
map<string,info>::iterator it;
for (i=0;i<sizeSeg;i++)
{
seg=poem.at(i);
lineSeg=seg.size();
string headStr=seg.at(0);
string tailStr=seg.at(lineSeg-1);
it=segInfo.find(headStr);
if (it==segInfo.end())
{
info tempInfo;
tempInfo.headIndex=i;
tempInfo.inHead=true;
segInfo[headStr]=tempInfo;
}
else
{
info tempInfo=it->second;
if (tempInfo.inHead==true)
{
cout<<"Oh on!!!!!!!!"<<endl;
exit(1);
}
else
{
tempInfo.headIndex=i;
tempInfo.inHead=true;
segInfo[headStr]=tempInfo;
}
}
it=segInfo.find(tailStr);
if (it==segInfo.end())
{
info tempInfo;
tempInfo.tailIndex=i;
tempInfo.inTail=true;
segInfo[tailStr]=tempInfo;
}
else
{
info tempinfo=it->second;
if (tempinfo.inTail==true)
{
cout<<"Oh no ,tail !!!"<<endl;
exit(1);
}
else
{
tempinfo.tailIndex=i;
tempinfo.inTail=true;
segInfo[tailStr]=tempinfo;
}
}
}
string next;
for (it=segInfo.begin();it!=segInfo.end();it++)
{
info tempinfo=it->second;
if (tempinfo.inHead==true&&tempinfo.inTail==false)
{
int headIndex=tempinfo.headIndex;
vector<string> line=poem.at(headIndex);
for (i=0;i<line.size();i++)
{
all.push_back(line.at(i));
}
next=line.at(line.size()-1);
}
}
info nextInfo=segInfo[next];
while (nextInfo.inHead==true)
{
int headIndex=nextInfo.headIndex;
vector<string> line=poem.at(headIndex);
for (i=1;i<line.size();i++)
{
all.push_back(line.at(i));
}
next=line.at(line.size()-1);
nextInfo=segInfo[next];
}
count++;
cout<<"Scenario #"<<count<<endl;
int sizeAll=all.size();
for (i=0;i<sizeAll;i++)
{
cout<<all.at(i)<<endl;
}
cout<<endl;
clearLast();
cin>>sn>>ln;
getchar();
}
return 0;
}