Project 3: Sort Poems

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 string
typedef map<string,int>::iterator iter;		//map iterator easy to traverse map
typedef 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 otherwise
	iter 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 map
			return (*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)) time
		return head[key];
	else
		return -1;
}
int main()
{
	int Sn,Ln;				// the segment number and line number per segment
	int test_case=1;		// test case number
	char line[81];			//for reading each line
	while(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 line
			printf("\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 index
		MSI tail;				//create a map recording each tail lines mapping to segment index
		//input the segments
		for(int i=0;i!=Sn;i++)				//traverse each segment
		{
			for(int j=0;j!=Ln;j++)			//traverse each line
			{
				gets(line);					//read a line
				string buffer(line);		
				Segment[i].push_back(buffer);	//push back into vector
				if(j==0)		//if the first line
					head.insert(pair<string,int>(buffer,i));		//const time
				if(j==Ln-1)		//if the last line
					tail.insert(pair<string,int>(buffer,i));		//const time
			}
			
		}
		int index=Find_Head(head,tail);		//get the head's segment index of the whole pome
		printf("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 index
			if(index==-1)				//if the last segment,print its last line
				cout<<*j<<endl;
		}
		test_case++;
		//destroy the variables
		head.clear();					
		tail.clear();
		delete[]Segment;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值