QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#229989 | #7618. Pattern Search | WJH# | WA | 0ms | 3824kb | C++14 | 1.5kb | 2023-10-28 17:22:10 | 2023-10-28 17:22:18 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int c[30],cc[30],d[30],r[30];
pair<int,int> a[30],b[30];
void solve(){
string s,t;cin>>s>>t;
for(int i=0;i<26;i++){
c[i]=cc[i]=0;
}
for(int i=0;i<s.length();i++){
c[s[i]-'a']++;
}
for(int i=0;i<t.length();i++){
cc[t[i]-'a']++;
}
int ok=1;
for(int i=0;i<26;i++){
if(cc[i]>c[i]) ok=0;
}
if(!ok){
cout<<"0\n";
return;
}
int ans=0;
for(int k=1;k<=t.length();k++){
ok=1;int cnt=0,re=0;
for(int i=0;i<26;i++){
int e=cc[i]/(t.length()/k);
r[i]=cc[i]-e*(t.length()/k);
if(r[i]>t.length()/k){
e+=r[i]/(t.length()/k);
r[i]=r[i]%(t.length()/k);
}
else if(r[i]==0){
while(e>r[i]+t.length()/k) e--,r[i]+=t.length()/k;
}
d[i]=e;
if(e<r[i]||r[i]>t.length()/k) ok=0;
// cout<<i<<" "<<e<<" "<<r[i]<<" "<<t.length()/k<<endl;
cnt+=e;re+=r[i];
}
int ned=k-cnt;
for(int i=0;i<26;i++){
if(!ned) break;
while(ned&&r[i]>t.length()/k){
ned--;d[i]++;r[i]-=t.length()/k;
}
}
if(ok&&cnt==k){
int now=1e9;
for(int i=0;i<26;i++)if(d[i]){
now=min(now,c[i]/d[i]);
}
int res=now-t.length()/cnt,ck=1;
for(int i=0;i<26;i++)//if(d[i])
{
int co=d[i]*now;
co=c[i]-co;
if(co<r[i]) ck=0;
}
// cout<<now<<" "<<k<<" "<<ck<<" "<<cnt<<endl;
res+=ck;
ans=max(ans,res);
}
}
cout<<ans<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int T;cin>>T;
while(T--)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3824kb
input:
2 bajkaaall aal abca cba
output:
2 1
result:
ok 2 number(s): "2 1"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3604kb
input:
16 a a a b b a aa a ab aa ab b ab c aaz az abcde edcba aaaaaaaaaaaabbb aaaaaaaaabb aaaaaazz az aaaaaaaaaz zzzzz gggggggggggggggggggge ggggeeee hyphyphyphyphyphyphyphyphyphyphyphyp eeeeeeeeee hyphyphyphyphyphyphyphyphyphyphyphype eeteeteeteet aaaabbbbbbcccccccc aaabbbbbcccccc
output:
1 0 0 2 0 1 0 1 1 1 2 0 0 0 0 1
result:
wrong answer 10th numbers differ - expected: '2', found: '1'