思路:二分答案+后缀数组check
如何check?
1.预处理所给字符串的价值前缀和,记为sum[]
2.枚举左端点,在sum[]中找到最靠右的pos,使sum[i…pos]的价值小于等于要check的值now。记所有价值小于now的子串为ans, 那么sum[i…pos]对ans的贡献就是pos-i+1-height[rk[i]]
tips: height[rk[i]]表示以i为开头的后缀与字典序比它小一的后缀的lcp,lcp部分在之前已经被统计过了,所以应该减去
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn = 1e5+5;
using namespace std;
ll n,k,m;
int kase;
char s[maxn];
int w[maxn],sum[maxn];
int rk1[maxn],sa[maxn],tp1[maxn],tax[maxn],height[maxn];
int *rk=rk1, *tp=tp1;
void bucket()
{
for(int i=0; i<=m; ++i) tax[i]=0;
for(int i=1; i<=n; ++i) tax[rk[i]]++;
for(int i=1; i<=m; ++i) tax[i]+=tax[i-1];
for(int i=n; i>=1; i--) sa[tax[rk[tp[i]]]--]=tp[i];
}
void get_sa()
{
m=128;
for(int i=1; i<=n; i++) rk[i]=s[i],tp[i]=i;
bucket();
for(int w=1,p=0; p<n; m=p,w<<=1)
{
p=0;
for(int i=1; i<=w; i++) tp[++p]=n-w+i;
for(int i=1; i<=n; i++)
if(sa[i]>w) tp[++p]=sa[i]-w;
bucket();
swap(tp,rk);
rk[sa[1]]=p=1;
for(int i=2;i<=n;++i)
rk[sa[i]]=(tp[sa[i-1]]==tp[sa[i]] && tp[sa[i-1]+w]==tp[sa[i]+w])?p:++p;
}
}
void get_height()
{
int k=0;
for(int i=1; i<=n; i++)
{
if(k) k--;
int j=sa[rk[i]-1];
while(s[i+k]==s[j+k]) ++k;
height[rk[i]]=k;
}
}
bool check(int now)
{
ll res=0;
for (int i=1; i<=n; i++)
{
int pos=upper_bound(sum+1,sum+n+1,now+sum[i-1])-sum;
int temp=pos-i;
res+=temp-min(temp,height[rk[i]]);
}
if (res<k) return false;
return true;
}
int main()
{
FAST;
cin>>kase;
while(kase--)
{
cin>>n>>k;
cin>>s+1;
for (int i=1; i<=26; i++) cin>>w[i];
for (int i=1; i<=n; i++) sum[i]=sum[i-1]+w[s[i]-'a'+1];
get_sa();
get_height();
int l=0, r=1e7+10;
int ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if (check(mid))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
cout<<ans<<endl;
}
return 0;
}