(The 11th Final of the Blue Bridge Cup) G Garden Arrangement (the longest ascending subsequence)

Topic link: "Blue Bridge Cup" Practice System

Analysis: This is a longest ascending subsequence problem, except that each element is not an integer but a string, but the essence is the same. For the longest ascending subsequence problem, I will give two methods below.

I will not go into details about the problem of solving the longest ascending subsequence. I have already covered it in detail in the previous blog. For details, please refer to: Detailed explanation of the longest subsequence problem

The first method is ordinary one-dimensional dp, f[i] represents the maximum length of the longest ascending subsequence ending with the i-th string. I will not talk about the dynamic transition equation. What I want to say is don’t forget Open a pre array to record what is the previous position transferred to the current position. This is updated during the process of updating f, but this can only pass 70% of the samples. After all, the complexity is o(n^2). , the code is attached below:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1000003;
string s[N]; 
int f[N],pre[N],st[N];//f[i]表示以第i个名字结尾的最长上升子序列 
int t1[N],t2[N];
bool cmp(int i,int j)//判断以第i个名字结尾的上升子序列和以第j个名字结尾的上升子序列谁字典序更小 
{
	if(f[i]<f[j]) return true;
	if(f[i]>f[j]) return false;
	int tt1=0,tt2=0;
	while(i)
	{
		t1[++tt1]=i;
		i=pre[i];
	}
	while(j)
	{
		t2[++tt2]=j;
		j=pre[j];
	}
	for(int i=tt1;i>=1;i--)
		if(s[t1[i]]<s[t2[i]]) return false;
		else if(s[t1[i]]>s[t2[i]]) return true;
}
int main()
{
	int tt=0;
	string p;
	cin>>p;
	for(int i=0;i<p.size();i++)
	{
		if(p[i]<='Z'&&p[i]>='A')
			s[++tt]+=p[i];
		else
			s[tt]+=p[i];
	}
	int ans=0;
	for(int i=1;i<=tt;i++)
	{
		f[i]=1;
		for(int j=1;j<i;j++)
		{
			if(s[i]>s[j])
			{
				if(f[i]<f[j]+1)
				{
					f[i]=f[j]+1;
					pre[i]=j;//记录路径 
				}
				else if(f[i]==f[j]+1)
				{
					if(s[pre[i]]>s[j])
					{
						pre[i]=j;//记录路径 
					}
				}
			}
			if(cmp(ans,i)) 
				ans=i;
		}
	}
	tt=0;
	while(ans)
	{
		st[++tt]=ans;
		ans=pre[ans];
	}
	for(int i=tt;i;i--)
		cout<<s[st[i]];
	return 0;
}

The following is the optimized code of the monotonic stack. The complexity is o(nlogn). I have given the proof in the blog attached above. It also includes how to output the final monotonic ascending subsequence. It is written in detail. You can read it. For a moment, the code is given directly below:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1000003;
string s[N];
int st[N];
vector<string> ans;
int id[N];//id[i]记录第i个数在最长上升子序列中的位置
int main()
{
	int tt=0;
	string p;
	cin>>p;
	for(int i=0;i<p.size();i++)
	{
		if(p[i]<='Z'&&p[i]>='A')
			s[++tt]+=p[i];
		else
			s[tt]+=p[i];
	}
	ans.push_back(s[1]);
	int id_len=0;id[++id_len]=1;
	for(int i=2;i<=tt;i++)
	{
		if(s[i]>ans[ans.size()-1]) 
		{
			ans.push_back(s[i]);
			id[++id_len]=ans.size();
			continue;
		}
		int t=lower_bound(ans.begin(),ans.end(),s[i])-ans.begin();
		ans[t]=s[i];
		id[++id_len]=t+1;
	}
	tt=0;
	for(int i=ans.size(),j=id_len;i>=1;j--)
		if(id[j]==i) 
			st[++tt]=j,i--;
	for(int i=tt;i>=1;i--)
		cout<<s[st[i]];
	return 0;
}

Guess you like

Origin blog.csdn.net/AC__dream/article/details/124023545