题目链接:  OnlineJudge         

这就是一道基本的最长上升子序列问题,但是这个字符串我们无法直接转化为对应的数值,我们只能全部读入后对其进行排序,排序完成后再对其进行映射,这样我们就可以对他进行求最长上升子序列的长度了,注意本道题求最长上升子序列时只能利用nlogn的方法。

还不知道nlogn求最长上升子序列的小伙伴可以看下我之前的博客,在这给出博客地址:最长子序列问题_AC__dream的博客-CSDN博客

下面是代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e6+10;
int a[N],dp[N];
struct node{
	string s;
	int id;
}p[N];
bool cmp(node a,node b)
{
	return a.s<b.s;//对字符串进行排序 
}
int main()
{
	int n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p[i].s;
		p[i].id=i;
	}
	sort(p+1,p+n+1,cmp);
	int cnt=0;
	a[p[1].id]=++cnt;
	for(int i=2;i<=n;i++)
		{
			if(p[i].s!=p[i-1].s)//注意字符串相同的应对应相同的值 
				a[p[i].id]=++cnt;
			else if(p[i].s==p[i-1].s)
				a[p[i].id]=cnt;
		}
	memset(dp,0x3f,sizeof dp);
	for(int i=1;i<=n;i++)
		*lower_bound(dp,dp+n,a[i])=a[i];
	printf("%d\n",lower_bound(dp,dp+n,0x3f3f3f3f)-dp);
	return 0;
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐