一道平平无奇的字符串题目(最长上升子序列)
题目链接: OnlineJudge这就是一道基本的最长上升子序列问题,但是这个字符串我们无法直接转化为对应的数值,我们只能全部读入后对其进行排序,排序完成后再对其进行映射,这样我们就可以对他进行求最长上升子序列的长度了,注意本道题求最长上升子序列时只能利用nlogn的方法。还不知道nlogn求最长上升子序列的小伙伴可以看下我之前的博客,在这给出博客地址:最长子序列问题_AC__dream的博客-C
·
题目链接: 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;
}
更多推荐
所有评论(0)