--- title: Shrinking tags: - Implementation id: "589" categories: - - OI - Common Skill date: 2017-06-21 18:20:36 --- ## 题目描述 Snuke can change a string $t$ of length $N$ into a string $t'$ of length $N-1$ under the following rule: - for each $i \; (1 \le i \le N-1)$, the $i$-th character of $t'$ must be either the $i$-th or $(i+1)$-th character of $t$. There is a string $s$ consisting of lowercase English letters. Snuke's objective is to apply the above operation to $s$ repeatedly so that all the characters in $s$ are the same. Find the minimum necessary number of operations. ## 题意概述 给定一个初始长度为$len$的字符串$s$。每次操作可以将$s$变成长度为$(len-1)$的字符串$s'$,并且满足 $$ \forall i \in [1, len-1], \; s_i'=s_i \lor s_i'=s_{i+1} $$ 问至少经过多少次操作使得$s$中所有字母都相同。 数据范围:$1 \le |s| \le 100$。 ## 算法分析 事实上,我们只需要算出把$s$分别变成全是$a, b, c, ..., z$所需要的次数,取最小值即可。 如何求出这个次数呢? 假设$s$包含字母$i$,$i$把$s$分割成了至少$2$段。对于每一段来说,把这一段全部变成$i$所需要的次数就是这一段的长度。因此,把$s$全部变成$i$的次数就是每一段长度的最大值。 ## 代码 ```cpp #include using namespace std; string s; int len, last[26], a[26], ans; int main() { cin >> s; len = ans = s.length(); for (int i = 0; i < len; ++i) { a[s[i] - 'a'] = max(a[s[i] - 'a'], i - last[s[i] - 'a']); last[s[i] - 'a'] = i + 1; } for (int i = 0; i < 26; ++i) { ans = min(ans, max(a[i], len - last[i])); } cout << ans << endl; return 0; } ```