-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path647.cpp
111 lines (95 loc) · 2.87 KB
/
647.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
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
#include <bits/stdc++.h>
using namespace std;
#define PRINT_VECTOR(vec, type) \
do { \
copy(vec.begin(), vec.end(), ostream_iterator<type>(cout, " ")); \
cout << endl; \
} while(0)
#define CHECK(j, k, m) ((j) >= 0) && ((k) < (m))
class Solution {
public:
/**
*
*/
int countSubstrings(string s)
{
ostringstream strbuf;
strbuf << "#";
for (const char &c: s) {
strbuf << c << "#";
}
string str = strbuf.str();
// #a#b#c#
int m = str.size();
vector<int> radius(m, 0);
int r_radius = 0, mid_index = 0, count = 0;
for (int i = 0; i < m; ++i) {
if (i <= r_radius + mid_index) {
// 对称位置是否超出右边的限制
// printf("%d, %d\n", radius[mid_index*2-i], mid_index+r_radius-i);
radius[i] = min(radius[mid_index*2-i], mid_index+r_radius-i);
}
#if 0
printf("radius[%d]=%d ", i, radius[i]);
printf(" str[i]=%c ", str[i]);
printf("str[i-radius[i]-1]=%c, str[i+radius[i]+1]=%c ", str[i-radius[i]-1], str[i+radius[i]+1] );
printf("%d ===> %d,%d\n", CHECK(i-radius[i]-1, i+radius[i]+1, m), i, str[i-radius[i]-1] == str[i + radius[i]+1]);
#endif
while (CHECK(i-radius[i]-1, i+radius[i]+1, m) && str[i-radius[i]-1] == str[i+radius[i]+1]) {
++radius[i];
}
if (mid_index + r_radius < i + radius[i]) {
r_radius = radius[i];
mid_index = i;
}
count += (radius[i]+1)/2;
}
// PRINT_VECTOR(radius, int);
return count;
}
};
#if 0
#define CHECK(j, k, m) ((j) >= 0) && ((k) < (m))
/** 基础算法*/
int countSubstrings(string s) {
ostringstream str;
int n = s.size(), count = 0;
for (int i = 0; i < n; ++i) {
str << "#" << s[i];
}
str << '#';
string ss = str.str();
int j, k, m = ss.size(), flag_num;
for (int i = 1; i < m; ++i) {
j = i - 1, k = i + 1;
flag_num = ss[i] == '#'? 1:0;
while(CHECK(j, k, m)) {
if (ss[j]=='#') flag_num++;
if (ss[j] == ss[k]){
if (ss[j]=='#') {
if (flag_num < 2) count++;
} else {
count++;
}
}
else break;
j--;
k++;
}
}
return count;
}
#endif
int main(int argc, char const *argv[])
{
Solution s;
freopen("test.in", "r", stdin);
string str;
while (cin >> str) {
/* code */
cout << str << endl;
int result = s.countSubstrings(str);
cout << result << endl;
}
return 0;
}