upc 6597 Don't Be a Subsequence(dp)

                                          6597: Don't Be a Subsequence

                                                                  时间限制: 1 Sec  内存限制: 128 MB
 

题目描述

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.

题目大意:

找一个字符串,不是s的子串,长度最短,字典序最小。

分析:

这个题不会, 看了题解竟然可以用dp做,很是佩服

Next[i][j] 表示  位置i之后  'a' - 'z' 第一次出现的位置

dp[i]  表示   位置i之后  最短的非子序列长度 

最后通过Next数组 找到dp[0]个字母, 字母从小到达大找, 输出就行了

AC代码:

#include <bits/stdc++.h>
#define mset(a, x) memset(a, x, sizeof(a))
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
string str;
int dp[2 * N];
int Next[2 * N][30];
int a[30];
int main() {
	std::ios::sync_with_stdio(false);
	cin >> str;
	for (int i = 0; i < 26; i++) {
		a[i] = str.length();
	}
	for (int i = str.length() - 1; i >= 0; i--) {
		a[str[i] - 'a'] = i;
		for (int j = 0; j < 26; j++) {
			Next[i][j] = a[j];
		}
		dp[i] = INF;
	}
	dp[str.length()] = 1;
	for (int i = str.length() - 1; i >= 0; i--) {
		for (int j = 0; j < 26; j++) {
			dp[i] = min(dp[i], dp[Next[i][j] + 1] + 1);
		}
	}
	int Index = 0;
	for (int i = dp[0]; i >= 0; i--) {
		for (int j = 0; j < 26; j++) {
			if (dp[Index] == dp[Next[Index][j] + 1] + 1) {
				cout << (char) (j + 'a');
				Index = Next[Index][j] + 1;
				break;
			}
		}
	}
	cout << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/MM__1997/article/details/81278921
UPC