description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
le = int(input()) ar = list("0" + input() + "0") al = list("abcdefghijklmnopqrstuvwxyz") dic = {"0": -5} for i in range(1, 27): dic[al[i - 1]] = i count = 0 for i in range(le): li = 0 for i in range(1, len(ar) - 1): if dic[ar[i]] - dic[ar[i + 1]] == 1 or dic[ar[i]] - dic[ar[i - 1]] == 1: if dic[ar[li]] < dic[ar[i]]: li = i if li != 0: del ar[li] count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) l = input() l3 = [] for i in l: l3.append(ord(i)) c = 0 for z in range(123, 97, -1): for i in range(len(l)): for j in range(len(l3) - 1): if l3[j] == l3[j + 1] - 1: if l3[j + 1] == z: l3.pop(j + 1) c += 1 break elif l3[j + 1] == l3[j] - 1: if l3[j] == z: c += 1 l3.pop(j) break else: continue print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() ind = 0 cou = 0 while 1: mx = -1 ind = -1 for i in range(len(s)): if ( i - 1 >= 0 and ord(s[i]) == 1 + ord(s[i - 1]) or i + 1 < len(s) and ord(s[i]) == 1 + ord(s[i + 1]) ): if ord(s[i]) > mx: mx = ord(s[i]) ind = i if ind != -1: s = s[:ind] + s[ind + 1 :] cou += 1 else: break print(cou)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() a = [x for x in s] for j in range(25, 0, -1): for k in range(n): for i in range(len(a)): if a[i] == chr(97 + j) and ( i > 0 and a[i - 1] == chr(97 + j - 1) or i < len(a) - 1 and a[i + 1] == chr(97 + j - 1) ): a.pop(i) break print(n - len(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() def f(s, c, cc): m = len(s) if m == 1: return s l = [1] * m for i in range(m - 1): if s[i] == cc and s[i + 1] == c: l[i + 1] = 0 if s[i] == c and s[i + 1] == c: l[i + 1] = l[i] for i in range(m - 1, 0, -1): if s[i] == cc and s[i - 1] == c: l[i - 1] = 0 if s[i] == c and s[i - 1] == c: l[i - 1] = l[i] ret = "" for i in range(m): if l[i] == 1: ret += s[i] return ret for x in range(122, 97, -1): s = f(s, chr(x), chr(x - 1)) print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() ls = [s[i] for i in range(n)] ans = 0 for i in range(25): x = chr(97 + 26 - i - 1) y = chr(97 + 26 - i - 1 - 1) j = 0 while j < len(ls): if ls[j] == x: if j - 1 >= 0 and j + 1 < len(ls): if ls[j - 1] == y or ls[j + 1] == y: ans += 1 ls.pop(j) j -= 1 elif j - 1 >= 0: if ls[j - 1] == y: ans += 1 ls.pop(j) j -= 1 elif j + 1 < len(ls): if ls[j + 1] == y: ans += 1 ls.pop(j) j -= 1 j += 1 j = len(ls) - 1 while j >= 0: if ls[j] == x: if j - 1 >= 0 and j + 1 < len(ls): if ls[j - 1] == y or ls[j + 1] == y: ans += 1 ls.pop(j) elif j - 1 >= 0: if ls[j - 1] == y: ans += 1 ls.pop(j) elif j + 1 < len(ls): if ls[j + 1] == y: ans += 1 ls.pop(j) j -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
ans = 0 def check1(seq, i): if i == 0 or i == len(seq) - 1: return False if seq[i] - seq[i - 1] == 1 and seq[i] - seq[i + 1] == 2: return True if seq[i] - seq[i + 1] == 1 and seq[i] - seq[i - 1] == 2: return True if seq[i] - seq[i - 1] == 1 and seq[i] == seq[i + 1]: return True if seq[i] - seq[i + 1] == 1 and seq[i] == seq[i - 1]: return True return False def check2(seq, i): if i == 0 or i == len(seq) - 1: return True if seq[i] - seq[i - 1] == 1 and seq[i + 1] - seq[i] >= 2: return True if seq[i] - seq[i + 1] == 1 and seq[i - 1] - seq[i] >= 2: return True if seq[i] - seq[i - 1] == 1 and seq[i] - seq[i + 1] >= 3: return True if seq[i] - seq[i + 1] == 1 and seq[i] - seq[i - 1] >= 3: return True if seq[i] - seq[i - 1] == 1 and seq[i] - seq[i + 1] == 1: return True if seq[i] - seq[i + 1] == 1 and seq[i] - seq[i - 1] == 1: return True return False def check3(seq, i): if i == 0 or i == len(seq) - 1: return False if seq[i] - seq[i - 1] == 1 and seq[i + 1] - seq[i] == 1: return True if seq[i] - seq[i + 1] == 1 and seq[i - 1] - seq[i] == 1: return True return False def seq_to_str(seq): return "".join([chr(s + ord("a")) for s in seq]) def make_move(seq): global ans candidates = [] if len(seq) > 1: if seq[0] - seq[1] == 1: candidates.append(0) for i in range(1, len(seq) - 1): if seq[i] - seq[i + 1] == 1 or seq[i] - seq[i - 1] == 1: candidates.append(i) if seq[-1] - seq[-2] == 1: candidates.append(len(seq) - 1) if len(candidates) > 0: priopity1 = list() priopity2 = list() priopity3 = list() for cand in candidates: if check1(seq, cand): priopity1.append(cand) elif check2(seq, cand): priopity2.append(cand) elif check3(seq, cand): priopity3.append(cand) candidates_sorted = ( sorted(priopity1, key=lambda cand: seq[cand], reverse=True) + sorted(priopity2, key=lambda cand: seq[cand], reverse=True) + sorted(priopity3, key=lambda cand: seq[cand], reverse=True) ) to_del_index = candidates_sorted[0] new_seq = seq[0:to_del_index] + seq[to_del_index + 1 :] ans += 1 make_move(new_seq) _ = int(input()) seq = [(ord(s) - ord("a")) for s in input()] make_move(seq) print(ans)
ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR VAR FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() def remove(s): actC = "a" actI = -1 for i in range(len(s)): if i > 0: if ord(s[i]) - ord(s[i - 1]) == 1 and s[i] > actC: actC = s[i] actI = i if i < len(s) - 1: if ord(s[i]) - ord(s[i + 1]) == 1 and s[i] > actC: actC = s[i] actI = i return actC, actI ans = 0 actIdxToRem = remove(s)[1] while actIdxToRem != -1: s = s[:actIdxToRem] + s[actIdxToRem + 1 :] ans += 1 actIdxToRem = remove(s)[1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n, s = int(input()), input() for i in range(25): while 1: ch, pch = chr(ord("z") - i), chr(ord("z") - i - 1) ns = "" for p, c in enumerate(s): if c == ch and ( p > 0 and s[p - 1] == pch or p < len(s) - 1 and s[p + 1] == pch ): continue ns += s[p] if s == ns: break s = ns print(n - len(s))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() c = [] for x in s: c.append(ord(x)) MAX = None while MAX != -1 and len(c) != 1: MAX = max(c) success = False for i, x in enumerate(c): if MAX == x: if i == 0: if c[i + 1] == MAX - 1: del c[i] success = True break elif i == len(c) - 1: if c[i - 1] == MAX - 1: del c[i] success = True break elif MAX - 1 in [c[i - 1], c[i + 1]]: del c[i] success = True break if success == False: for i, x in enumerate(c): if x == MAX: c[i] = -1 print(n - len(c))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NONE WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def solve(s): d = 0 for ss in sorted(set(s), reverse=True): while True: n = len(s) ns = list() succ = False for idx in range(len(s)): if s[idx] == ss and ( s[max(0, idx - 1)] + 1 == s[idx] or s[min(n - 1, idx + 1)] + 1 == s[idx] ): d += 1 succ = True else: ns.append(s[idx]) s = ns if not succ: break return d N = int(input()) s = list(map(ord, list(input()))) print(solve(s))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def func(x): return ord(x) - ord("a") n = int(input()) a = list(map(func, list(input()))) count = [0] * 26 for i in a: count[i] += 1 ans = 0 for i in range(25, 0, -1): if count[i]: j = 0 while j < n: if a[j] == i and ( j > 0 and a[j - 1] == i - 1 or j < n - 1 and a[j + 1] == i - 1 ): ans += 1 a = a[:j] + a[j + 1 :] n -= 1 j = -1 j += 1 print(ans)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def index_to_remove(s): n = len(s) char = "" index = -1 for i in range(n): can_remove = False if i > 0: can_remove = ord(s[i]) - ord(s[i - 1]) == 1 if not can_remove and i < n - 1: can_remove = ord(s[i]) - ord(s[i + 1]) == 1 if can_remove: if s[i] > char: char = s[i] index = i return index n = int(input()) s = list(input()) i = index_to_remove(s) ans = 0 while i != -1: s.pop(i) ans += 1 i = index_to_remove(s) print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def i(): return input() def ii(): return int(input()) def iis(): return map(int, input().split()) def liis(): return list(map(int, input().split())) n = int(input()) s = input() a = [] for i in range(len(s)): a.append(s[i]) a = sorted(a)[::-1] count = 0 todos = set() for c in a: for j in range(n): for i in range(len(s)): if s[i] == c and ( i > 0 and ord(s[i - 1]) == ord(s[i]) - 1 or i < len(s) - 1 and ord(s[i + 1]) == ord(s[i]) - 1 ): s = s[:i] + s[i + 1 :] count += 1 break print(count)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() flag = True while flag: s1 = "" n1 = len(s) m = 95 ind = -1 for i in range(len(s)): k = 0 a = ord(s[i]) if i < n1 - 1: if ord(s[i + 1]) - a == -1: if a > m: m = a ind = i continue if i > 0: if ord(s[i - 1]) - a == -1: if a > m: m = a ind = i continue if ind == -1: break s = s[0:ind] + s[ind + 1 : n1] print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) def recursion(s, target=chr(ord("a") + 25)): if target == "a": return 0 else: count = 0 stack = [] hunterr = chr(ord(target) - 1) for i in s: if not stack: stack.append(i) elif i == hunterr and stack[-1] == target: while stack and stack[-1] == target: stack.pop() count += 1 stack.append(i) elif i == target and stack[-1] == hunterr: count += 1 else: stack.append(i) return count + recursion(stack, hunterr) print(recursion(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER IF VAR STRING RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) k = 0 answer = 0 while k == 0: k = 1 mas = [] for i in range(1, len(s)): if ord(s[i]) - 1 == ord(s[i - 1]): k = 0 mas.append([ord(s[i]), i]) elif ord(s[i]) + 1 == ord(s[i - 1]): k = 0 mas.append([ord(s[i - 1]), i - 1]) if mas: mas.sort() answer += 1 ind = mas[-1][1] s.pop(ind) print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) a = list(map(lambda x: ord(x) - ord("a"), input())) flag = True flag2 = False ans = n mx = max(a) while flag and len(a) and mx > 0: n = len(a) flag = False for i in range(n): if ( n >= 3 and i > 0 and i < n - 1 and (a[i + 1] == mx - 1 or a[i - 1] == mx - 1) and a[i] == mx ): idx = i flag = True break elif n >= 2 and i == 0 and a[i + 1] == mx - 1 and a[i] == mx: idx = i flag = True break elif n >= 2 and i == n - 1 and a[i - 1] == mx - 1 and a[i] == mx: idx = i flag = True if flag: a.pop(idx) else: mx -= 1 flag2 = True flag = True print(ans - len(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) x = input() l = [i for i in x] def getMaxInd() -> int: m = "_" loc = -1 for i in range(len(l) - 1): if abs(ord(l[i]) - ord(l[i + 1])) == 1: if ord(l[i]) > ord(m): m = l[i] loc = i if ord(m) < ord(l[i + 1]): m = l[i + 1] loc = i + 1 return loc q = getMaxInd() c = 0 while q != -1: l.pop(q) c = c + 1 q = getMaxInd() print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() res = 0 def f(s): idx = -1 maxx = -float("inf") ords = [ord(c) for c in s] for i, c in enumerate(s): if ( i + 1 < len(s) and ords[i] == ords[i + 1] + 1 or i - 1 >= 0 and ords[i] == ords[i - 1] + 1 ): if ords[i] > maxx: maxx = ords[i] idx = i return s[:idx] + s[idx + 1 :] if maxx != -float("inf") and idx != -1 else s while s: ss = f(s) if s == ss: break s = ss res += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) u = list(input()) lft = list(range(-1, n)) rht = list(range(1, n)) + [-1] d = [] for i in range(97, 97 + 26): d.append(chr(i)) d.reverse() ans = 0 for k in range(len(d) - 1): for j in range(n): for i in range(n): if u[i] != d[k]: continue i1 = lft[i] j1 = rht[i] if i1 != -1 and u[i1] == d[k + 1] or j1 != -1 and u[j1] == d[k + 1]: u[i] = -1 ans += 1 if lft[i] != -1 and rht[i] != -1: rht[lft[i]], lft[rht[i]] = rht[i], lft[i] elif lft[i] != -1: rht[lft[i]] = rht[i] elif rht[i] != -1: lft[rht[i]] = lft[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) l = list(input()) ct = [] x = "" i = 0 while i < n: t = l[i] x = x + l[i] c = 0 while t == l[i]: c = c + 1 i = i + 1 if i == n: break ct.append(c) l = list(x) ans = 0 f = 1 while f: f = 0 m = "a" mi = 0 for i in range(1, len(l)): if abs(ord(l[i]) - ord(l[i - 1])) == 1: if max(ord(l[i]), ord(l[i - 1])) > ord(m): if ord(l[i]) > ord(l[i - 1]): m = l[i] mi = i else: m = l[i - 1] mi = i - 1 f = 1 if f: ans = ans + ct[mi] l.pop(mi) ct.pop(mi) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input().strip()) se = list(set(s)) c = 0 se.sort(reverse=True) m = n for j in range(len(se)): n = m - c k = 1 while k < n: if s[k] == se[j]: if ord(s[k - 1]) + 1 == ord(s[k]): del s[k] c = c + 1 n = n - 1 else: k = k + 1 else: k = k + 1 n = m - c for k in range(n - 2, -1, -1): if s[k] == se[j]: if ord(s[k + 1]) + 1 == ord(s[k]): del s[k] c = c + 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) s = [(ord(x) - ord("a")) for x in s] l = [[] for i in range(26)] ans = 0 for i in range(25, -1, -1): k = 0 while k < len(s): if s[k] == i: if k - 1 >= 0 and s[k] - 1 == s[k - 1]: s.pop(k) k -= 1 ans += 1 if k + 1 < len(s) and s[k] - 1 == s[k + 1]: s.pop(k) k = -1 ans += 1 k += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n = mint() i = 0 s = [0] * n for c in minp(): s[i] = ord(c) - ord("a") i += 1 for c in range(25, 0, -1): i = 0 while i < len(s): if s[i] == c and ( i > 0 and s[i - 1] == c - 1 or i + 1 < len(s) and s[i + 1] == c - 1 ): s.pop(i) if i > 0: i -= 1 else: i += 1 print(n - len(s)) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() def sol(s): n = len(s) if n == 1: return 0 else: ans = 0 m = "a" ind = -1 for i in range(n): f, b = min(n - 1, i + 1), max(0, i - 1) if ord(s[i]) - ord(s[f]) == 1 or ord(s[i]) - ord(s[b]) == 1: if s[i] > m: m = s[i] ind = i if ind == -1: return 0 elif ind == 0: v = 1 + sol(s[ind + 1 :]) elif ind == n - 1: v = 1 + sol(s[: n - 1]) else: v = 1 + sol(s[:ind] + s[ind + 1 :]) return v print(sol(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) str = input() lst = list(str) for i in range(122, 97, -1): for k in range(100): l = len(lst) tmp = [] for j in range(l): if ord(lst[j]) == i: if ( j + 1 < l and ord(lst[j + 1]) == i - 1 or j - 1 >= 0 and ord(lst[j - 1]) == i - 1 ): tmp.append(j) bul = True l = len(tmp) tmp.reverse() for j in tmp: del lst[j] print(n - len(lst))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() l = [ord(i) for i in s] cnt = 0 while True: mx = -3 flag = True i = 0 index = -1 while i < n: if i - 1 >= 0 and l[i] - l[i - 1] == 1 and l[i] >= mx: mx = l[i] index = i flag = False if i + 1 < n and l[i] - l[i + 1] == 1 and l[i] >= mx: mx = l[i] index = i flag = False i += 1 if flag: break l[index] = -10 l.remove(-10) n -= 1 cnt += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) for i in range(n): s[i] = ord(s[i]) - 97 ch = [i for i in range(26, -1, -1)] s = [-100] + s + [-100] for i in ch: j = 1 while j < len(s) - 1: if s[j] == i: if s[j] == s[j - 1] + 1 or s[j] == s[j + 1] + 1: s.pop(j) j = 1 else: j += 1 else: j += 1 print(n + 2 - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() s_nums = [ord(s[i]) for i in range(n)] ans = 0 for biggest_number in range(ord("z"), ord("a"), -1): to_delete = [(False) for _ in range(len(s_nums))] for j in range(len(s_nums)): if s_nums[j] == biggest_number: if len(s_nums) == 1: pass elif j == 0: if s_nums[j + 1] == biggest_number - 1: to_delete[j] = True elif j == len(s_nums) - 1: if s_nums[j - 1] == biggest_number - 1 or to_delete[j - 1]: to_delete[j] = True elif ( s_nums[j - 1] == biggest_number - 1 or s_nums[j + 1] == biggest_number - 1 or to_delete[j - 1] ): to_delete[j] = True for j in range(len(s_nums) - 1, -1, -1): if s_nums[j] == biggest_number: if len(s_nums) == 1: pass elif j == 0: if s_nums[j + 1] == biggest_number - 1 or to_delete[j + 1]: to_delete[j] = True elif j == len(s_nums) - 1: if s_nums[j - 1] == biggest_number - 1 or to_delete[j - 1]: to_delete[j] = True elif ( s_nums[j - 1] == biggest_number - 1 or s_nums[j + 1] == biggest_number - 1 or to_delete[j + 1] ): to_delete[j] = True count_deleted = 0 len_s_nums_before = len(s_nums) for j in range(len_s_nums_before): if to_delete[j]: s_nums = s_nums[: j - count_deleted] + s_nums[j + 1 - count_deleted :] ans += 1 count_deleted += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) l = list(input()) a = [0] * 26 b = [0] * 26 for i in range(n): a[ord(l[i]) - 97] = 1 b[ord(l[i]) - 97] += 1 ans = 0 for i in range(25, -1, -1): if a[i] == 1: while True: j = 0 while j < len(l): if l[j] == chr(i + 97): if j > 0 and l[j - 1] == chr(i + 96): l.pop(j) ans += 1 b[i] -= 1 j -= 1 elif j < len(l) - 1 and l[j + 1] == chr(i + 96): l.pop(j) ans += 1 b[i] -= 1 j -= 1 j += 1 j = len(l) - 1 while j >= 0: if l[j] == chr(i + 97): if j > 0 and l[j - 1] == chr(i + 96): l.pop(j) ans += 1 b[i] -= 1 elif j < len(l) - 1 and l[j + 1] == chr(i + 96): l.pop(j) ans += 1 b[i] -= 1 j -= 1 break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ n = int(input()) s = [ord(c) for c in input()] ans = 0 code = 122 while code >= 97: toDel = [] k = len(s) for i, c in enumerate(s): if c != code: continue if i > 0 and s[i - 1] + 1 == code: toDel.append(i - len(toDel)) elif i < k - 1 and s[i + 1] + 1 == code: toDel.append(i - len(toDel)) if toDel: for i in toDel: del s[i] ans += 1 else: code -= 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR FOR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input().strip() l = [(ord(x) - ord("a")) for x in s] cnt = 0 while True: mi = None if n - cnt == 1: break if l[0] - 1 == l[1]: mi = 0 for i in range(n - 1 - cnt): if l[i - 1] + 1 == l[i] or l[i + 1] + 1 == l[i]: if mi == None or l[mi] < l[i]: mi = i if l[n - 2 - cnt] + 1 == l[n - 1 - cnt]: mi = n - 1 - cnt if mi == None: break l.pop(mi) cnt += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NONE IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NONE VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ t = int(input()) word = input() res = 0 for ordi in range(25): ordinit = 122 - ordi while True: n = len(word) toremove = [] if n == 1: break for i in range(n): if ord(word[i]) == ordinit: if i == 0: if ord(word[i]) == ord(word[i + 1]) + 1: if not toremove or toremove[-1] != i: toremove.append(i) elif i == n - 1: if ord(word[i]) == ord(word[i - 1]) + 1: if not toremove or toremove[-1] != i: toremove.append(i) elif ( ord(word[i]) == ord(word[i + 1]) + 1 or ord(word[i]) == ord(word[i - 1]) + 1 ): if not toremove or toremove[-1] != i: toremove.append(i) res += len(toremove) if not toremove: break counter = 0 for i in toremove: word = word[: i - counter] + word[i - counter + 1 :] counter += 1 print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() d = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] s = list(s) ans = 0 for cr in range(len(d) - 1, 0, -1): c = 0 k = 0 while c < n - k: if not s: break if c - 1 >= 0 and s[c] == d[cr] and s[c - 1] == d[cr - 1]: s.pop(c) c = max(c - 1, 0) n -= 1 ans += 1 elif c + 1 < n and s[c] == d[cr] and s[c + 1] == d[cr - 1]: s.pop(c) c = max(c - 1, 0) n -= 1 ans += 1 else: c += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) count = 0 temp = "*" for i in range(0, 25): to_remove = chr(ord("z") - i) adj = chr(ord("y") - i) prev = 0 while s[prev] == temp: prev = prev + 1 for j in range(prev + 1, n): if s[j] == temp: continue if s[prev] == adj and s[j] == to_remove: s[j] = temp count += 1 else: prev = j prev = n - 1 while s[prev] == temp: prev -= 1 for j in range(prev - 1, -1, -1): if s[j] == temp: continue if s[prev] == adj and s[j] == to_remove: s[j] = temp count += 1 else: prev = j print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def find(n): arr = [ord(i) for i in n] ans = 0 for i in range(97 + 26, 96, -1): while True: temp = -1 for j in range(len(arr)): if arr[j] == i and ( arr[max(0, j - 1)] == i - 1 or arr[min(len(arr) - 1, j + 1)] == i - 1 ): temp = j if temp != -1: ans += 1 arr.pop(temp) else: break return ans x = int(input()) n = input() print(find(n))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = str(input()) s = list(s) n = len(s) count = [[] for i in range(26)] for i in range(n): count[ord(s[i]) - ord("a")].append(i) start = 25 total = 0 while True: if start <= 0: break if len(count[start]) == 0: start = start - 1 continue for i in range(len(count[start])): indexStart = count[start][i] indexBack = indexStart - 1 indexFront = indexStart + 1 b1 = 0 b2 = 0 while indexBack >= 0: if s[indexBack] != "." and ord(s[indexBack]) - ord("a") != start: b1 = 1 break indexBack = indexBack - 1 while indexFront < n: if s[indexFront] != "." and ord(s[indexFront]) - ord("a") != start: b2 = 1 break indexFront = indexFront + 1 if b1 == 1: if ord(s[indexBack]) - ord("a") == start - 1: s[indexStart] = "." total = total + 1 continue if b2 == 1: if ord(s[indexFront]) - ord("a") == start - 1: s[indexStart] = "." total = total + 1 4 start = start - 1 print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR STRING BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = [x for x in input()] alpha = [x for x in " abcdefghijklmnopqrstuvwxyz"] while len(s) > 1: maxvalue = "a" maxindex = -1 for i, x in enumerate(s): if x >= maxvalue: flag = False if i == 0: if s[i + 1] == alpha[alpha.index(x) - 1]: flag = True elif i == len(s) - 1: if s[i - 1] == alpha[alpha.index(x) - 1]: flag = True elif ( s[i - 1] == alpha[alpha.index(x) - 1] or s[i + 1] == alpha[alpha.index(x) - 1] ): flag = True if flag: maxvalue = x maxindex = i if maxindex == -1: break else: s.pop(maxindex) print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR STRING WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
N = int(input()) S = list(input()) res = 0 for i in range(26): idx = 0 found = True while found: found = False while idx < len(S): if S[idx] == chr(ord("z") - i): if ( idx + 1 < len(S) and ord(S[idx]) - ord(S[idx + 1]) == 1 or idx - 1 >= 0 and ord(S[idx]) - ord(S[idx - 1]) == 1 ): S.pop(idx) res += 1 found = True else: idx += 1 else: idx += 1 idx = 0 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def func(a): if len(a) == 1: return a ral = 0 a = "(" + a + ")" flag = True for i in range(1, len(a) - 1): p = i if chr(ord(a[p]) - 1) == a[p + 1] or chr(ord(a[p]) - 1) == a[p - 1]: if ral <= ord(a[p]): ral = ord(a[p]) k = p flag = False a = a[1 : len(a) - 1] if flag == False: k = k - 1 a = func(a[0:k] + a[k + 1 :]) return a n = int(input()) a = input() a = func(a) print(n - len(a))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys I = lambda: int(input()) readline = lambda: sys.stdin.readline().strip("\n") RM = readmap = lambda x=int: map(x, readline().split(" ")) n, s = I(), readline() for i in range(25, 0, -1): y = chr(ord("a") + i) x = chr(ord("a") + i - 1) while True: temp = len(s) s = s.replace(x + y, x).replace(y + x, x) if len(s) == temp: break print(n - len(s)) quit() d = {} for i, x in zip(range(I()), RM()): d[x - i] = d.get(x - i, 0) + x print(max(d.values())) quit() n, a, b = I(), [*RM()], [*RM()] count1, count2 = 0, 0 for i, j in zip(a, b): count1 += i & ~j count2 += j & ~i print(divmod(count2, count1)[0] + 1 if count1 else -1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR LIST FUNC_CALL VAR LIST FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
lengthofstring = int(input()) string1 = [(ord(k) - 97) for k in input()] traverse1 = list(range(26)) traverse1 = traverse1[::-1] + [] maxinstring = max(string1) answer = 0 for k in range(26): letter = traverse1[k] if letter <= maxinstring: cv = 0 while cv <= len(string1) - 1 and len(string1) >= 2: if string1[cv] == letter: if cv >= 1 and cv <= len(string1) - 2: if string1[cv - 1] == letter - 1 or string1[cv + 1] == letter - 1: string1.pop(cv) answer += 1 else: cv += 1 elif cv == 0: if string1[cv + 1] == letter - 1: string1.pop(cv) answer += 1 else: cv += 1 elif cv == len(string1) - 1: if string1[cv - 1] == letter - 1: string1.pop(cv) answer += 1 else: cv += 1 else: cv += 1 else: cv += 1 cv1 = len(string1) - 1 while cv1 >= 0 and len(string1) >= 2: if string1[cv1] == letter: if cv1 >= 1 and cv1 <= len(string1) - 2: if string1[cv1 - 1] == letter - 1 or string1[cv1 + 1] == letter - 1: string1.pop(cv1) answer += 1 else: cv1 += -1 elif cv1 == 0: if string1[cv1 + 1] == letter - 1: string1.pop(cv1) answer += 1 else: cv1 += -1 elif cv == len(string1) - 1: if string1[cv1 - 1] == letter - 1: string1.pop(cv1) answer += 1 else: cv1 += -1 else: cv1 += -1 else: cv1 += -1 print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
input() s = list(input()) orig = len(s) changed = True while changed: changed = False for c in sorted(list(set(s)), reverse=True): for i in range(len(s)): if s[i] == c and ( i - 1 >= 0 and ord(s[i - 1]) == ord(s[i]) - 1 or i + 1 < len(s) and ord(s[i + 1]) == ord(s[i]) - 1 ): s = s[:i] + s[i + 1 :] changed = True break if changed: break print(orig - len(s))
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = sys.stdin.readline n = int(input()) s = ["0"] + list(input()) for i in range(25, -1, -1): t = chr(97 + i) while True: d = [] for j in range(len(s)): if s[j] == t and ( s[j - 1] == chr(97 + i - 1) or s[j + 1] == chr(97 + i - 1) ): d.append(j) for j in range(len(d)): d[j] -= j for e in d: del s[e] if not d: break print(n - len(s) + 2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) arr = [199999] + [ord(i) for i in input()] + [0] ans = 0 for i in "abcdefghijklmnopqrstuvwxyz"[::-1]: rem = [] j = 1 while arr[j] != 0: if ord(i) == arr[j] and (arr[j] - 1 == arr[j - 1] or arr[j] - 1 == arr[j + 1]): arr.pop(j) j -= 1 ans += 1 else: j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR STRING NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() a = [ord(c) for c in s] for rmv in sorted(set(a), reverse=True): changed = True while changed: changed = False for i, v in enumerate(a): if v == rmv: check = a[i - 1] if i != 0 else None, ( a[i + 1] if i != len(a) - 1 else None ) if v - 1 in check: changed = True a[i] = None break if changed: a = list(filter(lambda x: x, a)) print(n - len(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NONE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NONE IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NONE IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
leng = int(input()) word = list(input()) best = 0 count = 0 while best != -1: best = -1 best_i = -1 for i in range(len(word)): c = word[i] if ord(c) > best: for x in [i - 1, i + 1]: if 0 <= x < len(word) and ord(word[x]) == ord(c) - 1: best = ord(c) best_i = i if best_i != -1: word.pop(best_i) count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = input() s = input() s = [char for char in s] alphabet = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] inst = [] go = True no = [] for a in range(25, 0, -1): go = True while go: inst = [] for b in range(len(s)): if s[b] == alphabet[a]: inst.append(b) go = False no = [] for i in inst: if i != 0 and s[i - 1] == alphabet[a - 1]: no.append(i) go = True elif i != len(s) - 1 and s[i + 1] == alphabet[a - 1]: no.append(i) go = True for i in range(len(no) - 1, -1, -1): s.pop(no[i]) print(str(int(n) - len(s)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys n = int(input()) s = str(input()) flag = True while len(s) > 1: maxChar, pos = chr(ord("a") - 1), -1 for i in range(len(s)): if maxChar < s[i]: if i == 0: if ord(s[i]) == ord(s[i + 1]) + 1: maxChar = s[i] pos = i elif i == len(s) - 1: if ord(s[i]) == ord(s[i - 1]) + 1: maxChar = s[i] pos = i elif ord(s[i]) == ord(s[i + 1]) + 1 or ord(s[i]) == ord(s[i - 1]) + 1: maxChar = s[i] pos = i if pos != -1: s = s[:pos] + s[pos + 1 :] else: break print(n - len(s))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
from sys import stdin inf = stdin n = int(inf.readline()) s = inf.readline() s = s[:n] maxyet = 0 while maxyet != -1 and len(s) > 1: maxyet = -1 nn = len(s) for i in range(nn): if ( i > 0 and ord(s[i]) - 1 == ord(s[i - 1]) or i < nn - 1 and ord(s[i + 1]) + 1 == ord(s[i]) ): if maxyet != -1: if s[i] > s[maxyet]: maxyet = i else: maxyet = i if maxyet != -1: s = s[:maxyet] + s[maxyet + 1 :] print(n - len(s))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = sys.stdin.readline n = int(input()) s = [ord(i) for i in input()] mv = ord("z") out = 0 for i in range(25): cv = mv - i pv = mv - i - 1 j = 0 ls = len(s) while j < ls: if s[j] == cv and (j > 0 and s[j - 1] == pv or j < ls - 1 and s[j + 1] == pv): s = s[:j] + s[j + 1 :] j = 0 out += 1 ls -= 1 else: j += 1 print(out)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def removeadj(l): removes = 0 while 1: maxE = 1 maxI = -1 if len(l) == 1: print(removes) return for i in range(len(l)): check = False if i == 0: if l[i] == l[i + 1] + 1: check = True if i == len(l) - 1: if l[i] == l[i - 1] + 1: check = True elif l[i] == l[i - 1] + 1 or l[i] == l[i + 1] + 1: check = True if check: if l[i] > maxE: maxE = l[i] maxI = i if maxI == -1: print(removes) return else: del l[maxI] removes += 1 n = int(input()) s = input() l = [(ord(i) - 96) for i in s] removeadj(l)
FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() alpha = "abcdefghijklmnopqrstuvwxyz" count = 0 while len(s) > 1: for j in range(len(alpha) - 1, 0, -1): if len(s) == 1: break ele = alpha[j - 1] + alpha[j] while ele in s or ele[::-1] in s: i = 0 while i < len(s): if s[i] == alpha[j]: if i == 0 and s[i + 1] == alpha[j - 1]: count += 1 s = s[1:] elif i == len(s) - 1: if s[i - 1] == alpha[j - 1]: count += 1 s = s[: len(s) - 1] elif s[i + 1] == alpha[j - 1] or s[i - 1] == alpha[j - 1]: count += 1 s = s[:i] + s[i + 1 :] i += 1 v1 = 0 for i in range(len(s) - 1): if abs(ord(s[i]) - ord(s[i + 1])) == 1: v1 += 1 if v1 == 0: break print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): n = II() s = SI() aa = [100] + [(ord(c) - 97) for c in s] + [100] for code in range(25, 0, -1): i = 1 while i < len(aa): if aa[i] == code and (aa[i - 1] == code - 1 or aa[i + 1] == code - 1): del aa[i] if aa[i] == code - 1: i -= 1 else: i += 1 print(n + 2 - len(aa)) main()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() l = [ord(i) for i in s] maxa = 0 numofc = 1 if n == 1: print(0) else: while 1: numofc = 0 lo = [] if len(l) > 1: if l[0] == l[1] + 1: lo.append((l[0], 0)) if l[-1] == l[-2] + 1: lo.append((l[-1], len(l) - 1)) for i in range(1, len(l) - 1): if l[i - 1] == l[i] - 1 or l[i + 1] == l[i] - 1: lo.append((l[i], i)) numofc = len(lo) if numofc > 0: maxa = max(lo) l.pop(maxa[1]) else: break else: break print(len(s) - len(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() if n == 1: print(0) exit() for i in range(122, 96, -1): for k in range(len(s)): arr = [] for j in range(len(s)): c = chr(i) if j == 0 and ord(s[j + 1]) + 1 == ord(s[j]) == i: arr += [j] elif j == len(s) - 1 and ord(s[j - 1]) + 1 == ord(s[j]) == i: arr += [j] elif ( j != 0 and j != len(s) - 1 and ( ord(s[j + 1]) + 1 == ord(s[j]) == i or ord(s[j - 1]) + 1 == ord(s[j]) == i ) ): arr += [j] new = "" for j in range(len(s)): if j in arr: continue new += s[j] s = new if len(s) == 1: break if len(s) == 1: break print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR LIST VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR LIST VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def charToInt(c): return ord(c) - ord("a") N = int(input()) s = input() arr = [charToInt(c) for c in s] didRemove = False j = 25 while True: didRemove = False n = len(arr) for i in range(n - 1, -1, -1): if arr[i] == j and ( i - 1 >= 0 and arr[i - 1] + 1 == arr[i] or i + 1 < n and arr[i + 1] + 1 == arr[i] ): arr.pop(i) didRemove = True break if didRemove: j = 25 else: j -= 1 if j == 0: break print(N - n)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def is_right_less(s, i): return i + 1 < len(s) and ord(s[i]) - ord(s[i + 1]) == 1 def is_left_less(s, i): return i - 1 >= 0 and ord(s[i]) - ord(s[i - 1]) == 1 def sort_dict(p: dict) -> dict: temp = {k: v for k, v in sorted(p.items(), key=lambda item: item[1])} x = list(temp.items()) x.reverse() return {k: v for k, v in x} def check_neighbours(s, i): if is_left_less(s, i) or is_right_less(s, i): return i else: return -1 def remove_neighbors(s: str) -> int: s = list(s) removed = 0 while len(s) != 0: candidates = {} for j in range(len(s)): c = check_neighbours(s, j) if c >= 0: candidates[c] = s[c] candidates = sort_dict(candidates) if len(candidates) > 0: s.pop(list(candidates.items())[0][0]) removed += 1 else: break return removed len_s = int(input()) s = input() print(remove_neighbors(s))
FUNC_DEF RETURN BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR VAR VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) for i in range(n): l = [] for j in range(len(s)): if j > 0 and ord(s[j]) == ord(s[j - 1]) + 1: l.append([s[j], j]) if j < len(s) - 1 and ord(s[j]) == ord(s[j + 1]) + 1: l.append([s[j], j]) if len(l) == 0: break l.sort() s1 = "" for i in range(len(s)): if i == l[-1][1]: continue s1 += s[i] s = s1[:] a = "" for i in s: if ord(i) == 13: continue a += i print(n - len(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() if n == 1: print(0) exit() l = [] for i in s: l.append(ord(i) - 97) f = 1 while f: o = [] m = -1 d = -1 i = 0 f = 0 if len(l) == 1: break while i < len(l): if i == 0: if l[i] - 1 == l[i + 1]: if l[i] > m: f = 1 m = l[i] d = i i = i + 1 continue if i == len(l) - 1: if l[i] - 1 == l[i - 1]: if l[i] > m: f = 1 m = l[i] d = i i = i + 1 continue if l[i] - 1 == l[i - 1] or l[i] - 1 == l[i + 1]: if l[i] > m: f = 1 m = l[i] d = i i = i + 1 for i in range(len(l)): if i == d: continue o.append(l[i]) l = o print(n - len(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) letters = [0] * 26 for c in s: letters[ord(c) - ord("a")] += 1 it = 25 def can_be_deleted(s, i): if i - 1 >= 0 and i + 1 < len(s): return ord(s[i - 1]) + 1 == ord(s[i]) or ord(s[i + 1]) + 1 == ord(s[i]) if i == 0: return ord(s[i + 1]) + 1 == ord(s[i]) if i == len(s) - 1: return ord(s[i - 1]) + 1 == ord(s[i]) score = 0 while it >= 0: if letters[it] == 0: it -= 1 continue i = 0 deleted = False while i < len(s) and len(s) > 1: if s[i] == chr(ord("a") + it) and can_be_deleted(s, i): del s[i] score += 1 deleted = True break i += 1 if not deleted: it -= 1 print(score)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) x = input() def remove(tmp, x): removed = 0 if len(tmp) >= 2 and tmp[0] == x and tmp[1] == x - 1: removed += 1 tmp.pop(0) i = 0 n = len(tmp) while i < n: if i == 0: if len(tmp) >= 2 and tmp[0] == x and tmp[1] == x - 1: removed += 1 tmp.pop(0) n -= 1 continue elif i == n - 1: if len(tmp) >= 2 and tmp[-1] == x and tmp[-2] == x - 1: removed += 1 tmp.pop(len(tmp) - 1) n -= 1 continue elif tmp[i] == x and (tmp[i + 1] == x - 1 or tmp[i - 1] == x - 1): removed += 1 tmp.pop(i) i -= 1 n -= 1 continue i += 1 return removed def solve(x, n): out = 0 tmp = [ord(e) for e in x] chars = list(set(tmp[:])) chars.sort(reverse=True) for char in chars: out += remove(tmp, char) print(out) solve(x, n)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
l = int(input()) s = input() ans = 0 for i in range(l): pos = "a", -1 n = len(s) for j in range(n): if j > 0: if ord(s[j - 1]) == ord(s[j]) - 1: pos = max(pos, (s[j], j)) if j < n - 1: if ord(s[j + 1]) == ord(s[j]) - 1: pos = max(pos, (s[j], j)) if pos[1] != -1: s = s[: pos[1]] + s[pos[1] + 1 :] ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def is_right_less(s, i): return i + 1 < len(s) and ord(s[i]) - ord(s[i + 1]) == 1 def is_left_less(s, i): return i - 1 >= 0 and ord(s[i]) - ord(s[i - 1]) == 1 def check_neighbours(s, i): if is_left_less(s, i) or is_right_less(s, i): return i else: return -10 def remove_neighbors(s: str) -> int: s = list(s) removed = 0 while len(s) != 0: biggest_cand = -1 remove_id = -1 k = [] for j in range(len(s)): c = check_neighbours(s, j) if c >= 0 and ord(s[c]) >= biggest_cand: k.append({c: s[c]}) biggest_cand = ord(s[c]) remove_id = c if remove_id >= 0: s.pop(remove_id) removed += 1 else: break return removed len_s = int(input()) s = input() print(remove_neighbors(s))
FUNC_DEF RETURN BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def rem(l, n): remo = [] for i in range(1, n + 1): if l[i] == l[i - 1] + 1 or l[i] == l[i + 1] + 1: remo.append((l[i], i)) remo.sort() return remo n = int(input()) l = [ord(i) for i in "$" + input() + "$"] removed = rem(l, n) x = 0 while removed: l.pop(removed[-1][1]) x += 1 removed = rem(l, n - x) print(x)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() for i in range(25): ch = chr(ord("z") - i) prev = chr(ord(ch) - 1) j = 0 while j < len(s): if s[j] == ch and ( j - 1 >= 0 and s[j - 1] == prev or j + 1 < len(s) and s[j + 1] == prev ): s = s[:j] + s[j + 1 :] j = 0 else: j += 1 print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = [sym for sym in input()] unicode_code_point = 122 the_num_of_deletions = 0 s_copy = s.copy() while unicode_code_point != 97: char = chr(unicode_code_point) previous_char = chr(unicode_code_point - 1) ind = 0 while ind < len(s_copy): if s_copy[ind] == char: if 0 < ind < len(s_copy) - 1 and ( s_copy[ind - 1] == previous_char or s_copy[ind + 1] == previous_char ): del s_copy[ind] the_num_of_deletions += 1 elif 0 == ind < len(s_copy) - 1 and s_copy[ind + 1] == previous_char: del s_copy[ind] the_num_of_deletions += 1 elif 0 < ind == len(s_copy) - 1 and s_copy[ind - 1] == previous_char: del s_copy[ind] the_num_of_deletions += 1 else: ind += 1 else: ind += 1 ind = len(s_copy) - 1 while ind != -1: if s_copy[ind] == char: if 0 < ind < len(s_copy) - 1 and ( s_copy[ind - 1] == previous_char or s_copy[ind + 1] == previous_char ): del s_copy[ind] the_num_of_deletions += 1 elif 0 == ind < len(s_copy) - 1 and s_copy[ind + 1] == previous_char: del s_copy[ind] the_num_of_deletions += 1 elif 0 < ind == len(s_copy) - 1 and s_copy[ind - 1] == previous_char: del s_copy[ind] the_num_of_deletions += 1 ind -= 1 unicode_code_point -= 1 print(the_num_of_deletions)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
m = int(input()) s = input() + "0" num = [] le = len(s) co = 1 for i in range(le - 1): if s[i] != s[i + 1]: num.append([ord(s[i]), co]) co = 1 else: co += 1 li = len(num) mi = min(num) i2 = mi[0] for l in range(li): ma = max(num) i1 = num.index(ma) le = len(num) if le < 2 or i2 == ma[0]: break elif i1 == 0: if num[i1][0] == num[i1 + 1][0] + 1: num.remove(ma) else: num[i1][0] -= 90 elif i1 == le - 1: if num[i1][0] == num[i1 - 1][0] + 1: num.remove(ma) else: num[i1][0] -= 90 elif num[i1][0] == num[i1 + 1][0] + 1 or num[i1][0] == num[i1 - 1][0] + 1: num.remove(ma) else: num[i1][0] -= 90 numd = [] num.append([1, 0]) lc = len(num) for i in range(lc - 1): if num[i][0] == num[i + 1][0]: num[i][1] = num[i][1] + num[i + 1][1] num[i + 1] = num[i] else: numd.append(num[i]) num = numd[:] to = 0 for i in num: to += i[1] print(m - to)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = list(input()) for i in range(ord("z"), ord("a") - 1, -1): j = 0 while j < len(s): if len(s) <= j: break if j == len(s) - 1: if s[j] == chr(i) and s[j - 1] == chr(i - 1): k = s.pop(j) j = 0 continue elif j == 0: if s[j] == chr(i) and s[j + 1] == chr(i - 1): k = s.pop(j) j = 0 continue elif s[j] == chr(i) and (s[j - 1] == chr(i - 1) or s[j + 1] == chr(i - 1)): k = s.pop(j) j = 0 continue j = j + 1 print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() now = ord("z") st = ord("a") tong = [] for i in range(144): tong.append(0) for c in s: tong[ord(c)] += 1 ans = 0 while now > st: pd = 0 while 1: pd = 0 i = 0 while i < len(s): if ord(s[i]) == now and ( i - 1 >= 0 and ord(s[i - 1]) == now - 1 or i + 1 < len(s) and ord(s[i + 1]) == now - 1 ): s = s[0:i] + s[i + 1 : len(s)] tong[now] -= 1 pd = 1 ans += 1 i += 1 if pd == 0: break now -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() z = list(range(0, 26))[::-1] ords = [(ord(i) - ord("a")) for i in s] for i in z: pos = len(ords) - 1 while pos >= 0: if ords[pos] == i: if ( pos - 1 >= 0 and ords[pos - 1] == i - 1 or pos + 1 < len(ords) and ords[pos + 1] == i - 1 ): del ords[pos] if len(ords) == pos: pos -= 1 else: pos -= 1 else: pos -= 1 print(n - len(ords))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import gc class Node: def __init__(self, data): self.data = data self.next = None self.prev = None class DoublyLinkedList: def __init__(self): self.head = None def push(self, new_data): new_node = Node(new_data) new_node.next = self.head if self.head is not None: self.head.prev = new_node self.head = new_node def insertAfter(self, prev_node, new_data): if prev_node is None: print("the given previous node cannot be NULL") return new_node = Node(new_data) new_node.next = prev_node.next prev_node.next = new_node new_node.prev = prev_node if new_node.next is not None: new_node.next.prev = new_node def append(self, new_data): new_node = Node(new_data) new_node.next = None if self.head is None: new_node.prev = None self.head = new_node return last = self.head while last.next is not None: last = last.next last.next = new_node new_node.prev = last return def deleteNode(self, dele): if self.head is None or dele is None: return if self.head == dele: self.head = dele.next if dele.next is not None: dele.next.prev = dele.prev if dele.prev is not None: dele.prev.next = dele.next gc.collect() def printList(self, node): while node is not None: print(node.data, end=" ") node = node.next print() llist = DoublyLinkedList() llist.append(-2) llist.append(-2) n = int(input()) curVal = -1 for i in input(): val = ord(i) - ord("a") llist.append(val) curVal = max(curVal, val) llist.append(-2) llist.append(-2) initLen = n curLen = n while curVal > 0: temp = llist.head while temp != None: if temp.data == curVal and (temp.next != None and temp.next.data == curVal - 1): temp = temp.prev llist.deleteNode(temp.next) curLen -= 1 temp = temp.prev elif temp.data == curVal and ( temp.prev != None and temp.prev.data == curVal - 1 ): temp = temp.prev llist.deleteNode(temp.next) curLen -= 1 temp = temp.next curVal -= 1 print(initLen - curLen)
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE IF VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR RETURN ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE IF VAR VAR VAR NONE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NONE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = sys.stdin.readline def main(): n = int(input()) s = "&" + input() s += "&" ans = 0 for i in range(97 + 26 - 1, 96, -1): j = 0 while j < len(s) - 1: if ord(s[j]) == i: if ord(s[j - 1]) + 1 == ord(s[j]) or ord(s[j + 1]) + 1 == ord(s[j]): s = s[:j] + s[j + 1 :] ans += 1 else: j += 1 else: j += 1 j = len(s) - 1 while j > 0: if ord(s[j]) == i: if ord(s[j - 1]) + 1 == ord(s[j]) or ord(s[j + 1]) + 1 == ord(s[j]): s = s[:j] + s[j + 1 :] ans += 1 else: j -= 1 else: j -= 1 print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() if n == 1: print(0) else: alph = "abcdefghijklmnopqrstuvwxyz" while True: if len(s) == 1: break mx = "a" index = -1 for i in range(1, len(s) - 1): if s[i] > mx and ( alph[alph.find(s[i]) - 1] == s[i - 1] or alph[alph.find(s[i]) - 1] == s[i + 1] ): mx = max(mx, s[i]) index = i if s[0] > mx and alph[alph.find(s[0]) - 1] == s[1]: index = 0 mx = s[0] if s[-1] > mx and alph[alph.find(s[-1]) - 1] == s[-2]: index = len(s) - 1 mx = alph[alph.find(s[-1]) - 1] if index == -1: break else: s = s[:index] + s[index + 1 :] print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
ans = 100000000.0 def solve(x, n): global ans ans = min(ans, n) res = "a" for i in range(n - 1): if abs(ord(x[i]) - ord(x[i + 1])) == 1: if x[i] > x[i + 1]: res = max(res, x[i]) else: res = max(res, x[i + 1]) if res == "a": return for i in range(n - 1): if abs(ord(x[i]) - ord(x[i + 1])) == 1: if x[i] > x[i + 1] and x[i] == res: solve(x[:i] + x[i + 1 :], n - 1) break elif x[i] < x[i + 1] and x[i + 1] == res: solve(x[: i + 1] + x[i + 2 :], n - 1) break n = int(input()) s = input() solve(s, n) print(n - ans)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) f = input() d = set() for i in f: d.add(i) d = sorted(list(d)) x = len(d) count = 0 x1 = len(f) i = x - 1 while i >= 0: flag = 0 if count + 1 == x1: break ut = 0 rty = x1 - 1 - count for j in range(1, rty): if f[j - ut] == d[i] and ( ord(f[j - ut]) - ord(f[j - 1 - ut]) == 1 or ord(f[j - ut]) - ord(f[j + 1 - ut]) == 1 ): count += 1 f = f[: j - ut] + f[j + 1 - ut :] ut += 1 flag = 1 if f[0] == d[i] and ord(f[0]) - ord(f[1]) == 1: count += 1 f = f[1:] flag = 1 if f[-1] == d[i] and ord(f[-1]) - ord(f[-2]) == 1: count += 1 flag = 1 f = f[:-1] i -= 1 i += flag print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) x = list(input()) y = len(x) abc = list("abcdefghijklmnopqrstuvwxyz") pre = list("0abcdefghijklmnopqrstuvwxy") for i in range(1, len(abc)): s = 0 while s < len(x): if x[s] == abc[-i]: if s - 1 >= 0 and x[s - 1] == pre[-i]: del x[s] s -= 1 elif s + 1 < len(x) and x[s + 1] == pre[-i]: del x[s] if s > 0: s -= 2 else: s -= 1 s += 1 answer = y - len(x) print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) S = list(input()) res = 0 for i in range(26): look_for = ord("z") - i cont = True while cont: cont = False j = 0 while j < n: if look_for == ord(S[j]): l = j - 1 r = j + 1 if ( l in range(n) and look_for - 1 == ord(S[l]) or r in range(n) and look_for - 1 == ord(S[r]) ): del S[j] j -= 1 n -= 1 res += 1 cont = True j += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def solve(n, string): string = [c for c in string] ans = 0 def valid(i, char): if char == "a": return False nbr = chr(ord(char) - 1) return ( i - 1 >= 0 and string[i - 1] == nbr or i + 1 < len(string) and string[i + 1] == nbr ) _maxxIdx = None while string: _maxxIdx = None for i, c in enumerate(string): if valid(i, c): if _maxxIdx is not None: if c > string[_maxxIdx]: _maxxIdx = i else: _maxxIdx = i if _maxxIdx is None: break string.pop(_maxxIdx) return n - len(string) print(solve(int(input()), input()))
FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NONE WHILE VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR NONE IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() now = ord("z") st = ord("a") tong = [] for i in range(144): tong.append(0) for c in s: tong[ord(c)] += 1 ans = 0 maxs = max(s) now = 0 while ord(maxs) > ord("a"): pos = s.find(maxs, now) if pos != -1: if ( pos - 1 >= 0 and ord(s[pos - 1]) == ord(maxs) - 1 or pos + 1 < len(s) and ord(s[pos + 1]) == ord(maxs) - 1 ): s = s[0:pos] + s[pos + 1 : len(s)] now = 0 ans += 1 else: now = pos + 1 else: maxs = chr(ord(maxs) - 1) now = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) word = input() s = "abcdefghijklmnopqrstuvwxyz" s = s[-1::-1] for i in range(25): cur = s[i] prev = s[i + 1] while cur in word and prev in word: ans = "" for j in range(len(word)): if word[j] == cur: f = 0 if j - 1 >= 0: if word[j - 1] == prev: f = 1 if j + 1 < len(word): if word[j + 1] == prev: f = 1 if f == 1: pass else: ans += word[j] else: ans += word[j] if word == ans: break word = ans[:] print(n - len(word))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) b = [i for i in map(ord, input())] for i in range(max(b), min(b), -1): while True and len(b) > 1: idx = [] if b[0] == b[1] + 1 and b[0] == i: idx.append(0) for j in range(1, len(b) - 1): if b[j] == i and (b[j] == b[j - 1] + 1 or b[j] == b[j + 1] + 1): idx.append(j) if b[-1] == b[-2] + 1 and b[-1] == i: idx.append(len(b) - 1) if len(idx) == 0: break for j in range(len(idx)): del b[idx[j] - j] print(n - len(b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER WHILE NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
w = "abcdefghijklmnopqrstuvwxyz" q = int(input()) p = q a = list(input()) e = [1] r = w.index(max(a)) while r: e = [] for i in range(q): if a[i] == w[r]: if i > 0 and a[i - 1] == w[r - 1]: e.append(i) elif i < q - 1 and a[i + 1] == w[r - 1]: e.append(i) t = 0 for i in e: del a[i - t] t += 1 q = len(a) if not e: r -= 1 print(p - len(a))
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) S = ["#"] + list(input()) + ["#"] ANS = 0 for i in range(25, -1, -1): j = 0 while j < len(S): if ( j < len(S) and ord(S[j]) - 97 == i and (ord(S[j - 1]) - 97 == i - 1 or ord(S[j + 1]) - 97 == i - 1) ): ANS += 1 S.pop(j) j -= 1 else: j += 1 print(ANS)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR LIST STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = sys.stdin.readline MOD = 1000000007 MOD2 = 998244353 ii = lambda: int(input().strip("\n")) si = lambda: input().strip("\n") dgl = lambda: list(map(int, input().strip("\n"))) f = lambda: map(int, input().strip("\n").split()) il = lambda: list(map(int, input().strip("\n").split())) ls = lambda: list(input().strip("\n")) lsi = lambda: [int(i) for i in ls()] let = "abcdefghijklmnopqrstuvwxyz" for _ in range(1): n = ii() s = ls() for j in reversed(let): fg = 1 while fg: fg1 = 0 if len(s) > 1: for i in range(len(s)): if s[i] == j: if i == 0 and ord(j) - ord(s[i + 1]) == 1: s.pop(0) fg1 = 1 break elif i == len(s) - 1 and ord(j) - ord(s[i - 1]) == 1: s.pop(len(s) - 1) fg1 = 1 break elif 0 < i < len(s) - 1 and ( ord(j) - ord(s[i + 1]) == 1 or ord(j) - ord(s[i - 1]) == 1 ): s.pop(i) fg1 = 1 break fg = fg1 print(n - len(s))
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n, s = int(input()), input() def finder(): best = -1 for i in range(len(s)): if ( i - 1 >= 0 and ord(s[i - 1]) + 1 == ord(s[i]) or i + 1 < len(s) and ord(s[i + 1]) + 1 == ord(s[i]) ): if best == -1 or ord(s[best]) < ord(s[i]): best = i return best ans = 0 best = finder() while best != -1: ans += 1 s = s[:best] + s[best + 1 :] best = finder() print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
l = "abcdefghijklmnopqrstuvwxyz" n = int(input()) s = list(input()) for i in range(25, 0, -1): remove_this = l[i] j = 0 while j < len(s): f = 0 if s[j] == remove_this: if j - 1 > -1: if s[j - 1] == l[i - 1]: s.pop(j) j = 0 f = 1 if j + 1 < len(s) and f == 0: if s[j + 1] == l[i - 1]: s.pop(j) j = 0 f = 1 if f == 0: j += 1 print(n - len(s))
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
N = int(input()) S = input() ans = 0 for n in range(N - 1, 0, -1): tmp = 0 t = -1 for i in range(n): if ord(S[i]) == ord(S[i + 1]) + 1 and tmp < ord(S[i]): tmp = ord(S[i]) t = i if ord(S[i + 1]) == ord(S[i]) + 1 and tmp < ord(S[i + 1]): tmp = ord(S[i + 1]) t = i + 1 if t == -1: break else: ans += 1 S = S[:t] + S[t + 1 :] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys input = sys.stdin.buffer.readline def I(): return list(map(int, input().split())) def sieve(n): a = [1] * n for i in range(2, n): if a[i]: for j in range(i * i, n, i): a[j] = 0 return a n = int(input()) l = list(input())[:n] if n >= 1: while len(l) > 1: a = 0 idx = -1 f = False if l[0] - 1 == l[1]: f = 1 idx = 0 a = l[0] idx = 0 for i in range(1, len(l) - 1): if l[i] - l[i - 1] == 1 or l[i] - l[i + 1] == 1: f = True if l[i] > a: a = max(a, l[i]) idx = i if l[-1] - l[-2] == 1: f = True if l[-1] > a: a = max(a, l[-1]) idx = -1 if not f: break l.pop(idx) print(n - len(l))
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
from sys import exit def i(): return input() def ii(): return int(input()) def iis(): return map(int, input().split()) def liis(): return list(map(int, input().split())) def print_array(a): print(" ".join(map(str, a))) n = ii() s = input() alph = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] alph = alph[::-1] count = 0 for c in alph: for j in range(n): for i in range(len(s)): if s[i] == c: if ( i > 0 and ord(s[i - 1]) == ord(s[i]) - 1 or i < len(s) - 1 and ord(s[i + 1]) == ord(s[i]) - 1 ): s = s[:i] + s[i + 1 :] count += 1 break print(count)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def go(): n = int(input()) s = list(map(ord, input())) letters = sorted(set(s), reverse=True) for l in letters: l1 = l - 1 if l - 1 not in letters: continue i = 0 while i < len(s): if s[i] != l1: i += 1 elif i > 0 and s[i - 1] == l: del s[i - 1] i -= 1 elif i < len(s) - 1 and s[i + 1] == l: del s[i + 1] else: i += 1 return n - len(s) print(go())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) ar = [(ord(x) - 97) for x in input()] ans = 0 flag = True while flag: temp = -1 ind = -1 for k in range(1, len(ar) - 1): if (ar[k] == ar[k - 1] + 1 or ar[k] == ar[k + 1] + 1) and ar[k] > temp: ind = k temp = ar[k] if len(ar) > 1 and ar[0] == ar[1] + 1 and ar[0] > temp: ind = 0 temp = ar[0] if len(ar) > 1 and ar[-1] == ar[-2] + 1 and ar[-1] > temp: ind = len(ar) - 1 temp = ar[-1] if temp != -1: ar = ar[:ind] + ar[ind + 1 :] ans += 1 else: flag = False print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() for i in range(122, 96, -1): t = 0 while t < len(s): if ord(s[t]) == i: if t + 1 < len(s) and ord(s[t + 1]) == i - 1: s = s[:t] + s[t + 1 :] if t - 2 >= 0: t -= 2 elif t - 1 >= 0: t = -1 t += 1 t = 0 while t < len(s): if ord(s[t]) == i: if t - 1 >= 0 and ord(s[t - 1]) == i - 1: s = s[:t] + s[t + 1 :] t -= 1 t += 1 print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
import sys n = int(input()) s = input() temp = [] for i in range(n): temp.append(s[i]) s = temp v = set() for i in s: v.add(i) ans = 0 for i in range(25, -1, -1): curr = chr(i + ord("a")) temp = set() if curr in v: l = len(s) for k in range(l): if s[k] == curr: if k - 1 >= 0: if ord(s[k - 1]) + 1 == ord(curr): ans += 1 temp.add(k) s[k] = s[k - 1] for k in range(l - 1, -1, -1): if s[k] == curr: if k + 1 < l: if ord(s[k + 1]) + 1 == ord(curr): ans += 1 temp.add(k) s[k] = s[k + 1] q = [] for k in range(l): if k not in temp: q.append(s[k]) s = q print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() rm = 0 while True: new_s = [] n = len(s) rm_ls = [] for i in range(n): nb = max(0, i - 1), min(n - 1, i + 1) if s[nb[0]] == chr(ord(s[i]) - 1) or s[nb[1]] == chr(ord(s[i]) - 1): rm_ls.append(i) if rm_ls: mx = max(zip(map(lambda x: s[x], rm_ls), rm_ls))[1] else: break new_s = s[:mx] + s[mx + 1 :] rm += 1 s = new_s if new_s == "": break print(rm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
MOD = 1000000007 MOD2 = 998244353 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) let = "@abcdefghijklmnopqrstuvwxyz" n = ii() s = ls() s = ["$"] + s + ["$"] for j in range(26, 0, -1): while 1: plen = len(s) for k in range(1, len(s)): if s[k] == let[j] and (s[k - 1] == let[j - 1] or s[k + 1] == let[j - 1]): s.pop(k) break currlen = len(s) if currlen == plen: break print(n - len(s) + 2)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) s = input() for maxElement in sorted(list(s), reverse=True): prev = chr(ord(maxElement) - 1) edited = False for i in range(len(s)): if s[i] == maxElement: if i == 0 and i + 1 < len(s): if s[i + 1] == prev: s = s[i + 1 :] edited = True break elif i == len(s) - 1 and i - 1 >= 0: if s[i - 1] == prev: s = s[:i] edited = True break elif i - 1 >= 0 and i + 1 < len(s): if s[i - 1] == prev or s[i + 1] == prev: s = s[:i] + s[i + 1 :] edited = True break print(n - len(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
n = int(input()) a = list(input()) for i in range(25, 0, -1): cur = chr(97 + i) prev = chr(96 + i) j = 0 while True: if j >= len(a): break x = j - 1 if j != 0 else j y = j + 1 if j != len(a) - 1 else j if a[j] == cur and (a[x] == prev or a[y] == prev): a.pop(j) j -= 1 j += 1 j = len(a) - 1 while j >= 0: x = j - 1 if j != 0 else j y = j + 1 if j != len(a) - 1 else j if a[j] == cur and (a[x] == prev or a[y] == prev): a.pop(j) j += 1 j -= 1 print(n - len(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
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. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. 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. -----Input----- 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. -----Output----- Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally. -----Examples----- Input 8 bacabcab Output 4 Input 4 bcda Output 3 Input 6 abbbbb Output 5 -----Note----- The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$. In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
def reducable(s): rc = None mx = "a" for i, c in enumerate(s): if ( i - 1 >= 0 and ord(c) - ord(s[i - 1]) == 1 or i + 1 < len(s) and ord(c) - ord(s[i + 1]) == 1 ): if c > mx: mx = c rc = i return rc n = int(input()) s = list(input()) idx = reducable(s) ans = 0 while idx != None: del s[idx] ans += 1 idx = reducable(s) print(ans)
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR