#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int d,i;
cin>>d;
char str[d];
int a=0,p=0;
for(i=0;i<d;i++)
{
cin>>str[i];
if(str[i]=='A')
{
a++;
}
else{
p++;
}
}int min=0;
vector<int>vec;
if(d%4==0)
{
min=(d/4)*3;
}
else
{
min=((d/4)*3)+1;
}
if(min>p)
{
for(i=2;i<d-2;i++)
{
if((str[i-1]=='P'||str[i-2]=='P'||str[i+1]=='P'||str[i+2]=='P')&&(str[i]=='A') )
{
vec.push_back(i);
}
}
if(min-p<=vec.size())
{
cout<<min-p<<endl;
}
else{
cout<<"-1"<<endl;
}
}
else{
cout<<"0"<<endl;
}
}
return 0;
}
Consider the test input:
1
3
APP
2 Likes
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int d,i;
cin>>d;
char str[d];
int a=0,p=0;
for(i=0;i<d;i++)
{
cin>>str[i];
if(str[i]=='A')
{
a++;
}
else{
p++;
}
}int min=0;
vector<int>vec;
if(d<=4)
{
if(d==4)
{
if(p>=3)
{
cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
if(d==3)
{
if(p==3)
{
cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
if(d==2)
{
if(p==2)
{cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
if(d==1)
{
if(p==1)
{cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
}
else
{
if(d%4==0)
{
min=(d/4)*3;
}
else
{
min=((d/4)*3)+1;
}
if(min>p)
{
for(i=2;i<d-2;i++)
{
if(((str[i-1]=='P'||str[i-2]=='P')&&(str[i+1]=='P'||str[i+2]=='P'))&&(str[i]=='A') )
{
vec.push_back(i);
}
}
if(min-p<=vec.size())
{
cout<<min-p<<endl;
}
else{
cout<<"-1"<<endl;
}
}
else{
cout<<"0"<<endl;
}
}
}
return 0;
}
still its wa
1
7
PPPAAAP
2 Likes
can you tell me how to find the total no of āpā s required for a given d.
is their a function which rounds off a number to a higher number than itself