给定一个长度为\(n\)的数字序列\(s\)(\(1 \leq n \leq 100000\)),知足\(1 \leq s_i \leq q\)(\(5 \leq q \leq 9\))。求一个最短的子序列\(s'\),知足\(s'\)在\(s\)中没有出现。为了方便,请输出\(s'\)的长度。
ios
当存在一个\(i\),知足\(s_1 \dots s_i\)中数字\(1 \dots q\)都出现过,那么确定不存在长度\(\leq 1\)的\(s'\)。当存在一个\(j\),知足\(s_{i + 1} \dots s_j\)中数字\(1 \dots q\)都出现过,那么确定不存在长度\(\leq 2\)的\(s'\)。
容易推广得对于序列\(p\),若是知足\(p_0 = 1\),\(p_i < p_{i + 1}\),\(p_m \leq n\),且\(s_{p_i} \dots s_{p_{i + 1} - 1}\)中数字\(1 \dots q\)都出现过,则最大的\(m\)即为所求的\(s'\)的长度。spa
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int n, q; bool b[15]; int ans = 1; int main() { scanf("%d%d", &n, &q); int cnt = 0, tmp; while (n--) { getchar(); tmp = getchar() - '0'; if (!b[tmp]) { b[tmp] = true; ++cnt; if (cnt == q) { ++ans; cnt = 0; memset(b, 0, sizeof b); } } } printf("%d", ans); return 0; }