首页 > 其他分享 >upc 6597: Don't Be a Subsequence (字符串的最短不匹配子序列 dp)

upc 6597: Don't Be a Subsequence (字符串的最短不匹配子序列 dp)

时间:2023-05-29 11:32:09浏览次数:48  
标签:string 6597 int MAX upc Subsequence English suffx dp


6597: Don't Be a Subsequence

时间限制: 1 Sec  内存限制: 128 MB
提交: 237  解决: 45
[提交] [状态] [讨论版] [命题人:admin]

 

题目描述

A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, arc, artistic and (an empty string) are all subsequences of artistic; abc and ci are not.
You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.

Constraints
1≤|A|≤2×105
A consists of lowercase English letters.

 

输入

Input is given from Standard Input in the following format:
A

 

输出

Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A.

 

样例输入

atcoderregularcontest

 

样例输出

b

 

提示

The string atcoderregularcontest contains a as a subsequence, but not b.

【题意】

给出一个字符串,求一个最短的且字典序最小的字符串(以下称为可行串),满足不能与所给串的任意子序列相匹配。

【分析】

设suffx(i)表示以i开头的后缀子串,比如字符串abcde的suffx(3)=cde

设dp[i] 表示suffx(i)串的 可行串 的最小长度

枚举当前后缀串suffx(i) 的第一个字符j,则这个字符一定能和suffx(i)的第一次出现的 j 匹配。

用nxt[j]记录当前后缀串中,第一个出现字符 j 的位置

则:dp[i] = min ( dp[ nxt[j] +1 ] + 1 );

同时记录路径:

 path[i] 表示一个字母,suffx(i) 中以此字母开头,可以得到最短可行串。

to[i] 表示suffx(i)第一个字符放上path[i]之后,可以移动到后面的哪个suffx( i )

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=2e5+5;
const int INF=0x3f3f3f3f;
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
int nxt[MAX];
int dp[MAX];
int path[MAX];
int to[MAX];
char str[MAX];
int main()
{
    while(~scanf("%s",str))
    {
        int len=strlen(str);
        for(int j=0;j<26;j++)nxt[j]=len;
        for(int i=0;i<=len;i++) //多考虑一个后缀串,空串,能匹配所有字符串
            dp[i]=INF;
        dp[len+1]=0;
        //nxt[i]表示字符i在当前后缀串中出现的第一个位置
        for(int i=len-1;i>=0;i--) //遍历每一个后缀串
        {
            nxt[str[i]-'a']=i;
            for(int j=0;j<26;j++)
            {
                if(gmin(dp[i],dp[nxt[j]+1]+1))
                {
                    path[i]=j; //记录下当前后缀第一个字符放哪个字母
                    to[i]=nxt[j]+1; //下一个后缀位置
                }
            }
        }
        int i=0;
        while(i<len)
        {
            printf("%c",path[i]+'a');
            i=to[i];
        }
        putchar('\n');
    }
    return 0;
}

 

标签:string,6597,int,MAX,upc,Subsequence,English,suffx,dp
From: https://blog.51cto.com/u_16125110/6369262

相关文章

  • upc 6445: 棋盘V (网络流费用流解决匹配问题)
    6445:棋盘V时间限制:1Sec  内存限制:128MB提交:325  解决:31[提交][状态][讨论版][命题人:admin]题目描述有一块棋盘,棋盘的边长为100000,行和列的编号为1到100000。棋盘上有n个特殊格子,任意两个格子的位置都不相同。现在小K要猜哪些格子是特殊格子。她知道所有格子......
  • 1332. Remove Palindromic Subsequences刷题笔记
    容易陷入思维盲区,只有a和b的字符串,只会有2个或1个回文classSolution:defremovePalindromeSub(self,s:str)->int:return2-(s==s[::-1])......
  • THUPC2023游记
    2023.2THUPC报名!和unputdownable,猫猬兽组队,队名XJ五队。devin让我们填毕业年份2028。收货地址填了重庆市第114514中学冯阳阳纪念谷学校。upd性别填其他被拒了。2023.3.4THUPC2023初赛测试赛!\(3\)月\(4\)日\(13\)点联络群消息:##OJ网址thupc2023.thusa......
  • P5446 [THUPC2018]绿绿和串串 题解
    Description给定一个串\(S\),要求串\(S\)是串\(R\)经过多次翻转后的前缀。问有多少种初始长度的串\(R\)。串\(R\)翻转的定义是将前\(|R|-1\)个字符倒序排列后,插入到串的最后。如\(\mathrm{aaa}\)翻转后得到\(\mathrm{abcdcba}\)。Solution我们先设以\(i\)为......
  • abc271_e Subsequence Path 题解
    SubsequencePath题意有\(n\)个城市和\(m\)条有向道路,编号从\(1\)开始,第\(i\)条道路从\(a_i\)到\(b_i\),长度为\(c_i\)。给定一个长度为\(k\)的序列\(e\),我们定义从\(1\)到\(n\)的一条路径是优秀的当且仅当:经过的边的编号按顺序构成\(e\)的一个子序列。......
  • subsequence1 (牛客多校) (2个串比大小, DP, 组合数)
    题面大意:给定2个字符串,问有多少个子字符串S,是大于t的 思路数据范围很小,因此考虑n^2做法分2步,位数s>位数t的时候然后位数相等的时候利用DP,处理,分别就是枚举前k个数和s相同,然后k+1个数比t大就可以. 具体思路自己想想,和那个比较像   cons......
  • CF1794C Scoring Subsequences题解
    文中\(a\)为题目中给的\(a\)。如果我们要求\(a_1,a_2,a_3,\dots,a_m\)的结果,那么我们可以把\(a\)数组从后往前依次除以\(i\),\(i\)从\(1\)到\(n\),即为\(\frac{a_1}{m},\frac{a_2}{m-1},\frac{a_3}{m-2},\dots,\frac{a_{m-1}}{2},\frac{a_m}{1}\),并将其保......
  • 「USACO2016JAN」Subsequences Summing to Sevens
    [USACO16JAN]SubsequencesSummingtoSevensS题目描述FarmerJohn's\(N\)cowsarestandinginarow,astheyhaveatendencytodofromtimetotime.EachcowislabeledwithadistinctintegerIDnumbersoFJcantellthemapart.FJwouldliketota......
  • 2023 xjtupc 西交校赛
    A签到,\(O(1)\)。C++Code#include"bits/stdc++.h"usingnamespacestd;usingi64=longlong;intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);intn;cin>>n;cout<<(n<=6?"water"......
  • CF750E - New Year and Old Subsequence
    题意:给一个字符串,每次询问它的一个区间,问最少删除多少个字符,使得区间没有子序列2016,但是有子序列2017。Mysolution首先考虑贪心,通过预处理的方式找到区间最后一个7,依次往前贪心的找到最靠后的一组2017。接下来,我们需要7的后面没有6,7前面的部分不能组合出2016。我们先......