solution
字符串哈希判断回文(令长度至少为D)
code
/*SiberianSquirrel*//*CuteKiloFish*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ill = __int128;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll MOD = ll(998244353);
const ll INF = ll(1e18 + 10);
const int N = int(1e7 + 10);
inline int rnd(){static int seed=2333;return seed=(((seed*666666ll+20050818)%998244353)^1000000007)%1004535809;}
template <class T> void read(T &x) {
static char ch;static bool neg;
for(ch=neg=0;ch<'0' || '9'<ch;neg|=ch=='-',ch=getchar());
for(x=0;'0'<=ch && ch<='9';(x*=10)+=ch-'0',ch=getchar());
x=neg?-x:x;
}
//VAR
ull Hash[2][N], bin = 1, base = 13331;
//FUNCTION
ull quick_pow(ull ans, ll p, ull res = 1) {
for(; p; p >>= 1, ans = ans * ans)
if(p & 1) res = res * ans;
return res;
}
inline void init(string s, int n, int d) {
for(int i = 1; i <= n; ++ i)
Hash[0][i] = Hash[0][i - 1] * base + s[i] - 'a' + 1;
for(int i = n; i >= 1; -- i)
Hash[1][i] = Hash[1][i + 1] * base + s[i] - 'a' + 1;
bin = quick_pow(base, d);
}
ull check(int l, int r) {
return Hash[0][r] - Hash[0][l - 1] * bin == Hash[1][l] - Hash[1][r + 1] * bin;
}
//WORK
void solve() {
int n, d; cin >> n >> d;
string s; cin >> s; s = " " + s;
init(s, n, d);
int ans = 0;
for(int i = 1, j; i <= n; i = j) {
j = i + d - 1;
while (j <= n && check(j - d + 1, j)) j ++;
ans ++;
}
cout << ans << endl;
}
//MAIN
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
// freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}