-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring_palindrome.cpp
52 lines (45 loc) · 1.06 KB
/
string_palindrome.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
51
52
#include <bits/stdc++.h>
using namespace std;
int evenpalin(string str)
{
int n = str.length();
int result = 0;
int sameChar[n] = { 0 };
int i = 0;
while (i < n) {
int sameCharCount = 1;
int j = i + 1;
while (str[i] == str[j] && j < n)
sameCharCount++, j++;
result += (sameCharCount * (sameCharCount + 1) / 2);
sameChar[i] = sameCharCount;
i = j;
}
return result - n;
}
int oddlen(string str)
{
int n = str.length();
int result = 0;
int sameChar[n] = { 0 };
//int i = 0;
for (int j = 1; j < n; j++)
{
if (str[j] == str[j - 1])
sameChar[j] = sameChar[j - 1];
if (j > 0 && j < (n - 1) &&
(str[j - 1] == str[j + 1] &&
str[j] != str[j - 1]))
result += min(sameChar[j - 1],
sameChar[j + 1]);
}
return n-result;
}
int main()
{
string s;
cin>>s;
cout<<evenpalin(s)<<" "<<oddlen(s);
//evenpalin(s);
return 0;
}