307209: CF1321C. Remove Adjacent
Memory Limit:256 MB
Time Limit:2 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Remove Adjacent
题意翻译
给出字符串 $s$,$1\le|s|\le100$ 。 定义一次变换为,选中第 $i$ 个字符移除,字符串长度减少 $1$,选中字符需要满足 $s_{i-1},s_{i+1}$ 中至少有一个是 $s_i$ 的前驱。 求最多可进行几次操作。题目描述
You are given a string $ s $ consisting of lowercase Latin letters. Let the length of $ s $ be $ |s| $ . You may perform several operations on this string. In one operation, you can choose some index $ i $ and remove the $ i $ -th character of $ s $ ( $ s_i $ ) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $ s_i $ . For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $ i $ should satisfy the condition $ 1 \le i \le |s| $ during each operation. For the character $ s_i $ adjacent characters are $ s_{i-1} $ and $ s_{i+1} $ . The first and the last characters of $ s $ both have only one adjacent character (unless $ |s| = 1 $ ). Consider the following example. Let $ s= $ bacabcab. 1. During the first move, you can remove the first character $ s_1= $ b because $ s_2= $ a. Then the string becomes $ s= $ acabcab. 2. During the second move, you can remove the fifth character $ s_5= $ c because $ s_4= $ b. Then the string becomes $ s= $ acabab. 3. During the third move, you can remove the sixth character $ s_6= $ 'b' because $ s_5= $ a. Then the string becomes $ s= $ acaba. 4. During the fourth move, the only character you can remove is $ s_4= $ b, because $ s_3= $ a (or $ s_5= $ a). The string becomes $ s= $ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.输入输出格式
输入格式
The first line of the input contains one integer $ |s| $ ( $ 1 \le |s| \le 100 $ ) — the length of $ s $ . The second line of the input contains one string $ s $ consisting of $ |s| $ lowercase Latin letters.
输出格式
Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
输入输出样例
输入样例 #1
8
bacabcab
输出样例 #1
4
输入样例 #2
4
bcda
输出样例 #2
3
输入样例 #3
6
abbbbb
输出样例 #3
5