이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const ll P = 2137;
const int N = 1e6+10;
ll p[N], h[N], inv[N];
ll bpw(ll x, ll n) {
if (n == 0) return 1;
ll c=bpw(x, n/2);
(c=c*c)%=MOD;
if (n&1) return (c*x)%MOD;
return c;
}
ll hsh(int l, int r) {
ll v=((h[r]-h[l-1])%MOD+MOD)%MOD;
return (v*inv[l])%MOD;
}
void solve() {
string s; cin>>s; h[0]=0;
int n=s.size();
for (int i=1; i<=n; ++i) h[i]=(h[i-1]+s[i-1]*p[i])%MOD;
int ans=0, l=0;
for (int i=1; i<=n/2; ++i) {
if (hsh(l+1, i) == hsh(n-i+1, n-l)) l=i, ans+=2;
}
if (l < (n+1)/2) ++ans;
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(NULL);
cout.tie(NULL);
p[0]=1, p[1]=P;
inv[0]=1, inv[1]=bpw(P, MOD-2);
for (int i=2; i<N; ++i) (p[i]=p[i-1]*P)%=MOD, (inv[i]=inv[i-1]*inv[1])%=MOD;
int t; cin>>t;
while (t--) solve();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |