首页 > 其他分享 >2019-ACM-ICPC 徐州网络赛 M. Longest subsequence 序列自动机

2019-ACM-ICPC 徐州网络赛 M. Longest subsequence 序列自动机

时间:2023-02-07 12:32:34浏览次数:44  
标签:string int tt 样例 length ACM ICPC subsequence

String is a very useful thing and a subsequence of the same string is equally important.

Now you have a string ss with length nn and a string tt with length mm. Find out the longest subsequence in the string ss so that the lexicographical order of this subsequence is strictly larger than tt.

Input

two integers nn, mm in the first line 

(All characters are lowercase letters)

The second line is a string ss

The third line is a string tt

  • 1 \le n,m \le 10^61≤n,m≤106

Output

Output an integer representing the longest length, otherwise output -1.

样例输入1复制

9 3 aaabbbccc abc

样例输出1复制

6

样例输入2复制

9 3 aaabbbccc zzz

样例输出2复制

-1

题意:

给你两个字符串S, T。问S串的子序列字典序严格>T的字典序的子序列最大长度?

分析:

序列自动机预处理出来每一个位置后面其最近的字母的位置是什么,即pos[i][j]数组,可以求出主串S第i个位置字母j在后面出现的第一个位置。

对于str2每一个字符来说,我们可以有两种选择:

  1. 选择比str2[i]大的,则剩下的都可以选,
  2. 选择跟str2[i]一样的,只有最后剩下的字符才能算比其字典序大。

具体看代码把

#include<bits/stdc++.h>
using namespace std;

const int N=1000005;
typedef long long LL;
int pos[N][30];
void getpos(char str1[])
{
    int len1=strlen(str1+1);      
    
    for(int i=0;i<26;i++)
    	pos[len1][i]=len1+1;
    for(int i=len1; i>=1; i--)
    {
        for(int j=0; j<26; j++)
        {
            pos[i-1][j]=pos[i][j];
        }
        pos[i-1][str1[i]-'a']=i;
    }
}
char str1[N],str2[N];
bool judge(char str2[])
{
    int len1=strlen(str1+1); 
    int len2=strlen(str2+1);
    int p=0;
    for(int i=1; i<=len2; i++)
    {
        p=pos[p][str2[i]-'a'];
        if(p==len1+1)
            return 0;
    }
    return 1;
}
 int len1,len2;
void out()
{
	for(int i=0;i<=len1;i++)
	{
		for(int j=0;j<26;j++)
		{
			cout<<pos[i][j]<<" ";
		}
		cout<<endl;
	}
}
int main()
{
   
    scanf("%d%d",&len1,&len2);
    scanf("%s",str1+1);
    getpos(str1);
    //out();
    
    scanf("%s",str2+1);
    
    int cur=0;
    LL ans=-1;
    
    for(int i=1;i<=len2;i++)
	{
		for(char j=str2[i]+1;j<='z';j++)
		{
			int p=pos[cur][j-'a'];
			if(p<len1+1)
				ans=max(ans,(LL)len1-p+1+i-1);	
			//cout<<ans<<" "<<p<<" "<<j<<endl;
		}
		cur=pos[cur][str2[i]-'a'];
	    //cout<<cur<<" "<<str2[i]-'a'<<endl;
		if(cur>=len1+1) break;


	}
	
	if(cur+1<len1+1) ans=max(ans,(LL)len1-cur+1+len2-1);

    printf("%lld\n",ans);
    return 0;
}

 

标签:string,int,tt,样例,length,ACM,ICPC,subsequence
From: https://blog.51cto.com/u_14932227/6041984

相关文章

  • 2019ccpc与icpc网络赛总结
    网络赛总结 ccpc一场网络赛,icpc的六场网络赛,接近一个月的时间,收获了不少东西。    这几场网络赛,我们队伍主要采取的策略是两个人做一道题,这样我们把低级错误......
  • 2019南昌大学邀请赛 M. Subsequence 序列自动机
     Giveastring SS and NN string T_iTi ,determinewhether T_iTi isasubsequenceof SS.Iftiissubsequenceof SS,print ​​YES​​​,elseprint ......
  • 2022-2023 ICPC Asia East - Hangzhou Regional Contest 中文版题面(部分)
    B给定两个长度为\(n\)的整数序列\(c,d\)和一个长度为\(m\)的\(01\)序列\(v\)。这里的\(c,d,w\)下标从\(1\)开始。有\(q\)次修改,每次会选择一个\(i\in......
  • 论文推荐:ACMix整合self-Attention和Convolution (ACMix)的优点的混合模型
    混合模型ACmix将自注意与卷积的整合,同时具有自注意和卷积的优点。这是清华大学、华为和北京人工智能研究院共同发布在2022年CVPR中的论文卷积分解与自注意力卷积分解......
  • 题解 G. Grammar Path 2020-2021 ICPC NERC (NEERC), North-Western Russia Regional
    传送门【大意】给定一个CNF和一个有向图。有向图上的每一条边都写上了一个字母。要求你从\(s\)到\(t\)走一条尽可能短的路,且将经过的字母写下来后,这个字符串能被......
  • 第十届ACM山东省赛总结
    省赛结束了,排名打铁,只出了四题。前三个小时只出了一道题。M题很简单,我第一次循环做的直接超时,没想到怎么优化,然后误以为有公式,推了半个小时的公式,测试都对,提交一直wa,然后考......
  • 2019年icpc沈阳网络赛 Guanguan's Happy water
    题目链接:​​点击这里​​题目大意:已知一个数列f(n):f[x]=a[x] (1<=x<=k)f[x]=f[x-1]*p[1]+f[x-2]*p[2]………+f[x-k]*p[k] (x>k)给你所有的a[i],再给你接下来k个f[i],求......
  • Genius ACM(归并+倍增)
    题目描述给定一个整数 MM,对于任意一个整数集合 SS,定义“校验值”如下:从集合 SS 中取出 MM 对数(即 2∗M2∗M 个数,不能重复使用集合中的数,如果 SS 中的整数不够 ......
  • HDU-1159-Common Subsequence
    ​​题目链接​​​题目大意:给出两个字符串,求两个字符串的最长公共字串。思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例......
  • poj-1458-Common Subsequence
    CommonSubsequenceTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:43207Accepted:17522DescriptionAsubsequenceofagivensequenceisthegivenseq......