QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#140563 | #6628. Flip it and Stick it | chengcheng567 | 0 | 0ms | 0kb | C++14 | 1.9kb | 2023-08-16 10:54:24 | 2023-08-16 10:54:27 |
answer
#include<bits/stdc++.h>
using namespace std;
int T,n,m;string s,t;
void solve1(){
for(int i=0;i<n;i++)
if(s[i]==t[0]){
cout<<"-1\n";
return;
}
cout<<"0\n";
return;
}void solve2(){
int ans=0;
for(int i=1;i<n;i++)
if(s[i]==t[1]&&s[i]!=s[i-1])
ans++;
cout<<ans<<"\n";
return;
}void solve3(){
int c=0,d=0;
for(int i=0;i<n;i++)
if(s[i]==t[0])c++;
else d++;
if(n%2==1&&c>d+1){
cout<<"-1\n";
return;
}if(n%2==0&&c>d){
cout<<"-1\n";
return;
}c=0;
for(int i=1;i<n;i++)
if(s[i]==s[i-1]&&s[i]==t[0])c++;
cout<<c<<"\n";
return;
}void solve4(){
int ans=0;
for(int i=0;i<n-2;i++)
if(s[i]==t[0]&&s[i+1]==t[1]&&s[i+2]==t[2])
ans++;
cout<<ans<<"\n";
return;
}void solve5(){
int ans=0;
for(int i=0;i<n-2;i++)
if(s[i]==t[0]&&s[i+1]==t[1]&&s[i+2]==t[2])
ans++;
cout<<(ans+1)/2<<"\n";
return;
}int g[200010],tot,h[200010],to;
void solve6(){
int c=0,d=0;tot=to=0;
for(int i=0;i<n;i++)
if(s[i]==t[0])c++;
else{
d++;
g[++tot]=i;
}
int maxc=(d+1)*2;
if(c>maxc){
cout<<"-1\n";
return;
}int ans=0;
if(tot){
h[++to]=g[1];
for(int i=1;i<tot;i++)
h[++to]=g[i+1]-g[i]-1;
h[++to]=n-g[tot]-1;
sort(h+1,h+to+1);
assert(to==d+1);
int st=1,ed=to;
while(h[ed]>=3&&st<=ed){
ans++;
int c=min(2-h[st],h[ed]-2);
h[ed]=h[ed]+h[st]-c;h[st]=c;
if(h[st]>=2)st++;
if(h[ed]<3)ed--;
}
}cout<<ans<<"\n";
return;
}signed main(){
freopen("shin.in","r",stdin);
freopen("shin.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);cin>>T;
while(T--){
cin>>s>>t;
n=(int)s.length();
m=(int)t.length();
if(m==1)solve1();
else if(m==2&&t[0]!=t[1])
solve2();
else if(m==2&&t[0]==t[1])
solve3();
else if(m==3&&t[0]!=t[2]){
if(t[1]==t[2]){
reverse(t.begin(),t.end());
reverse(s.begin(),s.end());
}solve4();
}else if(m==3&&t[0]!=t[1])
solve5();
else solve6();
}return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
1 0
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #8:
score: 0
Dangerous Syscalls
input:
0 01
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Dangerous Syscalls
Test #40:
score: 0
Dangerous Syscalls
input:
11 011
output:
result:
Subtask #5:
score: 0
Dangerous Syscalls
Test #53:
score: 0
Dangerous Syscalls
input:
11 011
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #4:
0%