Skip to content

Commit e39ac73

Browse files
authored
Update EDIT - Edit Distance Again.cpp
1 parent 9f539ef commit e39ac73

File tree

1 file changed

+41
-59
lines changed

1 file changed

+41
-59
lines changed
Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,48 @@
1-
#include <iostream>
2-
#include <string>
3-
#include <algorithm>
1+
#include <bits/stdc++.h>
42

53
using namespace std;
64

7-
void calc(int &cc, string s) {
8-
char a, b, c;
9-
10-
for(int i = 1; i < s.size() - 1; i++) {
11-
a = s[i];
12-
b = s[i+1];
13-
c = s[i-1];
14-
15-
if(!((a >= 'a' && a <= 'z') && (b == ' ' || (b >= 'A' && b <= 'Z')) && (c == ' ' || (c >= 'A' && c <= 'Z'))) || !((a >= 'A' && a <= 'Z') && (b == ' ' || (b >= 'a' && b <= 'z')) && (c == ' ' || (c >= 'a' && c <= 'z')))) {
16-
if((c >= 'a' && c <= 'z' && a >= 'a' && a <= 'z') || (c >= 'A' && c <= 'Z' && a >= 'A' && a <= 'Z')) {
17-
if(islower(s[i])) s[i] = toupper(s[i]);
18-
else s[i] = tolower(s[i]);
19-
a = s[i];
20-
cc++;
21-
}
22-
23-
if((b >= 'a' && b <= 'z' && a >= 'a' && a <= 'z') || (b >= 'A' && b <= 'Z' && a >= 'A' && a <= 'Z')) {
24-
if(islower(s[i+1])) s[i+1] = toupper(s[i+1]);
25-
else s[i+1] = tolower(s[i+1]);
26-
b = s[i];
27-
cc++;
28-
}
29-
}
30-
}
5+
int const N = 1e3 + 1;
6+
int dp[N][2];
7+
string s;
8+
9+
int rec(int i, bool c) {
10+
if(i == s.length()) {
11+
return 0;
12+
}
13+
14+
int &res = dp[i][c];
15+
if(res != -1) {
16+
return res;
17+
}
18+
res = 1e9;
19+
20+
if(c) {
21+
if(islower(s[i])) {
22+
res = min(res, rec(i + 1, false));
23+
} else {
24+
res = min(res, rec(i + 1, true) + 1);
25+
}
26+
} else {
27+
if(islower(s[i])) {
28+
res = min(res, rec(i + 1, false) + 1);
29+
} else {
30+
res = min(res, rec(i + 1, true));
31+
}
32+
}
33+
34+
return res;
3135
}
3236

3337
int main() {
34-
string s;
35-
int cc1, cc2;
36-
37-
while(cin >> s) {
38-
if(s.size() == 1)
39-
cout << 0 << endl;
40-
else if(s.size() == 2) {
41-
char a, b;
42-
43-
a = s[0];
44-
b = s[1];
45-
46-
if((a >= 'a' && a <= 'z' && b >= 'a' && b <= 'z') || (a >= 'A' && a <= 'Z' && b >= 'A' && b <= 'Z'))
47-
cout << 1 << endl;
48-
else
49-
cout << 0 << endl;
50-
} else {
51-
cc1 = 0;
52-
cc2 = 1;
53-
54-
calc(cc1, s);
55-
56-
if(islower(s[0])) s[0] = toupper(s[0]);
57-
else s[0] = tolower(s[0]);
58-
59-
calc(cc2, s);
60-
61-
cout << min(cc1, cc2) << endl;
62-
}
63-
}
64-
65-
return 0;
38+
#ifndef ONLINE_JUDGE
39+
freopen("input.in", "r", stdin);
40+
#endif
41+
42+
while(cin >> s) {
43+
memset(dp, -1, sizeof dp);
44+
cout << rec(0, islower(s[0])) << endl;
45+
}
46+
47+
return 0;
6648
}

0 commit comments

Comments
 (0)