-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBack Front
127 lines (125 loc) Β· 2.71 KB
/
Back Front
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
string s;
cin >> s;
int ans = n;
int k = 0, ck = 0, ack = 0, back = 0, f = 0, fr = 0, fro = 0, fron = 0, front = 0;
for (int i = n-1; i >=0; i--)
{
if(s[i]=='k'){
k++;
}
else if((s[i]=='c') && k){
ck++;
k--;
}
else if((s[i]=='a') && ck){
ack++;
ck--;
}
else if((s[i]=='b') && ack){
back++;
ack--;
}
}
for (int i = 0; i < n; i++)
{
if(s[i]=='f'){
f++;
}
else if((s[i]=='r') && f){
fr++;
f--;
}
else if((s[i]=='o') && fr){
fro++;
fr--;
}
else if((s[i]=='n') && fro){
fron++;
fro--;
}
else if((s[i]=='t') && fron){
front++;
fron--;
}
}
int av = 0;
while(back!=0){
back--;
ans -= 4;
av++;
if(av && ack){
ack--;
av--;
back++;
ans++;
}
else if(av>=2 && ck){
ck--;
av-=2;
back++;
ans+=2;
}
else if(av>=3 && k){
k--;
av-=3;
back++;
ans+=3;
}
else if(av>=4){
av -= 4;
back++;
ans += 4;
}
}
ans += av;
av = 0;
while(front!=0){
front--;
ans -= 5;
av++;
if(av && fron){
fron--;
av--;
front++;
ans++;
}
else if(av>=2 && fro){
fro--;
av-=2;
front++;
ans+=2;
}
else if(av>=3 && fr){
fr--;
av-=3;
front++;
ans+=3;
}
else if(av>=4 && f){
f--;
av-=4;
front++;
ans+=4;
}
else if (av >= 5)
{
av -= 5;
front++;
ans += 5;
}
}
ans += av;
cout << ans << endl;
}
}