Project 3: Sort Poems

来源:互联网 发布:智慧社区app源码下载 编辑:程序博客网 时间:2024/06/09 14:18

Project 3: Sort Poems
Harry once found an interesting ancient poem written on a long strip of paper.
He decided to save this piece of art into his computer by scanning the paper segment
by segment. However, over excited as he was, he forgot to name the segment files in
the proper order! Harry almost fainted with hundreds of unsorted segment files in
hand. Now he comes to you for help. The only clue he has is that there is always a
one-line overlap between two consecutive segments. For example, the poem shown
in Figure 1 is saved in two files as shown in Figure 2, provided that each file can only
contain up to 4 lines. Notice that a blank line is also counted as a line in the second
piece of file.
Figure 1 Figure 2
Input Specification:
Your program must read test cases from the standard input.
The input consists of several test cases. 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 are
the 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, for
each case, print the complete poem out. There must be one blank line between the
outputs 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.

My solution:

#pragma warning(disable : 4786)//for vc++ 6.0#include <string>#include <iostream>#include <vector>#include <map>#include <cstdio>using namespace std;typedef vector<string> Vec_Str;//vector contains stringtypedef map<string,int>::iterator iter;//map iterator easy to traverse maptypedef map<string,int>MSI;int Find_Head(MSI& head,MSI& tail){//find the final head line of the whole pome//input reference(for saving memory) of head and tail//return the index if successful,-1 otherwiseiter i=head.begin();for(;i!=head.end();i++)//traverse the head map,Nlog(N){if(tail.find((*i).first)==tail.end())//not exist in tail mapreturn (*i).second;}return -1;//failed }int Find_Next(MSI& head,string& key){//find the next segment index of key//complexity: O(log(N))if(head.find(key)!=head.end())//find() cost O(log(N)) timereturn head[key];elsereturn -1;}int main(){int Sn,Ln;// the segment number and line number per segmentint test_case=1;// test case numberchar line[81];//for reading each linewhile(1){scanf("%d%d",&Sn,&Ln);getchar();//avoid redundant '\n'if(Sn==0&&Ln==0)break;if(test_case!=1)//print a blank line before each line except the first lineprintf("\n");Vec_Str* Segment=new Vec_Str[Sn];//declare a map<string,int>and allocate memory if(Segment==NULL)printf("can't allocate memory!\n");MSI head;//create a map recording each head lines mapping to segment indexMSI tail;//create a map recording each tail lines mapping to segment index//input the segmentsfor(int i=0;i!=Sn;i++)//traverse each segment{for(int j=0;j!=Ln;j++)//traverse each line{gets(line);//read a linestring buffer(line);Segment[i].push_back(buffer);//push back into vectorif(j==0)//if the first linehead.insert(pair<string,int>(buffer,i));//const timeif(j==Ln-1)//if the last linetail.insert(pair<string,int>(buffer,i));//const time}}int index=Find_Head(head,tail);//get the head's segment index of the whole pomeprintf("Scenario #%d\n",test_case);while(index>=0)//ensure index is legal{Vec_Str::iterator j=Segment[index].begin();for(;j!=Segment[index].end()-1;j++)//traverse each segment except last line{cout<<*j<<endl;}index=Find_Next(head,*j);//find next segment's indexif(index==-1)//if the last segment,print its last linecout<<*j<<endl;}test_case++;//destroy the variableshead.clear();tail.clear();delete[]Segment;}}