[Blue Bridge Cup National Competition Zhenti]: Essential ascending sequence

[Blue Bridge Cup National Competition Zhenti]: Essential ascending sequence

Essential ascending sequence

Topic description

小蓝特别喜欢单调递增的事物。 在一个字符串中,如果取出若干个字符,将这些字符按照在字符串中的顺
序排列后是单调递增的,则成为这个字符串中的一个单调递增子序列。 例如,在字符串 lanqiao 中,如果取出字符 n 和 q,则 nq
组成一个单 调递增子序列。类似的单调递增子序列还有 lnq、 i、 ano 等等。
小蓝发现,有些子序列虽然位置不同,但是字符序列是一样的,例如取第 二个字符和最后一个字符可以取到 ao,取最后两个字符也可以取到 ao。小蓝
认为他们并没有本质不同。 对于一个字符串,小蓝想知道,本质不同的递增子序列有多少个? 例如,对于字符串
lanqiao,本质不同的递增子序列有 21 个。它们分别 是 l、 a、 n、 q、 i、 o、 ln、 an、 lq、 aq、 nq、
ai、 lo、 ao、 no、 io、 lnq、anq、 lno、 ano、 aio。 请问对于以下字符串(共 200
个小写英文字母,分四行显示):(如果你把 以下文字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。在 试题目录下有一个文件
inc.txt,内容与下面的文本相同)
本质不同的递增子序列有多少个?

string to solve

tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhf
iadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqij
gihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmad
vrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl

Topic ideas and codes

Idea: Similar to the longest ascending subsequence, the difference is that it is strictly monotonic, so if there are repeated letters, it needs to be de-duplicated. Among them, for the same letter as itself, subtract the previously constructed sequence, y because it will be constructed repeatedly.
Code:

#include <iostream>
#include <string.h>
using namespace std;
typedef long long int ll;
const ll N=200;
ll dp[N];
int main()
{
    
    
	string s="tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl";
	//string s="lanqiao";
	for(int i=0;i<N;i++)dp[i]=1;
	for(int i=1;i<N;i++)
	{
    
    
		for(int j=0;j<i;j++)
		{
    
    
			if(s[i]>s[j])
			{
    
    
				dp[i]+=dp[j];
			}
			else if(s[i]==s[j])
			{
    
    
				dp[i]-=dp[j];
			}
		}
	}
	int ans=0;
	for(int i=0;i<N;i++)
	{
    
    
		ans+=dp[i];
	}
	cout<<ans<<endl;
	return 0;
}

"If you are undecided, you can ask the spring breeze, and if the spring breeze does not speak, you will follow your heart" means: if you are hesitant about something, ask the spring breeze how to do it. . "If you are undecided, you can ask the spring breeze. If the spring breeze does not speak, you will follow your heart." The sentence comes from the "Jianlai" written by the Internet writer "Fenghuo Opera Princes". The original text is: "If you are undecided, you can ask the spring breeze. Follow your heart".

insert image description here


Guess you like

Origin blog.csdn.net/weixin_46627433/article/details/123756498