-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabet.cpp
50 lines (40 loc) · 1.4 KB
/
alphabet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Get the string
string inp;
cin >> inp;
// Convert the string to a vector of characters
vector<char> str;
for(int i = 0; i < inp.size(); i++)
{
str.push_back(inp[i]);
}
// Stores the length of the longest subsequence using the characters up to
// and including the index
vector<int> table(str.size());
table[0] = 1; // Using the first character can get length 1
for(int i = 1; i < str.size(); i++)
{
table[i] = 1; // At least 1 by using just character at index i
//For each character in the string to i
for(int j = 0; j < i; j++)
{
// If the character at index i is greater than this character,
// and using the characters to this character can generate a sequence
// that with the character at index i is longer than the current
// maximum length for characters to i
if(str[i] > str[j] && table[i] < table[j] + 1)
{
// Update the longest subsequence
table[i] = table[j] + 1;
}
}
}
// Get the maximum length from the table
int maxLen = *max_element(table.begin(), table.end());
// Subtract the length from 26 to find the number of extra letters to add
cout << (26 - maxLen) << endl;
return 0;
}