QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#485492 | #8571. Palworld | Rosmontispes | TL | 2042ms | 54024kb | C++20 | 4.1kb | 2024-07-20 18:36:42 | 2024-07-20 18:36:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
#define fi first
#define se second
using ll = long long;
using pll = pair<ll,ll>;
long long base[2] = { 131, 13331 };
long long mod[2] = { 1000000007, 998244353 };
long long pw[400000 + 200][2];
void init()
{
pw[0][0] = pw[0][1] = 1;
for (int i = 0; i <= 400000; i++)
for (int j = 0; j < 2; j++) {
pw[i + 1][j] = pw[i][j] * base[j] % mod[j];
}
}
void solve()
{
int n,k;
cin>>n>>k;
string s,t;
cin>>s;
t = s;
reverse(t.begin(),t.end());
s = s + t;
vector<vector<long long>> x(2 * n + 1,vector<long long>(2)), rx(2 * n + 1,vector<long long>(2));
for (int i = 0; i < 2 * n; i++)
for (int j = 0; j < 2; j++)
x[i + 1][j] = (x[i][j] * base[j] + s[i]) % mod[j];
auto gain = [&](int l, int r)->ll{
pll ret;
ret.fi = (x[r][0] - x[l - 1][0] * pw[r - l + 1][0] % mod[0] + mod[0]) % mod[0];
ret.se = (x[r][1] - x[l - 1][1] * pw[r - l + 1][1] % mod[1] + mod[1]) % mod[1];
return (ret.fi * 1000000000) ^ ret.se;
};
auto LCP = [&](int l1, int r1, int l2, int r2)->int{
if(r1 < 0 || l2 > n)
return 0;
if(r1 - l1 + 1 <= 0 || r2 - l2 + 1 <= 0)
return 0;
l1 = 2 * n - l1 + 1,r1 = 2 * n - r1 + 1;
swap(l1,r1);
if(s[l1 - 1] != s[l2 - 1])
return 0;
int l = 1, r = min(r1 - l1 + 1, r2 - l2 + 1);
while(l < r){
int mid = (l + r + 1) >> 1;
if(gain(l1, l1 + mid - 1) == gain(l2, l2 + mid - 1)) l = mid;
else r = mid - 1;
}
return l;
};
int ans = 0;
for(int i = 0;i < n;i ++)
{
int Lmx = i,Rmx = n - 1 - i;
int lidx = i - 1,ridx = i + 1;
int lcp1 = LCP(1,lidx + 1,ridx + 1,n);
int Lidx = lidx - lcp1,Ridx = ridx + lcp1;
Lmx -= lcp1,Rmx -= lcp1;
for(int j = 1;j <= min(Rmx,k);j ++)
{
int rRidx = Ridx + j;
int lcp2 = LCP(1,Lidx + 1,rRidx + 1,n);
int lasL = Lidx - lcp2 + 1,lasR = rRidx + lcp2 - 1;
ans = max(ans,lasR - lasL + 1 + j);
}
for(int j = 1;j <= min(Lmx,k);j ++)
{
int rLidx = Lidx - j;
int lcp2 = LCP(1,rLidx + 1,Ridx + 1,n);
int lasL = rLidx - lcp2 + 1,lasR = Ridx + lcp2 - 1;
ans = max(ans,lasR - lasL + 1 + j);
}
}
for(int i = 0;i < n;i ++)
{
int Lmx = i,Rmx = n - i;
int lidx = i - 1,ridx = i;
int lcp1 = LCP(1,lidx + 1,ridx + 1,n);
int Lidx = lidx - lcp1,Ridx = ridx + lcp1;
Lmx -= lcp1,Rmx -= lcp1;
for(int j = 1;j <= min(Rmx,k);j ++)
{
int rRidx = Ridx + j;
int lcp2 = LCP(1,Lidx + 1,rRidx + 1,n);
int lasL = Lidx - lcp2 + 1,lasR = rRidx + lcp2 - 1;
ans = max(ans,lasR - lasL + 1 + j);
}
for(int j = 1;j <= min(Lmx,k);j ++)
{
int rLidx = Lidx - j;
int lcp2 = LCP(1,rLidx + 1,Ridx + 1,n);
int lasL = rLidx - lcp2 + 1,lasR = Ridx + lcp2 - 1;
ans = max(ans,lasR - lasL + 1 + j);
}
}
for(int i = 0;i < n;i ++)
{
int Lmx = i,Rmx = n - i;
int lidx = i - 1,ridx = i;
int Lidx = lidx,Ridx = ridx;
for(int j = 1;j <= min(Rmx,k);j ++)
{
int rRidx = Ridx + j;
int lcp2 = LCP(1,Lidx + 1,rRidx + 1,n);
int lasL = Lidx - lcp2 + 1,lasR = rRidx + lcp2 - 1;
ans = max(ans,lasR - lasL + 1 + k);
}
for(int j = 1;j <= min(Lmx,k);j ++)
{
int rLidx = Lidx - j;
int lcp2 = LCP(1,rLidx + 1,Ridx + 1,n);
int lasL = rLidx - lcp2 + 1,lasR = Ridx + lcp2 - 1;
ans = max(ans,lasR - lasL + 1 + k);
}
}
cout<<ans<<"\n";
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
cin>>T;
init();
while(T --)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 10092kb
input:
4 1 3 a 4 1 icpc 4 2 icpc 8 4 icecream
output:
4 5 5 11
result:
ok 4 number(s): "4 5 5 11"
Test #2:
score: 0
Accepted
time: 1361ms
memory: 54024kb
input:
1 200000 66 jsmwjmibgkjvdscqllsxpaxiycmpauhnzschbivtqbjfrxrqvhvfbqecozjewqqpwdfbeqppjkgxhbnniopkptkygspcdswhwadfwhnzovvpshgcdukrupeztkpxwhmctaquqbxtidzbbxsyuaeikuldaoeudletrsmqptaejibkymsjhmwykqsjdvvdaqwelrcpxwrwhuvodipjniowfofbjktkdezwqqbvwsppsmpilntmdmlxgkaxymnugmmcsljkjzjuudnllydwdwwanadynsoiolso...
output:
141
result:
ok 1 number(s): "141"
Test #3:
score: 0
Accepted
time: 2042ms
memory: 53808kb
input:
1 200000 100 qhiaajzxinenucrnfffumuhnovpuwcnojbhsktztapgyivmfqrlntwazwnfetwqieckxcnkskpidltiydfoveaucckximydxxfbiwbdufmbhywqkflyqxbijakadqkzftlciccbpnldsqhtjxuxnvkusvizavuyfhdroiuominebadhfqzrpjnzpgyvkejfwmueiltyeqpvwrkanqknyacqganbszktocfuvvsfrboaennwpaabfdnaurvvurysrijnfaonesihbhrrvbvyhpbremsuhhbc...
output:
209
result:
ok 1 number(s): "209"
Test #4:
score: 0
Accepted
time: 73ms
memory: 10084kb
input:
10000 21 7 fhcjhdjcfbfbdeeheibhg 18 8 hccgjfaadefhjhcijc 15 7 ahefiiheaigjiid 15 3 fgjjebidbfgbdij 15 5 jccebbgjbhifjhb 20 2 hcbddhibgfccjjcfebcc 19 1 ehbdgiicchijebidbcd 20 8 gbcfghefhbjggecdhcbj 17 2 jjaeaccjbcfiehfdd 23 4 iafjedfbebbhcfgfjbehdaf 22 1 jgiiiaacehcbaiehfggcfi 17 2 jefbbfigfhbhfcici ...
output:
17 21 17 11 13 9 6 18 9 11 6 9 22 8 21 23 5 23 7 16 9 23 25 11 17 12 9 5 20 5 23 12 7 13 16 17 21 21 21 12 16 21 21 8 9 11 21 15 5 24 13 7 21 13 11 8 7 19 25 7 8 12 13 22 15 14 7 15 7 20 15 13 11 15 5 11 23 17 17 19 15 7 19 17 25 13 23 10 17 21 9 21 7 23 21 13 16 7 14 10 13 10 9 11 7 21 15 19 11 23 ...
result:
ok 10000 numbers
Test #5:
score: -100
Time Limit Exceeded
input:
1 200000 100 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...