zoukankan      html  css  js  c++  java
  • Longest 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^61n,m106

    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
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+10;
    
    int n,m;
    char s[maxn],t[maxn];
    int nx[maxn][26];
    
    int main() {
        scanf("%d%d",&n,&m);
        scanf("%s%s",s+1,t+1);
        for(register int i=0;i<26;++i){
            nx[n][i]=nx[n+1][i]=n+1;
        }
        for(register int i=n-1;i>=0;--i){
            for(register int j=0;j<26;++j){
                nx[i][j]=nx[i+1][j];
            }
            int to=s[i+1]-'a';
            nx[i][to]=i+1;
        }
        int res=-1,cur=0;
        for(register int i=1;t[i];++i){
            int pos=t[i]-'a';
            for(register int j=pos+1;j<26;++j){
                int id=nx[cur][j];
                if(id<=n){
                    res=max(res,n-id+1+i-1);
                }
            }
            cur=nx[cur][pos];
            if(cur>n)break;
        }
        if(cur<n)res=max(res,m+n-cur);
        printf("%d
    ",res);
        return 0;
    }
  • 相关阅读:
    h5红包雨
    Reflect
    el-dialog对话弹框中根据后台数据无限制添加el-select标签,并进行展示,搜索,删除
    jQuery伪分页效果
    canvas实现验证码
    jQuery四叶草菜单效果,跟360杀毒软件差不多
    事件
    传参
    在shell script中进行数值运算的两种方法
    为maven插件设置参数的三种方法
  • 原文地址:https://www.cnblogs.com/czy-power/p/11482430.html
Copyright © 2011-2022 走看看