首页 > ACM题库 > HDU-杭电 > HDU 3293-sort[解题报告]HOJ
2014
03-13

HDU 3293-sort[解题报告]HOJ

sort

问题描述 :

As is known to all, long long ago sailormoon once was an association of fighters. Till now, sailormoon is also an association of girls. Owe to some unknown reasons, girls are necessary to fight for peace.
Their boss, lcy, wants to strengthen their ability, so he give them his precious collections—weapons for many years. Because these collections are really age-old, it is hard to recognize from one to another. So girls intend to sort them before they use. Each weapon has its name, origin and level of harmfulness ( level contains three ranks: wonderful, good, so-so).
In order to make it clear, girls want to sort like this:
firstly,sort according to the origin (sort by lexicographic order), if two or more have the same origin, they will be sorted together;
secondly, sort according ranks, wonderful is the best, good is next, the third is so-so;
thirdly, if two or more have same origin and rank, sort them according to the lexicographic order.

No more tricks, Mr Nanguo

输入:

Input contains multiply cases. Each case contains several lines. First line is an integer N(0<N<=500), representing the number of weapons. Then N lines follows. Each line represent a kind of weapon, and contains a set of strings representing name, origin and level of harmfulness.
Each string will not exceed 20 characters.
Sure that same origin will not exist the same weapon.

输出:

Input contains multiply cases. Each case contains several lines. First line is an integer N(0<N<=500), representing the number of weapons. Then N lines follows. Each line represent a kind of weapon, and contains a set of strings representing name, origin and level of harmfulness.
Each string will not exceed 20 characters.
Sure that same origin will not exist the same weapon.

样例输入:

5
knife qizhou so-so
gun qizhou wonderful
knife zhengzhou good
stick zhengzhou good
rope shengzhou so-so

样例输出:

Case 1
qizhou:
          gun wonderful
          knife so-so
shengzhou:
          rope so-so
zhengzhou:
          knife good
          stick good

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

struct point
{
	string weapon;
	string origin;
	int harm;
};
point F[700];
bool cmp(const point& x,const point& y)
{
	if (x.origin != y.origin)
		return x.origin < y.origin;
	if (x.harm != y.harm)
		return x.harm > y.harm;
	return x.weapon < y.weapon;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("2.txt","r",stdin);
#endif
	int n,i,t=1;
	string str;
	while (cin >> n)
	{
		for (i=0; i < n; ++i)
		{
			cin >> F[i].weapon >> F[i].origin >> str;
			if (str[0]=='s')
				F[i].harm = 1;
			else if (str[0]=='g')
				F[i].harm = 2;
			else F[i].harm = 3;
		}
		sort(F,F+n,cmp);
		printf("Case %d\n",t++);
		for (i=0; i < n; )
		{
			str = F[i].origin;
			cout << str << ":" << endl;
			while (i < n && str==F[i].origin)	//debug:要加上i<n避免越界
			{
				cout << "          " << F[i].weapon << " ";
				if (F[i].harm==1)
					cout << "so-so" << endl;
				else if (F[i].harm==2)
					cout << "good" << endl;
				else
					cout << "wonderful" << endl;
				++i;
			}
		}
	}
	return 0;
}

参考:http://blog.csdn.net/linraise/article/details/20620933


  1. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  2. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c

  3. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。

  4. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c