2019-ACM-ICPC- Xuzhou station network game -M.Longest subsequence- find a longest sequence from the string s, such that lexicographic order is strictly greater than t

2019-ACM-ICPC- Xuzhou station network game -M.Longest subsequence- find a longest sequence from the string s, such that lexicographic order is strictly greater than t


【Problem Description】

From the string (S \) \ find a longest sequence, such that lexicographic order is strictly greater than \ (T \) .

【Solution】

The answer for the string, and must be \ (T \) consistent with the front part of the string, starting from a specific letter \ (T \) large characters, after the characters have to take on the line. Front to scan \ (S \) string, while using an array \ (sum [26] \) Maintenance \ (T \) position of the last occurrence of the letter, for \ (S \) for each character, each with a less than character \ (s_i \) letter and before that occurs gradually update the answer to. Special attention to the situation sentenced to two identical characters.

【Code】

/*
 * @Author: Simon 
 * @Date: 2019-09-07 15:43:51 
 * @Last Modified by:   Simon 
 * @Last Modified time: 2019-09-07 15:43:51 
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 2000005
char s[maxn],p[maxn];
int sum[30]/*每个字母最后出现的位置*/,pos=1;
bool vis[30];//每个字母是否出现过
Int main(){
#ifndef ONLINE_JUDGE
    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;cin>>n>>m;
    cin>>s+1>>p+1;
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<26;j++){
            if(vis[j]&&s[i]-'a'>j) ans=max(ans,sum[j]+n-i);
        }
        if (s[i] > p[pos]) ans = max(ans, pos + n - i);
        else if(s[i]==p[pos]){
            vis[p[pos]-'a']=1;
            sum[p[pos]-'a']=pos++;
        }
        if(pos>m){
            if(i!=n) ans=max(ans,m+n-i); //两个字符串不能完全相同
            break;
        }
    }
    if(!ans) cout<<"-1"<<endl;
    else cout<<ans<<endl;
#ifndef ONLINE_JUDGE
    cout<<endl;system("pause");
#endif
    return 0;
}

Guess you like

Origin www.cnblogs.com/--Simon/p/11502611.html