题解 P1628 【合并序列】

看到这个题,小金羊第一秒的反应就是:

优先队列可解!

看到楼上某同学一个个比较,

find()函数是时候现身了!

string//类型库
      //find具体用法可以自行百度
      //这里仅说这里的用法(逃)
原型:
public size_type basic_string::find(const basic_string&str,size_type __pos = 0)const noexcept;
/*
返回一个定位器:pos(unsigned int pos)
       或返回npos(unsigned int npos=-1)
现行G++版本适用-1代替npos。
英语解释:
pos=position位置
npos=no position没有位置,即没有这个子字符串
*/

还有一点,我们可以建立一个临时队列来存一下输入的字符串。
最后,我看到dalao使用了一个计数器来输出,并且输出都会多一个回车,

这里提供一个简单的方法来避免这个多回车的办法。

juruo不才,惭愧惭愧


Code:

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
priority_queue<string,vector<string>,greater<string> >qwq;
/*
一开始小金羊没有打里面多余的(不看题),
结果就是不看题的后果:一片WA然!(还交了3次)
*/
queue<string>qwq2;
//临时的队列,用来存放输入的字符串
int main()
{
//小金羊没有用输入优化,建议大家加上iostream的取消缓存优化
	int n;
	string input,t;
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		cin>>input;
		qwq2.push(input);
        //存入qwq2里
	}
	cin>>t;//子字符串
	while (!qwq2.empty())
	{
		input=qwq2.front();
		qwq2.pop();
		if (input.find(t,0)==0)
		{//当子字符串位于父字符串的最前端时
			qwq.push(input);
         //压入优先队列
		}
	}
	while (!qwq.empty())
	{//当优先队列不空的时候,尽情输出就是
		input=qwq.top();
		qwq.pop();
        //先取出一个元素
		if (!qwq.empty())
		{//如果里面还有元素,就输出换行(endl)
			cout<<input<<endl;
		}
		else
		{//空了,就只输出字符串,避免最后的换行
			cout<<input;
		}
        //这样就简单的避免了上述最后一个问题
	}
	return 0;
}

虽然,卡换行的出题人可以去世了

关于string在优先队列(或sort)里的排序,

是按照string重载的‘<’号,

自带字典序和长度比较,所以优先队列(或sort)可解!

这篇题解应该是写题解的OIER们最标准的了?

posted @ 2019-02-15 21:08  小金羊  阅读(265)  评论(0编辑  收藏  举报