exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 8c6eff0cb2d385327345b866abc69b9b | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
while (t--!=0){
String a=next();
char[]arr=a.toCharArray();
int count1=0,count2=0;
boolean g=true;
for(int i=0;i<arr.length;i++){
if(arr[i]=='A')count1++;
if(arr[i]=='B')count2++;
if(count1<count2)g=false;
}
if(arr[0]!='A'||arr[arr.length-1]!='B'||!g)pw.println("NO");
else pw.println("YES");
}
pw.close();
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(0);
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 0a37a621aed62e2be7ca4fbb1cfb8493 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Practice1 {
// static long[] sort(long[] arr) {
// int n=arr.length;
// ArrayList<Long> al=new ArrayList<>();
// for(int i=0;i<n;i++) {
// al.add(arr[i]);
// }
// Collections.sort(al);
// for(int i=0;i<n;i++) {
// arr[i]=al.get(i);
// }
// return arr;
// }
//
// static long nCr(int n, int r)
// {
// // int x=1000000007;
// long dp[][]=new long[2][r+1];
//
// for(int i=0;i<=n;i++){
// for(int j=0;j<=i&&j<=r;j++){
// if(i==0||j==0||i==j){
// dp[i%2][j]=1;
// }else {
//
// // dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1])%x;
// dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1]);
// }
//
// }
// }
//
// return dp[n%2][r];
//
// }
//
public static class UnionFind {
private final int[] p;
public UnionFind(int n) {
p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
public int find(int x) {
return x == p[x] ? x : (p[x] = find(p[x]));
}
public void union(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
p[x] = y;
}
}
}
public static boolean ispalin(String str) {
int n=str.length();
for(int i=0;i<n/2;i++) {
if(str.charAt(i)!=str.charAt(n-i-1)) {
return false;
}
}
return true;
}
static long power(long N,long R)
{
long x=1000000007;
if(R==0) return 1;
if(R==1) return N;
long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p
temp=(temp*temp)%x;
if(R%2==0){
return temp%x;
}else{
return (N*temp)%x;
}
}
public static String binary(int n) {
StringBuffer ans=new StringBuffer();
int a=4;
while(a-->0) {
int temp=(n&1);
if(temp!=0) {
ans.append('1');
}else {
ans.append('0');
}
n =n>>1;
}
ans=ans.reverse();
return ans.toString();
}
public static int find(String[][] arr,boolean[][] vis,int dir,int i,int j) {
if(i<0||i>=arr.length||j<0||j>=arr[0].length) return 0;
if(vis[i][j]==true) return 0;
if(dir==1&&arr[i][j].charAt(0)=='1') return 0;
if(dir==2&&arr[i][j].charAt(1)=='1') return 0;
if(dir==3&&arr[i][j].charAt(2)=='1') return 0;
if(dir==4&&arr[i][j].charAt(3)=='1') return 0;
vis[i][j]=true;
int a=find(arr,vis,1,i+1,j);
int b=find(arr,vis,2,i-1,j);
int c=find(arr,vis,3,i,j+1);
int d=find(arr,vis,4,i,j-1);
return 1+a+b+c+d;
}
// static ArrayList<Integer> allDivisors(int n) {
// ArrayList<Integer> al=new ArrayList<>();
// int i=2;
// while(i*i<=n) {
// if(n%i==0) al.add(i);
// if(n%i==0&&i*i!=n) al.add(n/i);
// i++;
// }
// return al;
// }
// static int[] sort(int[] arr) {
// int n=arr.length;
// ArrayList<Integer> al=new ArrayList<>();
// for(int i=0;i<n;i++) {
// al.add(arr[i]);
// }
// Collections.sort(al);
// for(int i=0;i<n;i++) {
// arr[i]=al.get(i);
// }
// return arr;
// }
//
/** Code for Dijkstra's algorithm **/
public static class ListNode {
int vertex, weight;
ListNode(int v, int w) {
vertex = v;
weight = w;
}
int getVertex() { return vertex; }
int getWeight() { return weight; }
}
public static int[] dijkstra(
int V, ArrayList<ArrayList<ListNode> > graph,
int source) {
int[] distance = new int[V];
for (int i = 0; i < V; i++)
distance[i] = Integer.MAX_VALUE;
distance[0] = 0;
PriorityQueue<ListNode> pq = new PriorityQueue<>(
(v1, v2) -> v1.getWeight() - v2.getWeight());
pq.add(new ListNode(source, 0));
while (pq.size() > 0) {
ListNode current = pq.poll();
for (ListNode n :
graph.get(current.getVertex())) {
if (distance[current.getVertex()]
+ n.getWeight()
< distance[n.getVertex()]) {
distance[n.getVertex()]
= n.getWeight()
+ distance[current.getVertex()];
pq.add(new ListNode(
n.getVertex(),
distance[n.getVertex()]));
}
}
}
// If you want to calculate distance from source to
// a particular target, you can return
// distance[target]
return distance;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static class Pair{
int l;
int r;
char c;
Pair(int l,int r,char c){
this.l=l;
this.r=r;
this.c=c;
}
}
public static void fill(int[][] arr,boolean[][] vis, int i,int j,int k,int l) {
int n=arr.length;
int m=arr[0].length;
if(i<0||j<0||i>=n||j>=m||vis[i][j]==true) return;
vis[i][j]=true;
int max=0;
if(i+1<n) {
max=Math.max(arr[i+1][j],max);
}
if(i-1>=0) {
max=Math.max(arr[i-1][j],max);
}
if(j+1<m) {
max=Math.max(arr[i][j+1],max);
}
if(j-1>=0) {
max=Math.max(arr[i][j-1],max);
}
arr[i][j]=Math.min(l-max,k);
fill(arr,vis,i+1,j,k,l);
fill(arr,vis,i-1,j,k,l);
fill(arr,vis,i,j+1,k,l);
fill(arr,vis,i,j-1,k,l);
}
static long nCr(int n, int r)
{
// int x=1000000007;
long dp[][]=new long[2][r+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=i&&j<=r;j++){
if(i==0||j==0||i==j){
dp[i%2][j]=1;
}else {
// dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1])%x;
dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1]);
}
}
}
return dp[n%2][r];
}
static long[] sort(long[] arr) {
int n=arr.length;
ArrayList<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static int[] sort(int[] arr) {
int n=arr.length;
ArrayList<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static boolean isPrime(int n)
{
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
public static void main (String[] args) {
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// out.print();
//out.println();
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
String str=sc.nextLine();
int n=str.length();
if(n==1) {
out.println("NO");
continue;
}
int a=0,b=0;
boolean bol=true;
if(str.charAt(0)=='B'||str.charAt(n-1)=='A') bol=false;
int i=0;
while(i<n){
if(str.charAt(i)=='A') {
a++;
}else {
b++;
}
if(a<b) {
bol=false;
}
i++;
}
if(bol==true) {
out.println("YES");
}else {
out.println("NO");
}
}
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 2eb1b2909708c80f6902d7a8787191ee | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class CodeforcesComp {
public static void main(String[] args) throws IOException {
_4B();
}
private static void _4B() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
String original = bi.readLine();
int As = 0, Bs = 0;
boolean flag = false;
for (int j = 0; j < original.length(); j++) {
char c = original.charAt(j);
if(c == 'A'){
As++;
} else if(c == 'B'){
Bs++;
}
if(Bs > As) {
flag = true;
}
}
//ABABBAAB
//ABABB
if(original.startsWith("A") && original.endsWith("B") && !flag && (As+Bs == original.length()) && (As >= Bs)){
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
private static void _4A() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(bi.readLine());
String[] as = bi.readLine().split(" ");
int[] a = new int[n];
int moves = 0;
for (int j = 0; j < n; j++) {
a[j] = Integer.parseInt(as[j]);
moves += a[j] - 1;
//2 == 1 1 = 1
//3 == 2 1 == 1 1 1 = 2
//4 == 3 1 = #3 = 3
//4 == 2 2 = #2 = 3
//5 == 4 1 = 2
//5 == 3 2 = 3
}
System.out.println(moves % 2 == 1 ? "errorgorn" : "maomao90");
}
}
private static void _3F() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(bi.readLine());
String[] as = bi.readLine().split(" ");
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = Integer.parseInt(as[j]);
}
int[] sumsL = new int[n];
sumsL[0] = arr[0];
for (int j = 1; j < n; j++) {
sumsL[j] = arr[j] + sumsL[j - 1];
}
int[] sumsR = new int[n];
sumsR[0] = arr[n - 1];
for (int j = n-2; j >= 0; j--) {
sumsR[(n-1) - j] = arr[j] + sumsR[(n-1) - j - 1];
}
int candies = 0;
ArrayList<Integer> sumsRlist = new ArrayList<>();
for (int integer : sumsR) sumsRlist.add(integer);
for (int j = n-1; j >= 0; j--) {
int leftInRight = Collections.binarySearch(sumsRlist, sumsL[j]);
if(leftInRight > -1){
if(leftInRight + j + 2 <= n) {
candies = leftInRight + j + 2;
break;
}
}
}
System.out.println(candies);
}
}
private static void _3E() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(bi.readLine());
int[] starts = new int[11];
int[] ends = new int[11];
String[] arr = new String[n];
for (int j = 0; j < n; j++) {
String s = bi.readLine();
arr[j] = s;
starts[s.charAt(0) - 'a']++;
ends[s.charAt(1) - 'a']++;
}
int pairs = 0;
for (int j = 0; j < 11; j++) {
pairs += starts[j] * (starts[j] - 1) / 2;
pairs += ends[j] * (ends[j] - 1) / 2;
}
Arrays.sort(arr);
int repeats = 0;
for (int j = 0; j < n - 1; j++) {
if(arr[j].equals(arr[j+1])) {
int toAdd = 2;
int shift = 2;
while(j+shift < n && arr[j].equals(arr[j+shift])) {
shift++;
toAdd += Math.pow(2, shift-1);
}
repeats += toAdd;
j += shift - 1;
}
}
if(repeats > 0) pairs -= repeats;
System.out.println(pairs);
}
}
private static void _3D() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(bi.readLine());
String str = bi.readLine();
String[] a = str.split("W");
boolean flag = false;
for (String s : a) {
if(s.length() > 0 && (!s.contains("B") || !s.contains("R"))) {
flag = true;
break;
}
}
System.out.println(flag ? "NO" : "YES");
}
}
private static void _3C() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(bi.readLine());
String[] as = bi.readLine().split(" ");
int[] arr = new int[n];
int evenShouldBe = 0;
int oddShouldBe = 0;
boolean YES = true;
for (int j = 0; j < n; j++) {
arr[j] = Integer.parseInt(as[j]);
if(j == 0) evenShouldBe = arr[j] % 2;
if(j == 1) oddShouldBe = arr[j] % 2;
if(j > 1){
if(j % 2 == 0 && arr[j] % 2 != evenShouldBe) {
YES = false;
break;
}
else if(j % 2 == 1 && arr[j] % 2 != oddShouldBe) {
YES = false;
break;
}
}
}
if(YES) System.out.println("YES");
else System.out.println("NO");
}
}
private static void _3B() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(bi.readLine());
int[] arr = new int[n];
String[] as = bi.readLine().split(" ");
for (int j = 0; j < n; j++) arr[j] = Integer.parseInt(as[j]);
Arrays.sort(arr);
if(n < 3) {
System.out.println("-1");
continue;
}
for (int j = 0; j < n-2; j++) {
if(arr[j] == arr[j+2]) {
System.out.println(arr[j]);
break;
}
if(j == n-3) System.out.println("-1");
}
}
}
private static void _3A() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(bi.readLine());
if(n <= 1399) System.out.println("Division 4");
else if(n <= 1599) System.out.println("Division 3");
else if(n <= 1899) System.out.println("Division 2");
else System.out.println("Division 1");
}
}
private static void _2B() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
String[] nm = bi.readLine().split(" ");
int n = Integer.parseInt(nm[0]);
int m = Integer.parseInt(nm[1]);
String[] as = bi.readLine().split(" ");
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = Integer.parseInt(as[j]);
}
Arrays.sort(a);
if(n > m) {
System.out.println("NO");
continue;
}
int peopleAdded = 0;
int earliestPersonPos = 0, earliestPersonSpace = 0;
int lastPersonPos = m-1, lastPersonSpace = 0;
boolean earlyPersonNow = true;
for (int pos : a) {
if(peopleAdded > 0 &&
((lastPersonPos - earliestPersonPos - 1) < Math.max(earliestPersonSpace, lastPersonSpace)
|| lastPersonPos <= earliestPersonPos)) {
break;
}
if(earlyPersonNow) {
if(peopleAdded == -1) {
earliestPersonPos = 0;
earliestPersonSpace = pos*2;
}else {
earliestPersonPos += Math.max(pos, earliestPersonSpace) + 1;
earliestPersonSpace = pos;
}
// System.out.println(earliestPersonPos);
} else {
if(peopleAdded == -1) {
lastPersonPos = m-1;
lastPersonSpace = pos*2;
} else {
lastPersonPos -= Math.max(lastPersonSpace, pos) + 1;
lastPersonSpace = pos;
}
// System.out.println(lastPersonPos);
}
if(lastPersonPos > m - 1 || earliestPersonPos < 0) break;
peopleAdded++;
earlyPersonNow = !earlyPersonNow;
}
System.out.println(peopleAdded == n ? "YES" : "NO");
}
}
private static void _2A() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
String[] nm = bi.readLine().split(" ");
int n = Integer.parseInt(nm[0]);
int m = Integer.parseInt(nm[1]);
if(n == m) System.out.println((n-1)+(m-1));
else if(Math.abs(n-m) == 1){
System.out.println((n-1)+(m-1));
}
else if(n == 1 || m == 1) System.out.println(-1);
else if(Math.abs(n-m) > 1){
int diff = Math.abs(n - m);
if(diff % 2 == 1) diff--;
System.out.println((n-1)+(m-1) + diff);
}
}
}
private static void _B() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
String[] nk = bi.readLine().split(" ");
int n = Integer.parseInt(nk[0]);
int k = Integer.parseInt(nk[1]);
String str = bi.readLine();
for (int j = 1; j < k; j++) {
StringBuilder newString = new StringBuilder();
if(j % 2 == 1){
newString.append(str.substring(0, 1));
for (int s = 1; s < str.length(); s++) {
newString.append(1 - Integer.parseInt(str.substring(s, s + 1)));
}
} else {
for (int s = 0; s < str.length() - 1; s++) {
newString.append(1 - Integer.parseInt(str.substring(s, s + 1)));
}
newString.append(str.substring(str.length() - 1));
}
str = String.valueOf(newString);
}
System.out.println(str);
}
}
private static void _A() throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bi.readLine());
for (int i = 0; i < t; i++) {
String[] nrb = bi.readLine().split(" ");
int n = Integer.parseInt(nrb[0]);
int r = Integer.parseInt(nrb[1]);
int b = Integer.parseInt(nrb[2]);
int largestStreak = (int) ((r - b)/(b + 1.0)) + 1;
StringBuilder ans = new StringBuilder();
while(ans.length() < n && r > 0 && b > 0) {
for (int j = 0; j < largestStreak; j++) {
ans.append("R");
}
r -= largestStreak;
ans.append("B");
b--;
largestStreak = (int) ((r - b)/(b + 1.0)) + 1;
}
for (int j = 0; j < r; j++) {
ans.append("R");
}
System.out.println(ans);
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 6bf41e724a05c22b3aca554c14077461 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
public class CF1{
public static void main(String[] args) {
FastScanner sc=new FastScanner();
int T=sc.nextInt();
// int T=1;
for (int tt=1; tt<=T; tt++){
String s = sc.next();
boolean ans =true;
int counta=0;
int countb=0;
int n = s.length();
for(int i=0;i<n; i++){
if(s.charAt(i)=='A') counta++;
else countb++;
if(counta<countb){
ans=false;
break;
}
}
if(s.charAt(0)=='B' || s.charAt(n-1)=='A') ans=false;
if (ans) System.out.println("YES");
else System.out.println("NO");
}
}
static class SegmentTree{
int nodes[];
int arr[];
int lazy[];
public SegmentTree(int n, int arr[]){
nodes = new int [4*n+1];
lazy= new int [4*n+1];
this.arr=arr;
build(1,1,n);
}
private void build (int v, int l , int r){
if (l==r) {
nodes[v]=arr[l-1];}
else {
int m=(l+r)/2;
build(2*v,l,m);
build(2*v+1,m+1,r);
nodes[v]=Math.min(nodes[2*v], nodes[2*v+1]);
}
}
private void push(int v) {
nodes[v*2] += lazy[v];
lazy[v*2] += lazy[v];
nodes[v*2+1] += lazy[v];
lazy[v*2+1] += lazy[v];
lazy[v] = 0;
}
private void update(int v, int l, int r, int x, int y, int add) {
if (l>y || r<x) return;
if (l>=x && y>=r) {
nodes[v]+=add;
lazy[v]+=add;
}
else {
push(v);
int mid =(l+r)/2;
update(2*v,l,mid,x,y,add);
update(2*v+1,mid+1,r,x,y,add);
nodes[v]=Math.min(nodes[v*2], nodes[v*2+1]);
}
}
private int query(int v, int l, int r, int x, int y){
if (l>y || r<x) return Integer.MAX_VALUE;
if (l>=x && r<=y) return nodes[v];
else {
int m = (l+r)/2;
push(v);
return Math.min(query(2*v+1,m+1,r,x,y),query(2*v,l,m,x,y));
}
}
}
static class LPair{
long x,y;
LPair(long x , long y){
this.x=x;
this.y=y;
}
}
static long prime(long n){
for (long i=3; i*i<=n; i+=2){
if (n%i==0) return i;
}
return -1;
}
static long factorial (int x){
if (x==0) return 1;
long ans =x;
for (int i=x-1; i>=1; i--){
ans*=i;
ans%=mod;
}
return ans;
}
static long mod =1000000007L;
static long power2 (long a, long b){
long res=1;
while (b>0){
if ((b&1)== 1){
res= (res * a % mod)%mod;
}
a=(a%mod * a%mod)%mod;
b=b>>1;
}
return res;
}
static boolean []sieveOfEratosthenes(int n){
boolean prime[] = new boolean[n+1];
for(int i=0;i<=n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static void sort(int[] a){
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sortLong(long[] a){
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static long gcd (long n, long m){
if (m==0) return n;
else return gcd(m, n%m);
}
static class Pair implements Comparable<Pair>{
int x,y;
private static final int hashMultiplier = BigInteger.valueOf(new Random().nextInt(1000) + 100).nextProbablePrime().intValue();
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pii = (Pair) o;
if (x != pii.x) return false;
return y == pii.y;
}
public int hashCode() {
return hashMultiplier * x + y;
}
public int compareTo(Pair o){
if (this.x==o.x) return Integer.compare(this.y,o.y);
else return Integer.compare(this.x,o.x);
}
// this.x-o.x is ascending
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 8fe6f622f65d46ecd5ac7116095d27d9 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
static int n,k;
static long ans;
static ArrayList<Integer>[] g;
static boolean[] vis;
static int[][] binLift;
static int[] depth;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
// Scanner sc = new Scanner(new File("consistency_chapter_1_input.txt"));
// PrintWriter pw = new PrintWriter("A_out.txt");
int t = sc.nextInt();
while (t-- > 0) {
char[] a = sc.next().toCharArray();
boolean f = a[0]!='B' && a.length>1 && a[a.length-1]=='B';
int s = 0;
int e = 0;
for (int i = 0; i < a.length;i++) {
if(a[i]=='A')
s++;
else
e++;
f&= s>=e;
}
pw.println(f?"YES":"NO");
}
pw.flush();
}
static int mod = 998244353;
static int modPow(int a, int e, int mod) // O(log e)
{
a %= mod;
int res = 1;
while (e > 0) {
if ((e & 1) == 1)
res = (res * a) % mod;
a = (a * a) % mod;
e >>= 1;
}
return res;
}
static class pair implements Comparable<pair> {
int x, y;
public pair(int u, int s) {
x = u;
y = s;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return x + " " + y;
}
@Override
public int compareTo(pair o) {
// TODO Auto-generated method stub
return x - o.x;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(File s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 7f1a42c3f454e6692a71a33d9fbd8f67 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author real
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BILoveAAAB solver = new BILoveAAAB();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class BILoveAAAB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
String s = in.readString();
int cntA = 0;
int cnb = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'B') {
cnb++;
} else
cntA++;
if (cntA >= cnb) {
} else {
out.println("NO");
return;
}
}
if (cnb != 0 && s.charAt(s.length() - 1) == 'B')
out.println("YES");
else
out.println("NO");
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
//*-*------clare-----anjlika---
//remeber while comparing 2 non primitive data type not to use ==
//remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort
//again silly mistakes ,yr kb tk krta rhega ye mistakes
//try to write simple codes ,break it into simple things
//knowledge>rating
/*
public class Main
implements Runnable{
public static void main(String[] args) {
new Thread(null,new Main(),"Main",1<<26).start();
}
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
libraries.InputReader in = new libraries.InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();//chenge the name of task
solver.solve(1, in, out);
out.close();
}
*/
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String next() {
return readString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 392373754fbaa87a0617798fc2cd67ae | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
new Thread(null, () -> new Main().run(), "1", 1 << 23).start();
}
private void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Solution solve = new Solution();
int t = scan.nextInt();
// int t = 1;
for (int qq = 0; qq < t; qq++) {
solve.solve(scan, out);
out.println();
}
out.close();
}
}
class Solution {
/*
* think and coding
*/
static long MOD = (long) (1e9);
double EPS = 0.000_0001;
public void solve(FastReader scan, PrintWriter out) {
char[] arr = scan.nextLine().toCharArray();
int i = 0, j = 0;
if (arr.length == 1 || arr[arr.length - 1] == 'A'){
out.print("NO");
return;
}
for (char c : arr) {
if (c == 'A') {
i++;
}
if (c == 'B') {
j++;
}
if (j > i) {
out.print("NO");
return;
}
}
out.print("YES");
}
int lower(int val, Pair[] arr) {
int l = -1, r = arr.length;
while (r - l > 1) {
int mid = (r + l) / 2;
if (arr[mid].a < val) {
l = mid;
} else r = mid;
}
return r;
}
static class Pair implements Comparable<Pair> {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public Pair(Pair p) {
this.a = p.a;
this.b = p.b;
}
@Override
public int compareTo(Pair p) {
return Integer.compare(a, p.a);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public String toString() {
return "Pair{" + "a=" + a + ", b=" + b + '}';
}
}
}
class FastReader {
private final BufferedReader br;
private StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] initInt(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
long[] initLong(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | dfb77b1c680e3701ebd4a8840f9ae1b7 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.lang.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i = 0; i < n; i++)
{
char[] x = scan.next().toCharArray();
int A = 0;
boolean sum = true;
for(char o: x)
{
if(o == 'A') A++;
else
A--;
if(A < 0) sum = false;
}
if(x.length == 1)
System.out.println("NO");
else if(x[0] != 'A' || x[x.length - 1] != 'B')
System.out.println("NO");
else if(sum)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 6f64bd24f87febd5939530525c30b87a | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | // package GlobalRound20;
import java.io.*;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = 1;
t = sc.nextInt();
while(t-->0){
char [] a = sc.nextLine().toCharArray();
boolean can=true;
if(a.length==1)can=false;
boolean b= false;
int countA=0;
for(int i =0;i<a.length;i++){
if(a[i]=='B'){
int countB = i-countA+1;
if(countB>countA)can=false;
b=true;
}
else countA++;
}
if(a[a.length-1]=='B')b=true;
else can=false;
if(can&&b){
pw.println("YES");
}else{
pw.println("NO");
}
}
pw.close();
}
// -------------------------------------------------------Scanner---------------------------------------------------
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 4bb3b8d1cc2b62c9319e0102f27e72b3 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.util.stream.*;
import java.util.regex.*;
public class Sequence {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
scan.nextLine();
StringBuilder result = new StringBuilder();
Pattern p = Pattern.compile("(A+B+)+");
for(int i = 0; i < t; i++) {
String s2 = scan.nextLine();
String res = "";
if(p.matcher(s2).matches()) {
int numA = 0;
int numB = 0;
int j = 0;
for(; j < s2.length(); j++) {
if(s2.charAt(j) == 'A') numA++; else numB++;
if(numA < numB) {
res = "NO";
break;
}
}
if(j == s2.length()) {
res = "YES";
}
} else {
res = "NO";
}
result.append(res + "\n");
}
System.out.println(result);
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | e17c382c406992b79c374d02093dc66b | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class B1672 {
public static void main(String[] args) throws NumberFormatException, IOException {
r.init();
int t = r.readInt();
while(t-- > 0)
{
String s = r.read();
System.out.println(solve(s));
}
}
private static String solve(String s) {
boolean ans = true;
int a = 0, b = 0;
if(s.charAt(0) == 'B' || s.charAt(s.length()-1) != 'B')
{
return "NO";
}
for(int i = 0; i < s.length() && ans; i++)
{
if(s.charAt(i) == 'A') a++;
else b++;
if(a < b) ans = false;
}
return (ans ? "YES" : "NO");
}
static class r {
// int[] in = r.readInts(2);
// n = in[0];
// k = in[1];
// r.init();
// int t = r.readInt();
// while(t-- > 0)
// {
//
// }
static BufferedReader br;
static StringTokenizer st;
public static void init() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public static String read() throws NumberFormatException, IOException
{
return br.readLine();
}
public static int readInt() throws NumberFormatException, IOException
{
return Integer.parseInt(br.readLine());
}
public static int[] readInts(int n) throws NumberFormatException, IOException
{
st = new StringTokenizer(br.readLine(), " ");
int[] arr = new int[n];
for(int i = 0; i < n; i++)
{
arr[i] = Integer.parseInt(st.nextToken());
}
return arr;
}
public static ArrayList<Integer> readIntsAsList(int n) throws NumberFormatException, IOException
{
st = new StringTokenizer(br.readLine(), " ");
ArrayList<Integer> al = new ArrayList<>();
for(int i = 0; i < n; i++)
{
al.add(Integer.parseInt(st.nextToken()));
}
return al;
}
public static long readLong() throws NumberFormatException, IOException
{
return Long.parseLong(br.readLine());
}
public static long[] readLongs(int n) throws NumberFormatException, IOException
{
st = new StringTokenizer(br.readLine(), " ");
long[] arr = new long[n];
for(int i = 0; i < n; i++)
{
arr[i] = Long.parseLong(st.nextToken());
}
return arr;
}
public static ArrayList<Long> readLongsAsList(int n) throws NumberFormatException, IOException
{
st = new StringTokenizer(br.readLine(), " ");
ArrayList<Long> al = new ArrayList<>();
for(int i = 0; i < n; i++)
{
al.add(Long.parseLong(st.nextToken()));
}
return al;
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | a8e7711595ebb770df70dfb87c2bdb4a | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SumOfGoodNumbers {
private static final String YES = "YES";
private static final String NO = "NO";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numOfInputs = scanner.nextInt();
scanner.nextLine();
List<String> inputs = new ArrayList<>(numOfInputs);
for( int i = 0; i < numOfInputs; i++ )
{
inputs.add(scanner.nextLine());
}
for( int i = 0; i < numOfInputs; i++ )
{
String result = null;
String input = inputs.get(i);
int length = input.length();
if( length < 2 )
{
result = NO;
}
else
{
char firstCharac = input.charAt(0);
char lastCharac = input.charAt(length-1);
if( firstCharac != 'A' || lastCharac != 'B')
{
result = NO;
}
else
{
int countOfA = 0;
int countOfB = 0;
for(int j = 0; j < length; j++)
{
char character = input.charAt(j);
if( character == 'A')
{
countOfA++;
}
else if( character == 'B')
{
countOfB++;
}
else
{
result = NO;
break;
}
if( countOfB > countOfA )
{
result = NO;
break;
}
}
if( result == null)
{
result = YES;
}
}
}
System.out.println(result);
}
scanner.close();
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 55fca4c13a7484efdba5da03299ca835 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class ILoveAAB {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while(t-->0) {
scanner.nextLine();
String str = scanner.next();
boolean isValid = true;
int count = 0;
if (str.charAt(str.length() - 1) != 'B') isValid = false;
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == 'A') {
count++;
} else {
count--;
}
if (count < 0) {
isValid = false;
break;
}
}
if (isValid) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 419a4dc6409ab2d483daeb4bf8d1b1c9 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class B_1672 {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int t=Integer.parseInt(sc.nextLine());
while(t-->0) {
char[] ch=sc.next().toCharArray();
boolean ans=true;
int a=0;
int b=0;
if(ch[0]=='B' || ch[ch.length-1]=='A') ans=false;
for(int i=0;i<ch.length && ans;i++ ) {
if(ch[i]=='A') a++;
else b++;
if(a<b) ans=false;
}
System.out.println(ans?"YES":"NO");
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 667c199055377c44f532a552a8fafd3e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Codeforces {
static int M = 1_000_000_007;
static int INF = 2_000_000_000;
static int N = (int) 2e5 + 1;
static long[] factorial;
static boolean[] isPrime = new boolean[N+1];
static final FastScanner fs = new FastScanner();
//variable
public static void main(String[] args) throws IOException {
int T = fs.nextInt();
while (T-- > 0) {
char[] arr = fs.next().toCharArray();
int x = 0;
boolean ans = true;
for(int i=0;i<arr.length;i++) {
if(arr[i] == 'A') {
x++;
}else {
x--;
}
if(x < 0) ans = false;
}
if(arr[arr.length-1] == 'A') ans = false;
System.out.println(ans && arr.length>1 ? "Yes" : "No");
}
}
//class
//function
static void getPrimes() {
for(int i=2;i<N;i++) isPrime[i] = true;
int i = 2;
while(i<N) {
if(isPrime[i]) {
for(int j=2*i;j<N;j+=i)
isPrime[j] = false;
}
System.out.println(i+" "+isPrime[i]);
i++;
}
}
static void build(int[] a, int[] seg, int ind, int low, int high) {
if (low == high) {
seg[ind] = a[low];
return;
}
int mid = (low + high) / 2;
build(a, seg, 2 * ind + 1, low, mid);
build(a, seg, 2 * ind + 2, mid + 1, high);
seg[ind] = Math.max(seg[2 * ind + 1], seg[2 * ind + 2]);
}
static long query(int ind, int[] seg, int l, int h, int low, int high) {
if (low > h || high < l) return -INF;
if (low >= l && high <= h) return seg[ind];
int mid = (low + high) / 2;
long left = query(2 * ind + 1, seg, l, h, low, mid);
long right = query(2 * ind + 2, seg, l, h, mid + 1, high);
return Math.max(left, right);
}
// Template
static long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
static void premutation(int n, ArrayList<Integer> arr, boolean[] chosen) {
if (arr.size() == n) {
System.out.println(arr);
} else {
for (int i = 1; i <= n; i++) {
if (chosen[i]) continue;
arr.add(i);
chosen[i] = true;
premutation(n, arr, chosen);
arr.remove(Integer.valueOf(i));
chosen[i] = false;
}
}
}
static boolean isPalindrome(char[] c) {
int n = c.length;
for (int i = 0; i < n / 2; i++) {
if (c[i] != c[n - i - 1]) return false;
}
return true;
}
static long nCk(int n, int k) {
return (modMult(fact(n), fastExpo(modMult(fact(n - k), fact(k)), M - 2)));
}
static long nPk(int n, int k) {
return (modMult(fact(n), fastExpo(fact(n - k), M - 2)));
}
static long fact(int n) {
if (factorial != null) return factorial[n];
else factorial = new long[N];
factorial[0] = 1;
long fact = 1;
for (int i = 1; i <= n; i++) {
factorial[i] = fact = modMult(fact, i);
}
return fact % M;
}
static long modMult(long a, long b) {
return (int) (a * b % M);
}
static long negMult(long a, long b) {
return (int) ((a * b) % M + M) % M;
}
static long fastExpo(long x, int y) {
if (y == 1) return x;
if (y == 0) return 1;
long ans = fastExpo(x, y / 2);
if (y % 2 == 0) return modMult(ans, ans);
else return modMult(ans, modMult(ans, x));
}
static final Random random = new Random();
static void ruffleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int j = random.nextInt(n);
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
private static class Pairs implements Comparable<Pairs> {
int f, s;
Pairs(int f, int s) {
this.f = f;
this.s = s;
}
public int compareTo(Pairs p) {
if (this.f != p.f) return Integer.compare(this.f, p.f);
return -Integer.compare(this.s, p.s);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pairs)) return false;
Pairs pairs = (Pairs) o;
return f == pairs.f && s == pairs.s;
}
@Override
public int hashCode() {
return Objects.hash(f, s);
}
}
private static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer str = new StringTokenizer("");
String next() throws IOException {
while (!str.hasMoreTokens())
str = new StringTokenizer(br.readLine());
return str.nextToken();
}
char nextChar() throws IOException {
return next().charAt(0);
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
float nextFloat() throws IOException {
return Float.parseFloat(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
byte nextByte() throws IOException {
return Byte.parseByte(next());
}
int[] arrayIn(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 76ae2c6cc408f219673403cd18381a5f | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.*;
public class P1672B {
public static void main(String[] args) throws Exception {
new P1672B().run();
}
void run() throws Exception {
Scanner scanner = new Scanner(getInputStream());
int t = scanner.nextInt();
while(t-- > 0) {
String s = scanner.next();
getOutputStream().println(isAB(s) ? "YES" : "NO");
}
}
private boolean isAB(String s) {
if (s.length() < 2) {
return false;
}
if (s.endsWith("A")) {
return false;
}
int diff = 0;
for(char c : s.toCharArray()) {
if (c == 'A') {
diff++;
} else {
diff--;
}
if (diff < 0) {
return false;
}
}
return true;
}
InputStream getInputStream() {
return System.in;
}
PrintStream getOutputStream() {
return System.out;
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 286b85f2fcb73ac5ccb72fab8d9d826a | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class AB {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
PrintWriter pr = new PrintWriter(System.out);
for(int i = 0 ; i<t ; i++){
boolean good = true;
int count = 0;
String s = br.readLine();
if(s.length()==1){
pr.println("NO");
good = false;
}
else if(s.charAt(0)!='A'){
pr.println("NO");
good = false;
}
else if(s.charAt(s.length()-1)!='B'){
pr.println("NO");
good = false;
}
else {for(int j=0 ; j<s.length() ; j++){
if(s.charAt(j)=='B')
count --;
if(s.charAt(j)=='A')
count ++;
if(count < 0){
pr.println("NO");
good = false;
break;
}
}
}
if(good)
pr.println("YES");
}
pr.flush();
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | b4ab94f60d88d2fda92c437ecc56f122 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.Math;
public class cp{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]){
FastReader sc=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0){
String s=sc.nextLine();
boolean flag=true;
if(s.charAt(0)=='B'|| s.charAt(s.length()-1)!='B' || s.length()<2 )
{
out.println("NO");
continue;
}
int ca=0,cb=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='A') ca++;
else cb++;
if(cb>ca){
flag=false;
break;
}
}
if(flag)
out.println("Yes");
else
out.println("No");
}
out.close();
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | f551bf147c65defc1b6239954ddc8e9d | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
import java.lang.String;
public class loveaaab {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int l=0;l<t;l++){
String s=sc.next();
int a=0;
boolean flag=true;
char ar[]=s.toCharArray();
for(int i=0;i<s.length();i++){
if(ar[i]=='B')
{
if(a==0){
flag=false;
break;
}
else{
a--;
}
}else{
a++;
}
}
if(flag && s.endsWith("B"))
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | e23b30213c641dce8684e29915c98ae0 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class ILoveAAAB {
static boolean chk(String s){
if(s.charAt(s.length()-1) == 'A' || s.charAt(0) == 'B')
return false;
LinkedList<Character> l = new LinkedList<>();
for(int i=0; i<s.length() ;i++){
if(s.charAt(i) == 'A')
l.add('A');
else{
if(l.isEmpty())
return false;
l.pollLast();
}
}
return l.size() != s.length();
}
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
String s = sc.next();
if(chk(s))
pw.println("YES");
else
pw.println("NO");
}
pw.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws IOException {
br = new BufferedReader(new FileReader(file));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | baecc7c2f49cdc8b6c80a834a5206dae | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) {
FastReader scanner = new FastReader();
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
String str = scanner.next();
if (str.length() == 1) {
System.out.println("NO");
continue;
}
if (str.charAt(str.length() - 1) != 'B') {
System.out.println("NO");
continue;
}
int cnt = 0;
boolean hasSol = true;
for (int j = 0; j < str.length(); j++) {
if (str.charAt(j) == 'A') {
cnt++;
} else {
cnt--;
if (cnt < 0) {
hasSol = false;
break;
}
}
}
if (hasSol) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 5f3ef7ab8d998fdc1aef3e67fb2f23a9 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | // package CodeforcesGlobalRound20;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) {
FastReader fr = new FastReader();
int t = fr.nextInt();
while (t-- > 0) {
String s2 = fr.nextLine();
int n = s2.length();
boolean flag = true;
if (s2.charAt(0) == 'B' || s2.charAt(n - 1) == 'A') {
flag = false;
}
long ca = 0, cb = 0;
if (flag) {
for (int i = 0; i < n - 1; i++) {
if (s2.charAt(i) == 'A') {
ca++;
}
if (s2.charAt(i + 1) == 'B') {
cb++;
} else {
if (cb > ca) {
flag = false;
break;
}
}
}
}
if (flag && cb <= ca && cb != 0) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | d95d42c513b1cd0f6f6a492a64a32e32 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class B_1672 {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int t=Integer.parseInt(sc.nextLine());
while(t-->0) {
char[] ch=sc.next().toCharArray();
boolean ans=true;
int a=0;
int b=0;
if(ch[0]=='B' || ch[ch.length-1]=='A') ans=false;
for(int i=0;i<ch.length && ans;i++ ) {
if(ch[i]=='A') a++;
else b++;
if(a<b) ans=false;
}
System.out.println(ans?"YES":"NO");
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | adce8ac012b28c6c63789f1768ce0ea4 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | //package Codeforces.Global_Round_20;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.StringTokenizer;
public class B {
static class RealScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
public boolean isSorted(List<Long> list) {
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) > list.get(i + 1))
return false;
}
return true;
}
}
public static void main(String[] args) {
RealScanner sc = new RealScanner();
int t = sc.nextInt();
while (t-- > 0) {
// int n = sc.nextInt();
String s = sc.next();
if (s.length() == 1) {
System.out.println("NO");
continue;
}
if (s.charAt(s.length() - 1) != 'B') {
System.out.println("NO");
continue;
}
int count1 = 0, count2 = 0;
boolean check = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
count1++;
} else {
count2++;
}
if (count1 < count2) {
check = false;
break;
}
}
if (check) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 09ab201f6504516648fcf44c13eaee1a | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T-->0) {
char[] str=sc.next().toCharArray();
boolean flag=true;
int cntA=0;
int cntB=0;
for(int i=0;i<str.length;i++) {
if(str[i]=='A')
cntA++;
else {
cntB++;
if(cntA>=cntB)
continue;
flag=false;
break;
}
}
if(flag && cntB>0 && str[str.length-1]=='B')
System.out.println("YES");
else
System.out.println("NO");
}
sc.close();
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 2146c031bf8871dd53ad6a28b1ec73f8 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
public class habd {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.nextInt();
while(tc-->0){
char[] arr = sc.next().toCharArray();
if(arr.length == 1)pw.println("NO");
else {
boolean ans = true;
int cnt = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'A') cnt++;
else {
if (cnt == 0) {
ans = false;
break;
} else cnt -= 1;
}
}
if (ans && arr[arr.length - 1] != 'A') pw.println("YES");
else pw.println("NO");
}
}
pw.flush();
}
static class SegmentTree{
long[] tree;
int N;
public SegmentTree(long[] arr){
N = arr.length;
tree = new long[2*N - 1];
build(tree, arr);
}
public void build(long[] tree, long[] arr){
for(int i = N-1, j = 0; i<tree.length; i++, j++)tree[i] = arr[j];
for(int i = tree.length - 1, j = i - 1, k = N-2; k>=0; i -= 2, j-= 2, k--){
tree[k] = tree[i] + tree[j];
}
}
public void update(int idx, int val){
tree[idx + N - 2] = val;
boolean f = true;
int i = idx + N - 2;
int j = i - 1;
if(i % 2 != 0){
i++;
j++;
}
for(int k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); k>=0; ){
tree[k] = tree[i] + tree[j];
f = !f;
i = k;
j = k - 1;
if(k % 2 != 0){
i++;
j++;
}
k = (tree.length - N - 1) - ((tree.length - 1 - i)/2);
}
}
}
public static boolean isSorted(Long[] arr){
boolean f = true;
for(int i = 1; i<arr.length; i++){
if(arr[i] < arr[i - 1]){
f = false;
break;
}
}
return f;
}
public static int binary_Search(long key, Long[] arr, int start){
int low = start;
int high = arr.length;
int mid = (low + high) / 2;
while(low < high){
mid = (low + high) / 2;
if(arr[mid] == key)break;
else if(arr[mid] > key){
high = mid;
}else{
low = mid;
}
}
return mid;
}
public static int differences(int n, int test){
int changes = 0;
while(test > 0){
if(test % 10 != n % 10)changes++;
test/=10;
n/=10;
}
return changes;
}
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return start;
}
static int maxSubArraySum2(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return end;
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair x){
if(this.y - x.y != 0) return this.y - x.y;
else return this.x - x.x;
}
public String toString(){
return "("+this.x + ", " + this.y + ")";
}
}
public static boolean isSubsequence(char[] arr, String s){
boolean ans = false;
for(int i = 0, j = 0; i<arr.length; i++){
if(arr[i] == s.charAt(j)){
j++;
}
if(j == s.length()){
ans = true;
break;
}
}
return ans;
}
public static void sortIdx(long[]a,long[]idx) {
mergesortidx(a, idx, 0, a.length-1);
}
static void mergesortidx(long[] arr,long[]idx,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesortidx(arr,idx,b,m);
mergesortidx(arr,idx,m+1,e);
mergeidx(arr,idx,b,m,e);
}
return;
}
static void mergeidx(long[] arr,long[]idx,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
long[] l=new long[len1];
long[] lidx=new long[len1];
long[] r=new long[len2];
long[] ridx=new long[len2];
for(int i=0;i<len1;i++) {
l[i]=arr[b+i];
lidx[i]=idx[b+i];
}
for(int i=0;i<len2;i++) {
r[i]=arr[m+1+i];
ridx[i]=idx[m+1+i];
}
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
arr[k++]=l[i++];
idx[k-1]=lidx[i-1];
}
else {
arr[k++]=r[j++];
idx[k-1]=ridx[j-1];
}
}
while(i<len1) {
idx[k]=lidx[i];
arr[k++]=l[i++];
}
while(j<len2) {
idx[k]=ridx[j];
arr[k++]=r[j++];
}
return;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 0a4cd2801ffb75df2ccbb6e9123dfe90 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | /*############################################################################################################
########################################## >>>> Diaa12360 <<<< ###############################################
########################################### Just Nothing #################################################
#################################### If You Need it, Fight For IT; #########################################
###############################################.-. 1 5 9 2 .-.################################################
############################################################################################################*/
import java.io.*;
import java.net.Inet4Address;
import java.util.*;
import java.util.List;
import java.util.function.Function;
public class Solution {
final static int N = 200002; // 2*10^5
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader in = new BufferedReader(new FileReader("src/input"));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
// int[][] arr = new int[N][32];
// for (int i = 0; i < 32; i++) {
// for (int j = 1; j < N; j++) {
// if (((j >> i) & 1) == 1)
// arr[j][i] = 1;
// arr[j][i] += arr[j-1][i];
// }
// }
// for (int i = 0; i < 16; i++) {
// for (int j = 0; j < 32; j++) {
// System.out.print(arr[i][j] + " ");
// }
// System.out.println();
// }
int t = ints(in.readLine());
while (t-- > 0){
char[] s = in.readLine().toCharArray();
boolean f = true;
int a = 0, b = 0;
for (int i = 0; i < s.length; i++) {
if(s[i] == 'A')
a++;
else
b++;
if(b > a){
f = false;
break;
}
}
if(s[s.length-1] != 'B')
f = false;
out.append(f ? "YES" : "NO").append('\n');
}
System.out.print(out);
}
static int ints(String s) {
return Integer.parseInt(s);
}
static long ll(String s) {
return Long.parseLong(s);
}
static int[] readArray(String s, int n) {
StringTokenizer tk = new StringTokenizer(s);
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = ints(tk.nextToken());
return arr;
}
static int[] readArray(String s) {
StringTokenizer tk = new StringTokenizer(s);
int[] arr = new int[tk.countTokens()];
for (int i = 0; i < arr.length; i++)
arr[i] = ints(tk.nextToken());
return arr;
}
static void printArray(char[][] arr, StringBuilder out) {
out.append("YES").append('\n');
for (char[] chars : arr) {
for (char c : chars) {
out.append(c);
}
out.append('\n');
}
}
static void printArray(String[] arr) {
for (String x : arr) {
System.out.println(x);
}
}
static <T, E> Map<T, E> createMapFromList(List<T> l, Function<T, E> fun) {
Map<T, E> mp = new HashMap<>();
for (T x : l) {
mp.put(x, fun.apply(x));
}
return mp;
}
}
class ArrayStack<E> {
public static final int CAPACITY = 1000;
private E[] data;
private int t = -1;
public ArrayStack() {
this(CAPACITY);
}
public ArrayStack(int capacity) {
data = (E[]) new Object[capacity];
}
public int size() {
return t + 1;
}
public boolean isEmpty() {
return t == -1;
}
public E push(E e) throws IllegalStateException {
if (size() == data.length) throw new IllegalStateException("Stack is full");
data[++t] = e;
return e;
}
public E peek() {
return isEmpty() ? null : data[t];
}
public E pop() {
if (isEmpty())
return null;
E d = data[t];
data[t] = null;
t--;
return d;
}
}
class Pair<E, T> {
E first;
T second;
Pair(E f, T s) {
first = f;
second = s;
}
public E getFirst() {
return first;
}
public T getSecond() {
return second;
}
}
/*
4 4
3 1 3 1
11111111111111111111111111111110
100000000000000000000000000011
100000000000000000000000000001
100000000000000000000000000011
100000000000000000000000000001
100000000000000000000000000001
_________________
0001
0010
00
*/
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 87a185728363772c67db064e8c97eeab | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
/*
* way to red
*/
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class A2
{
public static void main(String AC[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
// long N = Long.parseLong(st.nextToken());
// int N = Integer.parseInt(st.nextToken());
// int[] arr = readArr(N, infile, st);
String s = String.valueOf(st.nextToken());
ArrayList<String> ls = new ArrayList<>();
if(s.length()==1) {
sb.append("NO").append('\n');
}
else {
int cA= 0;
int cB =0;
int parts =0;
boolean flag = true;
for(int i=0;i<s.length();i++) {
if(s.charAt(i)=='A') {
cA++;
}else {
cB++;
}
if(cB>cA) {
flag = false;
}
}
if(s.charAt(s.length()-1)!='B') {
flag = false;
}
if(flag) {
sb.append("YES").append('\n');
}else {
sb.append("NO").append('\n');
}
}
}
out.print(sb);
out.flush();
//BufferedReader infile = new BufferedReader(new FileReader("input.txt"));
//System.setOut(new PrintStream(new File("output.txt")));
}
static final int mod = 1_000_000_007;
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
long[] arr = new long[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Long.parseLong(st.nextToken());
return arr;
}
public static void print(int[] arr)
{
//for debugging only
for(int x: arr)
out.print(x+" ");
out.println();
}
//input shenanigans
/*
Random stuff to try when stuck:
-if it's 2C then it's dp
-for combo/probability problems, expand the given form we're interested in
-make everything the same then build an answer (constructive, make everything 0 then do something)
-something appears in parts of 2 --> model as graph
-assume a greedy then try to show why it works
-find way to simplify into one variable if multiple exist
-treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them)
-find lower and upper bounds on answer
-figure out what ur trying to find and isolate it
-see what observations you have and come up with more continuations
-work backwards (in constructive, go from the goal to the start)
-turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements)
-instead of solving for answer, try solving for complement (ex, find n-(min) instead of max)
-draw something
-simulate a process
-dont implement something unless if ur fairly confident its correct
-after 3 bad submissions move on to next problem if applicable
-do something instead of nothing and stay organized
-write stuff down
Random stuff to check when wa:
-if code is way too long/cancer then reassess
-switched N/M
-int overflow
-switched variables
-wrong MOD
-hardcoded edge case incorrectly
Random stuff to check when tle:
-continue instead of break
-condition in for/while loop bad
Random stuff to check when rte:
-switched N/M
-long to int/int overflow
-division by 0
-edge case for empty list/data structure/N=1
*/
public static boolean isPrime(long n)
{
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
public static long gcd(long a, long b)
{
if(a > b)
a = (a+b)-(b=a);
if(a == 0L)
return b;
return gcd(b%a, a);
}
public static long totient(long n)
{
long result = n;
for (int p = 2; p*p <= n; ++p)
if (n % p == 0)
{
while(n%p == 0)
n /= p;
result -= result/p;
}
if (n > 1)
result -= result/n;
return result;
/*
find phi(i) from 1 to N fast
O(N*loglogN)
long[] arr = new long[N+1];
for(int i=1; i <= N; i++)
arr[i] = i;
for(int v=2; v <= N; v++)
if(arr[v] == v)
for(int a=v; a <= N; a+=v)
arr[a] -= arr[a]/v;
*/
}
public static ArrayList<Integer> findDiv(int N)
{
//gens all divisors of N
ArrayList<Integer> ls1 = new ArrayList<Integer>();
ArrayList<Integer> ls2 = new ArrayList<Integer>();
for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++)
if(N%i == 0)
{
ls1.add(i);
ls2.add(N/i);
}
Collections.reverse(ls2);
for(int b: ls2)
if(b != ls1.get(ls1.size()-1))
ls1.add(b);
return ls1;
}
public static void sort(int[] arr)
{
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long power(long x, long y, long p)
{
//0^0 = 1
long res = 1L;
x = x%p;
while(y > 0)
{
if((y&1)==1)
res = (res*x)%p;
y >>= 1;
x = (x*x)%p;
}
return res;
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v)
{
//map[k] += v;
if(!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k)+v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v)
{
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if(lol == v)
map.remove(k);
else
map.put(k, lol-v);
}
public static int[] compress(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for(int x: ls)
if(!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for(int i=0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
public static long[][] multiply(long[][] left, long[][] right)
{
long MOD = 1000000007L;
int N = left.length;
int M = right[0].length;
long[][] res = new long[N][M];
for(int a=0; a < N; a++)
for(int b=0; b < M; b++)
for(int c=0; c < left[0].length; c++)
{
res[a][b] += (left[a][c]*right[c][b])%MOD;
if(res[a][b] >= MOD)
res[a][b] -= MOD;
}
return res;
}
public static long[][] power(long[][] grid, long pow)
{
long[][] res = new long[grid.length][grid[0].length];
for(int i=0; i < res.length; i++)
res[i][i] = 1L;
long[][] curr = grid.clone();
while(pow > 0)
{
if((pow&1L) == 1L)
res = multiply(curr, res);
pow >>= 1;
curr = multiply(curr, curr);
}
return res;
}
/*
* Data Structures
*/
class DSU
{
public int[] dsu;
public int[] size;
public DSU(int N)
{
dsu = new int[N+1];
size = new int[N+1];
for(int i=0; i <= N; i++)
{
dsu[i] = i;
size[i] = 1;
}
}
//with path compression, no find by rank
public int find(int x)
{
return dsu[x] == x ? x : (dsu[x] = find(dsu[x]));
}
public void merge(int x, int y)
{
int fx = find(x);
int fy = find(y);
dsu[fx] = fy;
}
public void merge(int x, int y, boolean sized)
{
int fx = find(x);
int fy = find(y);
size[fy] += size[fx];
dsu[fx] = fy;
}
}
class FenwickTree
{
//Binary Indexed Tree
//1 indexed
public int[] tree;
public int size;
public FenwickTree(int size)
{
this.size = size;
tree = new int[size+5];
}
public void add(int i, int v)
{
while(i <= size)
{
tree[i] += v;
i += i&-i;
}
}
public int find(int i)
{
int res = 0;
while(i >= 1)
{
res += tree[i];
i -= i&-i;
}
return res;
}
public int find(int l, int r)
{
return find(r)-find(l-1);
}
}
class SegmentTree
{
//Tlatoani's segment tree
//iterative implementation = low constant runtime factor
//range query, non lazy
final int[] val;
final int treeFrom;
final int length;
public SegmentTree(int treeFrom, int treeTo)
{
this.treeFrom = treeFrom;
int length = treeTo - treeFrom + 1;
int l;
for (l = 0; (1 << l) < length; l++);
val = new int[1 << (l + 1)];
this.length = 1 << l;
}
public void update(int index, int delta)
{
//replaces value
int node = index - treeFrom + length;
val[node] = delta;
for (node >>= 1; node > 0; node >>= 1)
val[node] = comb(val[node << 1], val[(node << 1) + 1]);
}
public int query(int from, int to)
{
//inclusive bounds
if (to < from)
return 0; //0 or 1?
from += length - treeFrom;
to += length - treeFrom + 1;
//0 or 1?
int res = 0;
for (; from + (from & -from) <= to; from += from & -from)
res = comb(res, val[from / (from & -from)]);
for (; to - (to & -to) >= from; to -= to & -to)
res = comb(res, val[(to - (to & -to)) / (to & -to)]);
return res;
}
public int comb(int a, int b)
{
//change this
return Math.max(a,b);
}
}
class LazySegTree
{
//definitions
private int NULL = -1;
private int[] tree;
private int[] lazy;
private int length;
public LazySegTree(int N)
{
length = N; int b;
for(b=0; (1<<b) < length; b++);
tree = new int[1<<(b+1)];
lazy = new int[1<<(b+1)];
}
public int query(int left, int right)
{
//left and right are 0-indexed
return get(1, 0, length-1, left, right);
}
private int get(int v, int currL, int currR, int L, int R)
{
if(L > R)
return NULL;
if(L <= currL && currR <= R)
return tree[v];
propagate(v);
int mid = (currL+currR)/2;
return comb(get(v*2, currL, mid, L, Math.min(R, mid)),
get(v*2+1, mid+1, currR, Math.max(L, mid+1), R));
}
public void update(int left, int right, int delta)
{
add(1, 0, length-1, left, right, delta);
}
private void add(int v, int currL, int currR, int L, int R, int delta)
{
if(L > R)
return;
if(currL == L && currR == R)
{
//exact covering
tree[v] += delta;
lazy[v] += delta;
return;
}
propagate(v);
int mid = (currL+currR)/2;
add(v*2, currL, mid, L, Math.min(R, mid), delta);
add(v*2+1, mid+1, currR, Math.max(L, mid+1), R, delta);
tree[v] = comb(tree[v*2], tree[v*2+1]);
}
private void propagate(int v)
{
//tree[v] already has lazy[v]
if(lazy[v] == 0)
return;
tree[v*2] += lazy[v];
lazy[v*2] += lazy[v];
tree[v*2+1] += lazy[v];
lazy[v*2+1] += lazy[v];
lazy[v] = 0;
}
private int comb(int a, int b)
{
return Math.max(a,b);
}
}
class RangeBit
{
//FenwickTree and RangeBit are faster than LazySegTree by constant factor
final int[] value;
final int[] weightedVal;
public RangeBit(int treeTo)
{
value = new int[treeTo+2];
weightedVal = new int[treeTo+2];
}
private void updateHelper(int index, int delta)
{
int weightedDelta = index*delta;
for(int j = index; j < value.length; j += j & -j)
{
value[j] += delta;
weightedVal[j] += weightedDelta;
}
}
public void update(int from, int to, int delta)
{
updateHelper(from, delta);
updateHelper(to + 1, -delta);
}
private int query(int to)
{
int res = 0;
int weightedRes = 0;
for (int j = to; j > 0; j -= j & -j)
{
res += value[j];
weightedRes += weightedVal[j];
}
return ((to + 1)*res)-weightedRes;
}
public int query(int from, int to)
{
if (to < from)
return 0;
return query(to) - query(from - 1);
}
}
class SparseTable
{
public int[] log;
public int[][] table;
public int N; public int K;
public SparseTable(int N)
{
this.N = N;
log = new int[N+2];
K = Integer.numberOfTrailingZeros(Integer.highestOneBit(N));
table = new int[N][K+1];
sparsywarsy();
}
private void sparsywarsy()
{
log[1] = 0;
for(int i=2; i <= N+1; i++)
log[i] = log[i/2]+1;
}
public void lift(int[] arr)
{
int n = arr.length;
for(int i=0; i < n; i++)
table[i][0] = arr[i];
for(int j=1; j <= K; j++)
for(int i=0; i + (1 << j) <= n; i++)
table[i][j] = Math.min(table[i][j-1], table[i+(1 << (j - 1))][j-1]);
}
public int query(int L, int R)
{
//inclusive, 1 indexed
L--; R--;
int mexico = log[R-L+1];
return Math.min(table[L][mexico], table[R-(1 << mexico)+1][mexico]);
}
}
class LCA
{
public int N, root;
public ArrayDeque<Integer>[] edges;
private int[] enter;
private int[] exit;
private int LOG = 17; //change this
private int[][] dp;
public LCA(int n, ArrayDeque<Integer>[] edges, int r)
{
N = n; root = r;
enter = new int[N+1];
exit = new int[N+1];
dp = new int[N+1][LOG];
this.edges = edges;
int[] time = new int[1];
//change to iterative dfs if N is large
dfs(root, 0, time);
dp[root][0] = 1;
for(int b=1; b < LOG; b++)
for(int v=1; v <= N; v++)
dp[v][b] = dp[dp[v][b-1]][b-1];
}
private void dfs(int curr, int par, int[] time)
{
dp[curr][0] = par;
enter[curr] = ++time[0];
for(int next: edges[curr])
if(next != par)
dfs(next, curr, time);
exit[curr] = ++time[0];
}
public int lca(int x, int y)
{
if(isAnc(x, y))
return x;
if(isAnc(y, x))
return y;
int curr = x;
for(int b=LOG-1; b >= 0; b--)
{
int temp = dp[curr][b];
if(!isAnc(temp, y))
curr = temp;
}
return dp[curr][0];
}
private boolean isAnc(int anc, int curr)
{
return enter[anc] <= enter[curr] && exit[anc] >= exit[curr];
}
}
class BitSet
{
private int CONS = 62; //safe
public long[] sets;
public int size;
public BitSet(int N)
{
size = N;
if(N%CONS == 0)
sets = new long[N/CONS];
else
sets = new long[N/CONS+1];
}
public void add(int i)
{
int dex = i/CONS;
int thing = i%CONS;
sets[dex] |= (1L << thing);
}
public int and(BitSet oth)
{
int boof = Math.min(sets.length, oth.sets.length);
int res = 0;
for(int i=0; i < boof; i++)
res += Long.bitCount(sets[i] & oth.sets[i]);
return res;
}
public int xor(BitSet oth)
{
int boof = Math.min(sets.length, oth.sets.length);
int res = 0;
for(int i=0; i < boof; i++)
res += Long.bitCount(sets[i] ^ oth.sets[i]);
return res;
}
}
class MaxFlow
{
//Dinic with optimizations (see magic array in dfs function)
public int N, source, sink;
public ArrayList<Edge>[] edges;
private int[] depth;
public MaxFlow(int n, int x, int y)
{
N = n;
source = x;
sink = y;
edges = new ArrayList[N+1];
for(int i=0; i <= N; i++)
edges[i] = new ArrayList<Edge>();
depth = new int[N+1];
}
public void addEdge(int from, int to, long cap)
{
Edge forward = new Edge(from, to, cap);
Edge backward = new Edge(to, from, 0L);
forward.residual = backward;
backward.residual = forward;
edges[from].add(forward);
edges[to].add(backward);
}
public long mfmc()
{
long res = 0L;
int[] magic = new int[N+1];
while(assignDepths())
{
long flow = dfs(source, Long.MAX_VALUE/2, magic);
while(flow > 0)
{
res += flow;
flow = dfs(source, Long.MAX_VALUE/2, magic);
}
magic = new int[N+1];
}
return res;
}
private boolean assignDepths()
{
Arrays.fill(depth, -69);
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
q.add(source);
depth[source] = 0;
while(q.size() > 0)
{
int curr = q.poll();
for(Edge e: edges[curr])
if(e.capacityLeft() > 0 && depth[e.to] == -69)
{
depth[e.to] = depth[curr]+1;
q.add(e.to);
}
}
return depth[sink] != -69;
}
private long dfs(int curr, long bottleneck, int[] magic)
{
if(curr == sink)
return bottleneck;
for(; magic[curr] < edges[curr].size(); magic[curr]++)
{
Edge e = edges[curr].get(magic[curr]);
if(e.capacityLeft() > 0 && depth[e.to]-depth[curr] == 1)
{
long val = dfs(e.to, Math.min(bottleneck, e.capacityLeft()), magic);
if(val > 0)
{
e.augment(val);
return val;
}
}
}
return 0L; //no flow
}
private class Edge
{
public int from, to;
public long flow, capacity;
public Edge residual;
public Edge(int f, int t, long cap)
{
from = f;
to = t;
capacity = cap;
}
public long capacityLeft()
{
return capacity-flow;
}
public void augment(long val)
{
flow += val;
residual.flow -= val;
}
}
}
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | d3dd4671f799044dcd58e05ca2a4310f | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
/**
* <a href = "https://codeforces.com/contest/1672/problem/B"> Link </a>
* @author Bris
* @version 1.0
* @since 9:39:14 PM - Apr 23, 2022
*/
public class B1672 {
/**
* .
* @param s .
* @return .
*/
public static boolean isGood(String s) {
if (s.length() < 2) {
return false;
}
if (s.length() == 2) {
return s.equals("AB");
}
int countA = 0;
int countB = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
countA++;
}else {
countB++;
}
if (countA < countB) {
return false;
}
}
return (countA >= countB) && (s.charAt(s.length() - 1) == 'B') && (s.charAt(0) != 'B');
}
/**
* .
* @param args .
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-->0) {
String s2 = scanner.next();
System.out.println((isGood(s2))? "YES" : "NO");
}
scanner.close();
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c283a229be9bd558d05a6ab2be705778 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class B_I_love_AAAB {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
int a = 0, b = 0;
boolean f = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A')
a++;
else {
if (a == 0) {
f = false;
break;
}
b++;
a--;
}
}
if (s.charAt(s.length() - 1) != 'B') {
System.out.println("NO");
} else if (b == 0 || !f) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | adb1b8d4ee23d293bfeafc7e859d4e9e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
new Solution().function();
}
}
class Solution {
private Scanner sc = new Scanner(System.in);
public void function() {
int T = sc.nextInt();
while (T-- > 0) {
String s = sc.next();
char[] c = s.toCharArray();
int len = c.length;
int tag = 0;
boolean res = true;
for (int i = 0; i < len; i++) {
if (c[i] == 'B') {
tag = 1;
}
}
if (tag == 0 || c[0] == 'B' || c[len - 1] != 'B') {
sout("NO");
continue;
}
int i = 0;
int j = 0;
int lenA = 0;
int lenB = 0;
while (i < len) {
while (j < len && c[j] == c[i]) {
j++;
}
lenA += j - i;
int u = j;
while (u < len && c[u] == c[j]) {
u++;
}
lenB += u - j;
if (lenB > lenA) {
res = false;
break;
}
i = u;
j = i;
}
sout(res ? "YES" : "NO");
}
}
private void sout(Object o) {
System.out.println(o);
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 2d2d335f233b0586dc0510cda15e51ad | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.awt.datatransfer.StringSelection;
import java.util.*;
import javax.management.Query;
import java.io.*;
public class practice {
static String s;
static HashMap<Long,Integer>hm;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
String s= sc.next();
Stack<Integer>st=new Stack<>();
boolean f=true;
for (int i = 0; i <s.length(); i++) {
if(s.charAt(i)=='A')
st.push(0);
else{
if(st.isEmpty()){
f=false;
break;
}
else{
st.pop();
}
}
}
f=f&s.charAt(s.length()-1)=='B';
pw.println(f?"Yes":"No");
}
pw.close();
}
public static int next(long[] arr, int target,int days)
{
// pw.println("days="+days);
int start = 0, end = arr.length-1;
// Minimum size of the array should be 1
// If target lies beyond the max element, than the index of strictly smaller
// value than target should be (end - 1)
if (target >= 0l+arr[end]+1l*(end+1)*days) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to the left side if the target is smaller
if (0l+arr[mid]+1l*(mid+1)*days > target) {
end = mid - 1;
}
// Move right side
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
public static long factorial(int n){
int y=1;
for (int i = 2; i <=n ; i++) {
y*=i;
}
return y;
}
public static void sort(int[] in) {
shuffle(in);
Arrays.sort(in);
}
public static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
int tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
static LinkedList getfact(int n){
LinkedList<Integer>ll=new LinkedList<>();
LinkedList<Integer>ll2=new LinkedList<>();
for (int i = 1; i <= Math.sqrt(n); i++) {
if(n%i==0) {
ll.add(i);
if(i!=n/i)
ll2.addLast(n/i);
}
}
while (!ll2.isEmpty()){
ll.add(ll2.removeLast());
}
return ll;
}
static void rev(int n){
String s1=s.substring(0,n);
s=s.substring(n);
for (int i = 0; i <n ; i++) {
s=s1.charAt(i)+s;
}
}
static class SegmentTree { // 1-based DS, OOP
int N; //the number of elements in the array as a power of 2 (i.e. after padding)
long[] array, sTree;
Long[]lazy;
SegmentTree(long[] in)
{
array = in; N = in.length - 1;
sTree = new long[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new Long[N<<1];
build(1,1,N);
}
void build(int node, int b, int e) // O(n)
{
if(b == e)
sTree[node] = array[b];
else
{
int mid = b + e >> 1;
build(node<<1,b,mid);
build(node<<1|1,mid+1,e);
sTree[node] = sTree[node<<1]+sTree[node<<1|1];
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] += val;
while(index>1)
{
index >>= 1;
sTree[index] = sTree[index<<1] + sTree[index<<1|1];
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1,1,N,i,j,val);
}
void update_range(int node, int b, int e, int i, int j, int val)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node] = (e-b+1)*val;
lazy[node] = val*1l;
}
else
{
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node<<1,b,mid,i,j,val);
update_range(node<<1|1,mid+1,e,i,j,val);
sTree[node] = sTree[node<<1] + sTree[node<<1|1];
}
}
void propagate(int node, int b, int mid, int e)
{
if(lazy[node]!=null) {
lazy[node << 1] = lazy[node];
lazy[node << 1 | 1] = lazy[node];
sTree[node << 1] = (mid - b + 1) * lazy[node];
sTree[node << 1 | 1] = (e - mid) * lazy[node];
}
lazy[node] = null;
}
long query(int i, int j)
{
return query(1,1,N,i,j);
}
long query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return 0;
if(b>= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
long q1 = query(node<<1,b,mid,i,j);
long q2 = query(node<<1|1,mid+1,e,i,j);
return q1 + q2;
}
}
// public static long dp(int idx) {
// if (idx >= n)
// return Long.MAX_VALUE/2;
// return Math.min(dp(idx+1),memo[idx]+dp(idx+k));
// }
// if(num==k)
// return dp(0,idx+1);
// if(memo[num][idx]!=-1)
// return memo[num][idx];
// long ret=0;
// if(num==0) {
// if(s.charAt(idx)=='a')
// ret= dp(1,idx+1);
// else if(s.charAt(idx)=='?') {
// ret=Math.max(1+dp(1,idx+1),dp(0,idx+1) );
// }
// }
// else {
// if(num%2==0) {
// if(s.charAt(idx)=='a')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// else {
// if(s.charAt(idx)=='b')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// }
// }
static void putorrem(long x){
if(hm.getOrDefault(x,0)==1){
hm.remove(x);
}
else
hm.put(x,hm.getOrDefault(x,0)-1);
}
public static int max4(int a,int b, int c,int d) {
int [] s= {a,b,c,d};
Arrays.sort(s);
return s[3];
}
public static double logbase2(int k) {
return( (Math.log(k)+0.0)/Math.log(2));
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
public tuble add(tuble t){
return new tuble(this.x+t.x,this.y+t.y,this.z+t.z);
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | ea500f4bf9f9c5f6c7a615747b56a5b6 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[]args) throws Exception{
Scanner sc = new Scanner(System.in) ;
PrintWriter pw = new PrintWriter(System.out) ;
int t=sc.nextInt();
while(t-->0){
String s = sc.nextLine() ;
boolean res = true ;
if(!s.contains("A") || !s.contains("B")){
res = false ;
}
else{
Stack<Character> st = new Stack<Character>() ;
if(s.charAt(0)=='B' || s.charAt(s.length()-1)=='A'){
res = false ;
}
else{
st.push(s.charAt(0)) ;
}
for(int i=1 ; i<s.length() && res ;i++){
if(s.charAt(i)=='B'&& st.isEmpty()){
res = false ;
break ;
}
else{
if(s.charAt(i)=='B'){
st.pop();
}
else{
st.push(s.charAt(i)) ;
}
}
}
/*if(res && !st.isEmpty()){
res = false ;
}*/
}
String resu = (res)? "YES" : "NO" ;
pw.println(resu) ;
}
pw.flush();
pw.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {return br.ready();}
}
static class Pair {
long x ;
long y ;
public Pair(long x, long y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (x ^ (x >>> 32));
result = prime * result + (int) (y ^ (y >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public String toString() {
return x + " " + y;
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 8c5dde82b6b4679e5a80cb227ead9774 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class c731{
public static void main(String[] args) throws IOException{
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter pt = new PrintWriter(System.out);
FastReader sc = new FastReader();
int t = sc.nextInt();
for(int o = 0; o<t;o++) {
//int n = sc.nextInt();
String s = sc.next();
if(s.length()<2) {
System.out.println("NO");
continue;
}
int c1 =0;
int c2 = 0;
int f = 0;
for(int i = 0 ; i<s.length();i++) {
if(s.charAt(i) == 'A') {
c1++;
}else {
c2++;
}
if(c2>c1) {
f = 1;
break;
}
}
if(s.charAt(s.length()-1) == 'A') {
System.out.println("NO");
continue;
}
if(c2 == 0) {
System.out.println("NO");
continue;
}
if(f == 0) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
public static boolean check(int[] arr , int n , int v , int l ) {
int x = v/2;
int y = v/2;
// System.out.println(x + " " + y);
if(v%2 == 1 ) {
x++;
}
for(int i = 0 ; i<n;i++) {
int d = l - arr[i];
int c = Math.min(d/2, y);
y -= c;
arr[i] -= c*2;
if(arr[i] > x) {
return false;
}
x -= arr[i];
}
return true;
}
public static int cnt_set(long x) {
long v = 1l;
int c =0;
int f = 0;
while(v<=x) {
if((v&x)!=0) {
c++;
}
v = v<<1;
}
return c;
}
public static int lis(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
dp[0]= 1;
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>x) {
al.add(arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), arr[i]);
// System.out.println(v);
al.set(v, arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
public static int lis2(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(-arr[n-1]);
dp[n-1] = 1;
// System.out.println(al);
for(int i = n-2 ; i>=0;i--) {
int x = al.get(al.size()-1);
// System.out.println(-arr[i] + " " + i + " " + x);
if((-arr[i])>x) {
al.add(-arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), -arr[i]);
// System.out.println(v);
al.set(v, -arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
static int cntDivisors(int n){
int cnt = 0;
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
cnt++;
else
cnt+=2;
}
}
return cnt;
}
public static long power(long x, long y, long p){
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0){
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ncr(long[] fac, int n , int r , long m) {
if(r>n) {
return 0;
}
return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m;
}
public static int lower_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
public static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public static long modInverse(long a, long m){
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
//_________________________________________________________________________________________________________________________________________________________________
// private static int[] parent;
// private static int[] size;
public static int find(int[] parent, int u) {
while(u != parent[u]) {
parent[u] = parent[parent[u]];
u = parent[u];
}
return u;
}
private static void union(int[] parent,int[] size,int u, int v) {
int rootU = find(parent,u);
int rootV = find(parent,v);
if(rootU == rootV) {
return;
}
if(size[rootU] < size[rootV]) {
parent[rootU] = rootV;
size[rootV] += size[rootU];
} else {
parent[rootV] = rootU;
size[rootU] += size[rootV];
}
}
//-----------------------------------------------------------------------------------------------------------------------------------
//segment tree
//for finding minimum in range
public static void build(int [] seg,int []arr,int idx, int lo , int hi) {
if(lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi)/2;
build(seg,arr,2*idx+1, lo, mid);
build(seg,arr,idx*2+2, mid +1, hi);
// seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
// seg[idx] = seg[idx*2+1]+ seg[idx*2+2];
// seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]);
seg[idx] = seg[idx*2+1] * seg[idx*2+2];
}
//for finding minimum in range
public static int query(int[]seg,int idx , int lo , int hi , int l , int r) {
if(lo>=l && hi<=r) {
return seg[idx];
}
if(hi<l || lo>r) {
return 1;
}
int mid = (lo + hi)/2;
int left = query(seg,idx*2 +1, lo, mid, l, r);
int right = query(seg,idx*2 + 2, mid + 1, hi, l, r);
// return Math.min(left, right);
//return gcd(left, right);
// return Math.min(left, right);
return left * right;
}
// // for sum
//
//public static void update(int[]seg,int idx, int lo , int hi , int node , int val) {
// if(lo == hi) {
// seg[idx] += val;
// }else {
//int mid = (lo + hi )/2;
//if(node<=mid && node>=lo) {
// update(seg, idx * 2 +1, lo, mid, node, val);
//}else {
// update(seg, idx*2 + 2, mid + 1, hi, node, val);
//}
//seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2];
//
//}
//}
//---------------------------------------------------------------------------------------------------------------------------------------
//
static void shuffleArray(int[] ar)
{
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
// static void shuffleArray(coup[] ar)
// {
// // If running on Java 6 or older, use `new Random()` on RHS here
// Random rnd = ThreadLocalRandom.current();
// for (int i = ar.length - 1; i > 0; i--)
// {
// int index = rnd.nextInt(i + 1);
// // Simple swap
// coup a = ar[index];
// ar[index] = ar[i];
// ar[i] = a;
// }
// }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
class coup{
int a;
int b;
public coup(int a , int b) {
this.a = a;
this.b = b;
}
}
class tripp{
int a;
int b;
double c;
public tripp(int a , int b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 700091759931c210fe2831caaf1df3da | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
//import org.graalvm.compiler.core.common.Fields.ObjectTransformer;
import java.math.*;
import java.math.BigInteger;
public final class A
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
// static int g[][];
static ArrayList<Integer> g[];
static long mod=(long)998244353,INF=Long.MAX_VALUE;
static boolean set[];
static int max=0;
static int lca[][];
static int par[],col[],D[];
static long fact[];
static int size[],N;
static long dp[][],sum[][],f[];
static int seg[];
static ArrayList<Integer> A;
public static void main(String args[])throws IOException
{
/*
* star,rope,TPST
* BS,LST,MS,MQ
*/
int T=i();
outer:while(T-->0)
{
char X[]=in.next().toCharArray();
int N=X.length;
if(N==1)ans.append("NO\n");
else if(X[0]=='B' || X[N-1]=='A')ans.append("NO\n");
else
{
int a=0,b=0;
for(int i=0; i<N; i++)
{
if(X[i]=='B')
{
b++;
}
else a++;
if(b>a)
{
ans.append("NO\n");
continue outer;
}
}
ans.append("YES\n");
}
}
out.print(ans);
out.close();
}
static int f(int A[])
{
int s=0;
for(int i=1; i<4; i++)
{
s+=Math.abs(A[i]-A[i-1]);
}
return s;
}
static boolean f(int A[],int B[],int N)
{
for(int i=0; i<N; i++)
{
if(Math.abs(A[i]-B[i])>1)return false;
}
return true;
}
static void dfS(int n,int p)
{
int child=0;
for(int c:g[n])
{
if(c!=p)
{
child++;
dfS(c,n);
}
}
if(child!=0)A.add(child);
}
static int [] prefix(char s[],int N) {
// int n = (int)s.length();
// vector<int> pi(n);
N=s.length;
int pi[]=new int[N];
for (int i = 1; i < N; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static int count(long N)
{
int cnt=0;
long p=1L;
while(p<=N)
{
if((p&N)!=0)cnt++;
p<<=1;
}
return cnt;
}
static long kadane(long A[])
{
long lsum=A[0],gsum=0;
gsum=Math.max(gsum, lsum);
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
public static boolean pal(int i)
{
StringBuilder sb=new StringBuilder();
StringBuilder rev=new StringBuilder();
int p=1;
while(p<=i)
{
if((i&p)!=0)
{
sb.append("1");
}
else sb.append("0");
p<<=1;
}
rev=new StringBuilder(sb.toString());
rev.reverse();
if(i==8)System.out.println(sb+" "+rev);
return (sb.toString()).equals(rev.toString());
}
public static void reverse(int i,int j,int A[])
{
while(i<j)
{
int t=A[i];
A[i]=A[j];
A[j]=t;
i++;
j--;
}
}
public static int ask(int a,int b,int c)
{
System.out.println("? "+a+" "+b+" "+c);
return i();
}
static int[] reverse(int A[],int N)
{
int B[]=new int[N];
for(int i=N-1; i>=0; i--)
{
B[N-i-1]=A[i];
}
return B;
}
static boolean isPalin(char X[])
{
int i=0,j=X.length-1;
while(i<=j)
{
if(X[i]!=X[j])return false;
i++;
j--;
}
return true;
}
static int distance(int a,int b)
{
int d=D[a]+D[b];
int l=LCA(a,b);
l=2*D[l];
return d-l;
}
static int LCA(int a,int b)
{
if(D[a]<D[b])
{
int t=a;
a=b;
b=t;
}
int d=D[a]-D[b];
int p=1;
for(int i=0; i>=0 && p<=d; i++)
{
if((p&d)!=0)
{
a=lca[a][i];
}
p<<=1;
}
if(a==b)return a;
for(int i=max-1; i>=0; i--)
{
if(lca[a][i]!=-1 && lca[a][i]!=lca[b][i])
{
a=lca[a][i];
b=lca[b][i];
}
}
return lca[a][0];
}
static void dfs(int n,int p)
{
lca[n][0]=p;
if(p!=-1)D[n]=D[p]+1;
for(int c:g[n])
{
if(c!=p)
{
dfs(c,n);
}
}
}
static int[] prefix_function(char X[])//returns pi(i) array
{
int N=X.length;
int pre[]=new int[N];
for(int i=1; i<N; i++)
{
int j=pre[i-1];
while(j>0 && X[i]!=X[j])
j=pre[j-1];
if(X[i]==X[j])j++;
pre[i]=j;
}
return pre;
}
static TreeNode start;
public static void f(TreeNode root,TreeNode p,int r)
{
if(root==null)return;
if(p!=null)
{
root.par=p;
}
if(root.val==r)start=root;
f(root.left,root,r);
f(root.right,root,r);
}
static int right(int A[],int Limit,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<Limit)l=m;
else r=m;
}
return l;
}
static int left(int A[],int a,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<a)l=m;
else r=m;
}
return l;
}
static void build(int v,int tl,int tr,int A[])
{
if(tl==tr)
{
seg[v]=A[tl];
return;
}
int tm=(tl+tr)/2;
build(v*2,tl,tm,A);
build(v*2+1,tm+1,tr,A);
seg[v]=seg[v*2]+seg[v*2+1];
}
static void update(int v,int tl,int tr,int index)
{
if(index==tl && index==tr)
{
seg[v]--;
}
else
{
int tm=(tl+tr)/2;
if(index<=tm)update(v*2,tl,tm,index);
else update(v*2+1,tm+1,tr,index);
seg[v]=seg[v*2]+seg[v*2+1];
}
}
static int ask(int v,int tl,int tr,int l,int r)
{
if(l>r)return 0;
if(tl==l && r==tr)
{
return seg[v];
}
int tm=(tl+tr)/2;
return ask(v*2,tl,tm,l,Math.min(tm, r))+ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r);
}
static boolean f(long A[],long m,int N)
{
long B[]=new long[N];
for(int i=0; i<N; i++)
{
B[i]=A[i];
}
for(int i=N-1; i>=0; i--)
{
if(B[i]<m)return false;
if(i>=2)
{
long extra=Math.min(B[i]-m, A[i]);
long x=extra/3L;
B[i-2]+=2L*x;
B[i-1]+=x;
}
}
return true;
}
static int f(int l,int r,long A[],long x)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]>=x)l=m;
else r=m;
}
return r;
}
static boolean f(long m,long H,long A[],int N)
{
long s=m;
for(int i=0; i<N-1;i++)
{
s+=Math.min(m, A[i+1]-A[i]);
}
return s>=H;
}
static long ask(long l,long r)
{
System.out.println("? "+l+" "+r);
return l();
}
static long f(long N,long M)
{
long s=0;
if(N%3==0)
{
N/=3;
s=N*M;
}
else
{
long b=N%3;
N/=3;
N++;
s=N*M;
N--;
long a=N*M;
if(M%3==0)
{
M/=3;
a+=(b*M);
}
else
{
M/=3;
M++;
a+=(b*M);
}
s=Math.min(s, a);
}
return s;
}
static int ask(StringBuilder sb,int a)
{
System.out.println(sb+""+a);
return i();
}
static void swap(char X[],int i,int j)
{
char x=X[i];
X[i]=X[j];
X[j]=x;
}
static int min(int a,int b,int c)
{
return Math.min(Math.min(a, b), c);
}
static long and(int i,int j)
{
System.out.println("and "+i+" "+j);
return l();
}
static long or(int i,int j)
{
System.out.println("or "+i+" "+j);
return l();
}
static int len=0,number=0;
static void f(char X[],int i,int num,int l)
{
if(i==X.length)
{
if(num==0)return;
//update our num
if(isPrime(num))return;
if(l<len)
{
len=l;
number=num;
}
return;
}
int a=X[i]-'0';
f(X,i+1,num*10+a,l+1);
f(X,i+1,num,l);
}
static boolean is_Sorted(int A[])
{
int N=A.length;
for(int i=1; i<=N; i++)if(A[i-1]!=i)return false;
return true;
}
static boolean f(StringBuilder sb,String Y,String order)
{
StringBuilder res=new StringBuilder(sb.toString());
HashSet<Character> set=new HashSet<>();
for(char ch:order.toCharArray())
{
set.add(ch);
for(int i=0; i<sb.length(); i++)
{
char x=sb.charAt(i);
if(set.contains(x))continue;
res.append(x);
}
}
String str=res.toString();
return str.equals(Y);
}
static boolean all_Zero(int f[])
{
for(int a:f)if(a!=0)return false;
return true;
}
static long form(int a,int l)
{
long x=0;
while(l-->0)
{
x*=10;
x+=a;
}
return x;
}
static int count(String X)
{
HashSet<Integer> set=new HashSet<>();
for(char x:X.toCharArray())set.add(x-'0');
return set.size();
}
static int f(long K)
{
long l=0,r=K;
while(r-l>1)
{
long m=(l+r)/2;
if(m*m<K)l=m;
else r=m;
}
return (int)l;
}
// static void build(int v,int tl,int tr,long A[])
// {
// if(tl==tr)
// {
// seg[v]=A[tl];
// }
// else
// {
// int tm=(tl+tr)/2;
// build(v*2,tl,tm,A);
// build(v*2+1,tm+1,tr,A);
// seg[v]=Math.min(seg[v*2], seg[v*2+1]);
// }
// }
static int [] sub(int A[],int B[])
{
int N=A.length;
int f[]=new int[N];
for(int i=N-1; i>=0; i--)
{
if(B[i]<A[i])
{
B[i]+=26;
B[i-1]-=1;
}
f[i]=B[i]-A[i];
}
for(int i=0; i<N; i++)
{
if(f[i]%2!=0)f[i+1]+=26;
f[i]/=2;
}
return f;
}
static int[] f(int N)
{
char X[]=in.next().toCharArray();
int A[]=new int[N];
for(int i=0; i<N; i++)A[i]=X[i]-'a';
return A;
}
static int max(int a ,int b,int c,int d)
{
a=Math.max(a, b);
c=Math.max(c,d);
return Math.max(a, c);
}
static int min(int a ,int b,int c,int d)
{
a=Math.min(a, b);
c=Math.min(c,d);
return Math.min(a, c);
}
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static long mul(long a, long b)
{
return ( a %mod * 1L * b%mod )%mod;
}
static void swap(int A[],int a,int b)
{
int t=A[a];
A[a]=A[b];
A[b]=t;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
if(par[a]>par[b]) //this means size of a is less than that of b
{
int t=b;
b=a;
a=t;
}
par[a]+=par[b];
par[b]=a;
}
}
static boolean isSorted(int A[])
{
for(int i=1; i<A.length; i++)
{
if(A[i]<A[i-1])return false;
}
return true;
}
static boolean isDivisible(StringBuilder X,int i,long num)
{
long r=0;
for(; i<X.length(); i++)
{
r=r*10+(X.charAt(i)-'0');
r=r%num;
}
return r==0;
}
static int lower_Bound(int A[],int low,int high, int x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static String f(String A)
{
String X="";
for(int i=A.length()-1; i>=0; i--)
{
int c=A.charAt(i)-'0';
X+=(c+1)%2;
}
return X;
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static int sD(long n)
{
if (n % 2 == 0 )
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0 )
return i;
}
return (int)n;
}
// static void setGraph(int N,int nodes)
// {
//// size=new int[N+1];
// par=new int[N+1];
// col=new int[N+1];
//// g=new int[N+1][];
// D=new int[N+1];
// int deg[]=new int[N+1];
// int A[][]=new int[nodes][2];
// for(int i=0; i<nodes; i++)
// {
// int a=i(),b=i();
// A[i][0]=a;
// A[i][1]=b;
// deg[a]++;
// deg[b]++;
// }
// for(int i=0; i<=N; i++)
// {
// g[i]=new int[deg[i]];
// deg[i]=0;
// }
// for(int a[]:A)
// {
// int x=a[0],y=a[1];
// g[x][deg[x]++]=y;
// g[y][deg[y]++]=x;
// }
// }
static long pow(long a,long b)
{
//long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(boolean A[][])
{
for(boolean a[]:A)print(a);
}
static void print(long A[][])
{
for(long a[]:A)print(a);
}
static void print(int A[][])
{
for(int a[]:A)print(a);
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
class segNode
{
long pref,suff,sum,max;
segNode(long a,long b,long c,long d)
{
pref=a;
suff=b;
sum=c;
max=d;
}
}
//class TreeNode
//{
// int cnt,index;
// TreeNode left,right;
// TreeNode(int c)
// {
// cnt=c;
// index=-1;
// }
// TreeNode(int c,int index)
// {
// cnt=c;
// this.index=index;
// }
//}
class role
{
String skill;
int level;
role(String s,int l)
{
skill=s;
level=l;
}
}
class project implements Comparable<project>
{
int score,index;
project(int s,int i)
{
// roles=r;
index=i;
score=s;
// skill=new String[r];
// lvl=new int[r];
}
public int compareTo(project x)
{
return x.score-this.score;
}
}
class post implements Comparable<post>
{
long x,y,d,t;
post(long a,long b,long c)
{
x=a;
y=b;
d=c;
}
public int compareTo(post X)
{
if(X.t==this.t)
{
return 0;
}
else
{
long xt=this.t-X.t;
if(xt>0)return 1;
return -1;
}
}
}
class TreeNode
{
int val;
TreeNode left, right,par;
TreeNode() {}
TreeNode(int item)
{
val = item;
left =null;
right = null;
par=null;
}
}
class edge
{
int a,wt;
edge(int a,int w)
{
this.a=a;
wt=w;
}
}
class pair3 implements Comparable<pair3>
{
long a;
int index;
pair3(long x,int i)
{
a=x;
index=i;
}
public int compareTo(pair3 x)
{
if(this.a>x.a)return 1;
if(this.a<x.a)return -1;
return 0;
// return this.index-x.index;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | d2a49f0b51838a0631122cb8e198bf01 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | // "static void main" must be defined in a public class.
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
char[] ch = sc.next().toCharArray();
int len = ch.length;
int count=0, res = 1,c2 = 0;
for(int i=0; i<len; i++){
if(ch[i]=='A'){
count++;
}
else{
c2 = 1;
count--;
}
if(count<0){
res=0;
break;
}
}
if(res==0||c2==0||ch[len-1]!='B'){
System.out.println("NO");
}
else{
System.out.println("YES");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | b86a8114691bed14ff9601bc75789872 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=1000000007;
// static long mod=998244353;
static int MAX=Integer.MAX_VALUE;
static int MIN=Integer.MIN_VALUE;
static long MAXL=Long.MAX_VALUE;
static long MINL=Long.MIN_VALUE;
static ArrayList<Integer> graph[];
// static ArrayList<ArrayList<Integer>> graph;
static long fact[];
static pair seg[];
static int dp[][];
// static long dp[][];
public static void main (String[] args) throws java.lang.Exception
{
// code goes here
int t=I();
outer:while(t-->0)
{
String s=S();
char c[]=s.toCharArray();
int n=c.length;
if(c[0]=='B' || c[n-1]=='A'){
out.println("NO");
}else{
int cnt1=0,cnt2=0;
int p=0,q=0;
for(int i=0;i<n;i++){
if(c[i]=='A'){
cnt1++;
}else{
cnt2++;
if(cnt2>cnt1){
p=1;
break;
}
}
}
// for(int i=0;i<n;i++){
// if(c[i]=='A'){
// if(q==1){
// cnt=0;
// q=0;
// }
// cnt++;
// }else{
// q=1;
// cnt--;
// }
// if(cnt<0){
// p=1;
// break;
// }
// }
if(p==1)out.println("NO");
else out.println("YES");
}
}
out.close();
}
public static class pair
{
long a;
long b;
public pair(long aa,long bb)
{
a=aa;
b=bb;
}
}
public static class myComp implements Comparator<pair>
{
//sort in ascending order.
public int compare(pair p1,pair p2)
{
if(p1.a==p2.a)
return 0;
else if(p1.a<p2.a)
return -1;
else
return 1;
}
// sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return 1;
// else
// return -1;
// }
}
public static void DFS(int s,boolean visited[])
{
visited[s]=true;
for(int i:graph[s]){
if(!visited[i]){
DFS(i,visited);
}
}
}
public static void setGraph(int n,int m)
{
graph=new ArrayList[n+1];
for(int i=0;i<=n;i++){
graph[i]=new ArrayList<>();
}
for(int i=0;i<m;i++){
int u=I(),v=I();
graph[u].add(v);
graph[v].add(u);
}
}
//LOWER_BOUND and UPPER_BOUND functions
//It returns answer according to zero based indexing.
public static int lower_bound(long arr[],long X,int start, int end) //start=0,end=n-1
{
if(start>end)return -1;
if(arr[arr.length-1]<X)return end;
if(arr[0]>X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
// if(arr[mid]==X){ //Returns last index of lower bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
if(arr[mid]==X){ //Returns first index of lower bound value.
if(mid>start && arr[mid-1]==X){
right=mid-1;
}else{
return mid;
}
}
else if(arr[mid]>X){
if(mid>start && arr[mid-1]<X){
return mid-1;
}else{
right=mid-1;
}
}else{
if(mid<end && arr[mid+1]>X){
return mid;
}else{
left=mid+1;
}
}
}
return left;
}
//It returns answer according to zero based indexing.
public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1
{
if(arr[0]>=X)return start;
if(arr[arr.length-1]<X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
if(arr[mid]==X){ //returns first index of upper bound value.
if(mid>start && arr[mid-1]==X){
right=mid-1;
}else{
return mid;
}
}
// if(arr[mid]==X){ //returns last index of upper bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
else if(arr[mid]>X){
if(mid>start && arr[mid-1]<X){
return mid;
}else{
right=mid-1;
}
}else{
if(mid<end && arr[mid+1]>X){
return mid+1;
}else{
left=mid+1;
}
}
}
return left;
}
//END
//Segment Tree Code
public static void buildTree(long a[],int si,int ss,int se)
{
if(ss==se){
seg[si].a=a[ss];
seg[si].b=1;
return;
}
int mid=(ss+se)/2;
buildTree(a,2*si+1,ss,mid);
buildTree(a,2*si+2,mid+1,se);
long p=seg[2*si+1].a,q=seg[2*si+2].a;
seg[si].a=min(p,q);
if(p==q){
seg[si].b=seg[2*si+1].b+seg[2*si+2].b;
}else{
if(p>q){
seg[si].b=seg[2*si+2].b;
}else{
seg[si].b=seg[2*si+1].b;
}
}
// seg[si]=(seg[2*si+1]&seg[2*si+2]);
}
public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b)
{
int i=0,j=0;
while(i<a.size() && j<b.size()){
if(a.get(i)<=b.get(j)){
f.add(a.get(i));
i++;
}else{
f.add(b.get(j));
j++;
}
}
while(i<a.size()){
f.add(a.get(i));
i++;
}
while(j<b.size()){
f.add(b.get(j));
j++;
}
}
public static void update(int si,int ss,int se,int pos,long val)
{
if(ss==se){
seg[si].a=val;
seg[si].b=1;
return;
}
int mid=(ss+se)/2;
if(pos<=mid){
update(2*si+1,ss,mid,pos,val);
}else{
update(2*si+2,mid+1,se,pos,val);
}
long p=seg[2*si+1].a;
long q=seg[2*si+2].a;
seg[si].a=min(p,q);
if(p==q){
seg[si].b=seg[2*si+1].b+seg[2*si+2].b;
}else{
if(p>q){
seg[si].b=seg[2*si+2].b;
}else{
seg[si].b=seg[2*si+1].b;
}
}
}
public static pair m(pair a, pair b){
if(a.a>b.a)return b;
if(a.a<b.a)return a;
return new pair(a.a,a.b+b.b);
}
public static pair query1(int si,int ss,int se,int qs,int qe)
{
if(qs>se || qe<ss)return new pair(MAXL,0);
if(ss>=qs && se<=qe)return seg[si];
int mid=(ss+se)/2;
return m(query1(2*si+1,ss,mid,qs,qe),query1(2*si+2,mid+1,se,qs,qe));
}
// public static long query2(int si,int ss,int se,int qs,int qe)
// {
// if(qs>se || qe<ss)return 0;
// if(ss>=qs && se<=qe)return seg[si];
// int mid=(ss+se)/2;
// return query2(2*si+1,ss,mid,qs,qe)+query2(2*si+2,mid+1,se,qs,qe);
// }
//Segment Tree Code end
//Prefix Function of KMP Algorithm
public static int[] KMP(char c[],int n)
{
int pi[]=new int[n];
for(int i=1;i<n;i++){
int j=pi[i-1];
while(j>0 && c[i]!=c[j]){
j=pi[j-1];
}
if(c[i]==c[j])j++;
pi[i]=j;
}
return pi;
}
public static long kadane(long a[],int n) //largest sum subarray
{
long max_sum=Long.MIN_VALUE,max_end=0;
for(int i=0;i<n;i++){
max_end+=a[i];
if(max_sum<max_end){max_sum=max_end;}
if(max_end<0){max_end=0;}
}
return max_sum;
}
public static long nPr(int n,int r)
{
long ans=divide(fact(n),fact(n-r),mod);
return ans;
}
public static long nCr(int n,int r)
{
long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod);
return ans;
}
public static boolean isSorted(int a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static boolean isSorted(long a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static int computeXOR(int n) //compute XOR of all numbers between 1 to n.
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
public static int np2(int x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
public static int hp2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static long hp2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static ArrayList<Integer> primeSieve(int n)
{
ArrayList<Integer> arr=new ArrayList<>();
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
public static class FenwickTree
{
int farr[];
int n;
public FenwickTree(int c)
{
n=c+1;
farr=new int[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x,int p)
{
for(;x<n;x+=x&(-x))
{
farr[x]+=p;
}
}
public int get(int x)
{
int ans=0;
for(;x>0;x-=x&(-x))
{
ans=ans+farr[x];
}
return ans;
}
}
//Disjoint Set Union
public static class DSU
{
int par[],rank[];
public DSU(int c)
{
par=new int[c+1];
rank=new int[c+1];
for(int i=0;i<=c;i++)
{
par[i]=i;
rank[i]=0;
}
}
public int find(int a)
{
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public void union(int a,int b)
{
int a_rep=find(a),b_rep=find(b);
if(a_rep==b_rep)
return;
if(rank[a_rep]<rank[b_rep])
par[a_rep]=b_rep;
else if(rank[a_rep]>rank[b_rep])
par[b_rep]=a_rep;
else
{
par[b_rep]=a_rep;
rank[a_rep]++;
}
}
}
public static HashMap<Integer,Integer> primeFact(int a)
{
// HashSet<Long> arr=new HashSet<>();
HashMap<Integer,Integer> hm=new HashMap<>();
int p=0;
while(a%2==0){
// arr.add(2L);
p++;
a=a/2;
}
hm.put(2,hm.getOrDefault(2,0)+p);
for(int i=3;i*i<=a;i+=2){
p=0;
while(a%i==0){
// arr.add(i);
p++;
a=a/i;
}
hm.put(i,hm.getOrDefault(i,0)+p);
}
if(a>2){
// arr.add(a);
hm.put(a,hm.getOrDefault(a,0)+1);
}
// return arr;
return hm;
}
public static boolean isInteger(double N)
{
int X = (int)N;
double temp2 = N - X;
if (temp2 > 0)
{
return false;
}
return true;
}
public static boolean isPalindrome(String s)
{
int n=s.length();
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long fact(long n)
{
long fact=1;
for(long i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long fact(int n)
{
long fact=1;
for(int i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static boolean isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean isPrime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void printArray(long a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(char a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]);
}
out.println();
}
public static void printArray(String a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(boolean a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(pair a[])
{
for(pair p:a){
out.println(p.a+"->"+p.b);
}
}
public static void printArray(int a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(long a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(char a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(ArrayList<?> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapI(HashMap<?,?> hm){
for(Map.Entry<?,?> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMap(HashMap<Long,ArrayList<Integer>> hm){
for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){
out.print(e.getKey()+"->");
ArrayList<Integer> arr=e.getValue();
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}out.println();
}
}
//Modular Arithmetic
public static long add(long a,long b)
{
a+=b;
if(a>=mod)a-=mod;
return a;
}
public static long sub(long a,long b)
{
a-=b;
if(a<0)a+=mod;
return a;
}
public static long mul(long a,long b)
{
return ((a%mod)*(b%mod))%mod;
}
public static long divide(long a,long b,long m)
{
a=mul(a,modInverse(b,m));
return a;
}
public static long modInverse(long a,long m)
{
int x=0,y=0;
own p=new own(x,y);
long g=gcdExt(a,m,p);
if(g!=1){
out.println("inverse does not exists");
return -1;
}else{
long res=((p.a%m)+m)%m;
return res;
}
}
public static long gcdExt(long a,long b,own p)
{
if(b==0){
p.a=1;
p.b=0;
return a;
}
int x1=0,y1=0;
own p1=new own(x1,y1);
long gcd=gcdExt(b,a%b,p1);
p.b=p1.a - (a/b) * p1.b;
p.a=p1.b;
return gcd;
}
public static long pwr(long m,long n)
{
long res=1;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m);
}
n=n>>1;
m=(m*m);
}
return res;
}
public static long modpwr(long m,long n)
{
long res=1;
m=m%mod;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m)%mod;
}
n=n>>1;
m=(m*m)%mod;
}
return res;
}
public static class own
{
long a;
long b;
public own(long val,long index)
{
a=val;
b=index;
}
}
//Modular Airthmetic
public static void sort(int[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(char[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
char tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(long[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
//max & min
public static int max(int a,int b){return Math.max(a,b);}
public static int min(int a,int b){return Math.min(a,b);}
public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));}
public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));}
public static long max(long a,long b){return Math.max(a,b);}
public static long min(long a,long b){return Math.min(a,b);}
public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));}
public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));}
public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
//end
public static int[] I(int n){int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;}
public static long[] IL(int n){long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;}
public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static int I(){return sc.I();}
public static long L(){return sc.L();}
public static String S(){return sc.S();}
public static double D(){return sc.D();}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int I(){ return Integer.parseInt(next());}
long L(){ return Long.parseLong(next());}
double D(){return Double.parseDouble(next());}
String S(){
String str = "";
try {
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 7333f62327c7cee2149717bb99143cbe | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BILoveAAAB solver = new BILoveAAAB();
solver.solve(1, in, out);
out.close();
}
static class BILoveAAAB {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
String s = in.nextLine();
boolean ans = true;
int a = 0;
int b = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'B') {
b++;
} else {
a++;
}
if (b > a) {
ans = false;
break;
}
}
if (s.charAt(s.length() - 1) == 'A') ans = false;
out.println(ans ? "YES" : "NO");
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
public String next() {
while (st == null || (!st.hasMoreElements())) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | ab6709eee721121ed9a96561f61da227 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
/**
* @author Naitik
*
*/
public class Main
{
static FastReader sc=new FastReader();
static long dp[][];
// static boolean v[][][];
static int mod=998244353;;
// static int mod=1000000007;
static int max;
static int bit[];
//static long fact[];
static HashMap<Long,Integer> map;
//static StringBuffer sb=new StringBuffer("");
//static HashMap<Integer,Integer> map;
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
outer :while (ttt-- > 0)
{
// int n=i();
char A[]=s().toCharArray();
int n=A.length;
if(n<2 || A[0]=='B' || A[n-1]!='B') {
System.out.println("NO");
continue outer;
}
int cnt=0;
for(int i=0;i<n;i++) {
if(A[i]=='A') {
cnt++;
}
else {
if(cnt==0) {
System.out.println("NO");
continue outer;
}
cnt--;
}
}
System.out.println("YES");
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
// int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return A[a]=find(A, A[a]);
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return find(A, A[a]);
//}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
static void add(long v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(long v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
public static int upper(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
if(n==0)
return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void print(int A[]) {
for(int i : A) {
out.print(i+" ");
}
out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static HashMap<Long,Integer> hash(long A[]){
HashMap<Long,Integer> map=new HashMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 92d101920646089382b0703deaf118c5 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | /*
I am dead inside
Do you like NCT, sKz, BTS?
5 4 3 2 1 Moonwalk
Imma knock it down like domino
Is this what you want? Is this what you want?
Let's ttalkbocky about that
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class x1672B
{
public static void main(String omkar[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
char[] arr = st.nextToken().toCharArray();
String res = "YES\n";
int N = arr.length;
if(arr[0] == 'A')
{
int cnt = 1;
for(int i=1; i < N; i++)
{
if(arr[i] == 'A')
cnt++;
else
cnt--;
if(cnt < 0)
res = "NO\n";
}
}
if(arr[0] == 'B' || N < 2)
res = "NO\n";
if(arr[N-1] == 'A')
res = "NO\n";
sb.append(res);
}
System.out.print(sb);
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
}
/*
ABAAB
AABB
AAABABB
*/ | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 98f7432a28262bca257eb33215fab703 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class cf1672b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
int a = 0, b = 0;
boolean f = true;
for (char ch : s.toCharArray()) {
if (ch == 'A') {
a++;
} else {
b++;
if (a < b) {
System.out.println("NO");
f = false;
break;
}
}
}
if (f) {
if (b != 0 && s.charAt(s.length() - 1) == 'B') {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 0b8838618953828c827d80622ae1242a | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | // Created By Jefferson Samuel
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.regex.Pattern;
import static java.lang.Math.*;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
public class A {
static void solve() throws Exception {
int T = 1;
T = sint();
for(int t = 1;t<=T;t++) {
String S = sstr();
boolean isOk = true;
if(S.length() <2 || S.charAt(0)=='B' || S.charAt(S.length()-1)=='A')isOk=false;
int Pk = 0;
for(char k : S.toCharArray())
{
if(k == 'A')Pk++;
else
Pk--;
if(Pk<0)isOk=false;
}
out.println(isOk ?"YES":"NO");
}
}
/* -----------------------------------------------------********------------------------------------------------------*/
//Globals
static int[] d4 = {-1,1,0,0};
static int[] d8r = {-1,-1,-1,1,1,1,0,0};
static int[] d8c = {-1,1,0,-1,1,0,-1,1};
static final int IMa = Integer.MAX_VALUE;
static final int Imi = Integer.MIN_VALUE;
static final long LMa = Long.MAX_VALUE;
static final long LMi = Long.MIN_VALUE;
// Graphs
static ArrayList<Integer>[] Gph;
static boolean[] vis;
static int[][] Grid;
static char[][] Crid;
// Essentials
static int sint() throws IOException {return parseInt(sstr());}
static double sdob() throws IOException{return parseDouble(sstr());}
static long gcd(long a, long b){
return (b == 0) ? a : gcd(b, a % b);
}
static int[] iar(int N) throws IOException {
int[] a = new int[N];
for(int i = 0;i<N;i++)a[i] = sint();
return a;
}
static String sfull() throws IOException {return in.readLine();}
static void revsor(long[] ar){sortAr(ar);revar(ar);}
static void revar(long[] ar){for (int i = 0; i < ar.length / 2; i++) { long temp = ar[i]; ar[i] = ar[ar.length - 1 - i]; ar[ar.length - 1 - i] = temp;}}
static long[] lar(int N) throws IOException {
long[] a = new long[N];
for(int i = 0;i<N;i++)a[i] = slong();
return a;
}
static String[] Sfar(int N)throws IOException{
String ar[] = new String[N];
for(int i =0;i<N;i++)ar[i] = sfull();
return ar;
}
static String[] Sar(int N)throws IOException{
String ar[] = new String[N];
for(int i =0;i<N;i++)
ar[i] = sstr();
return ar;
}
static final Random random=new Random();
static void sortAr(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void sortAr(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long slong() throws IOException {return parseLong(sstr());}
static String sstr() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
public static PrintWriter out;
static StringTokenizer tok;
static final int MOD = (int) 1e9 + 7;
static class Pair<T,K>{ T F;K S;public Pair(T F, K S) {this.F = F;this.S = S;}}
static class Pii{int F,S; public Pii(int F,int S){this.F = F;this.S = S;}}
static class Pil{int F;long S; public Pil(int F,long S){this.F=F;this.S=S;}}
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
long startTime = System.currentTimeMillis();
solve();
in.close();
out.close();
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
err.println("Exec time " + timeElapsed+"ms");
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 857c33b613c6b1fdeaf82c6ab59f4447 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
String s= sc.nextLine();
int n= s.length();
String ans="YES";
if(n<2 || s.charAt(n-1)!='B' || s.charAt(0)=='B') ans="NO";
else {
int c=0;
for(int i=0;i<n;i++) {
if(s.charAt(i)=='A') c++;
if(s.charAt(i)=='B') {
if(c==0) ans="NO";
c--;
}
}
}
pw.println(ans);
}
pw.flush();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 8 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c3fa0388cd512c52c8ca6844cb5578da | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
char ch[]=br.readLine().toCharArray();
boolean flag=true;
int count=0;
int ans=0;
for(int i=0;i<ch.length;i++)
{
if(ch[i]=='B')
{
count++;
}
else{
ans++;
}
if(count>ans)
break;
}
if(ans>=count && ch[0]=='A'&& ch[ch.length-1]=='B'){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 44534531c8418c1aa163675daee3fb55 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class d3
{
//static boolean[] visited;
//static long min;
public static void main (String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
outer : while(t-->0){
st = new StringTokenizer(br.readLine());
String s = st.nextToken().trim();
int n= s.length();
// for(int i = n-1;i>0;i--){
// if(s.charAt(i) == 'B'){
// if(s.charAt(i-1) == 'B'){
// bw.write("NO\n");
// continue outer;
// }
// }
// }
if(n == 1){
bw.write("NO\n");
continue outer;
}
// if(s.charAt(0) == 'B'){
// bw.write("NO\n");
// continue outer;
// }
boolean visited = false;
int countA = 0;
int countB = 0;
int cur = 0;
for(int i = 0;i<n;){
if(s.charAt(i) == 'A'){
cur++;
countA++;
visited = false;
i++;
}
else{
while(i<n && s.charAt(i) == 'B'){
countB++;
if(countA < countB){
bw.write("NO\n");
continue outer;
}
i++;
}
cur = 0;
}
}
// if(countA > 0){
// bw.write("NO\n");
// continue outer;
// }
if(cur != 0){
bw.write("NO\n");
continue outer;
}
bw.write("YES\n");
}
bw.flush();
}
public static int recur(int e){
if(e <= 0){
return 0;
}
if(e == 1){
return 1;
}
if(e == 2){
return 2;
}
if(e == 3){
return 4;
}
return recur(e-1)+recur(e-2)+recur(e-3);
}
static class pair{
int a;
int b;
pair(int a,int b){
this.a = a;
this.b = b;
}
}
public static boolean check1(ArrayList<ArrayList<Integer>> graph,boolean[] visited){
for(int i = 0;i<visited.length;i++){
if(!visited[i] && graph.get(i).size() != 0){
if(dfs(i, graph, visited)){
return true;
}
}
print1(visited);
}
return false;
}
public static void print1(boolean[] visited){
for(int i = 0;i<visited.length;i++){
System.out.print(visited[i]+" ");
}
System.out.println();
}
public static boolean dfs(int i ,ArrayList<ArrayList<Integer>> g,boolean[] visited){
visited[i] = true;
for(int e : g.get(i)){
if(!visited[e]){
// if(dfs(e,g,visited) == true){
// System.out.println("ji");
// return true;
// }
// dfs(i, g, visited);
}
else{
return true;
}
}
return false;
}
public static boolean checksum(int n){
int temp = 0;
while(n > 0){
temp += n%10;
n /= 10;
}
return (temp == 10)?true:false;
}
public static class Node{
int val;
int freq;
Node(int val,int freq){
this.val = val;
this.freq = freq;
}
}
/* public static void dijk(ArrayList<ArrayList<pair>> graph,int i,boolean[] visited){
int[] dist = new int[visited.length];
for(int j = 0;j<dist.length;i++){
dist[j] = Integer.MAX_VALUE;
}
dist[i] = 0;
PriorityQueue<pair> queue = new PriorityQueue<>((pair a1,pair a2)->(a1.dist - a2.dist));
queue.add(new pair(i, 0));
while(!queue.isEmpty()){
pair p1 = queue.poll();
int current = p1.b;
if(visited[current]){
continue;
}
visited[current] = true;
for(pair p : graph.get(current)){
int childnode = p.b;
int childdist = p.dist;
if(!visited[childnode] && dist[childnode] > dist[current]+childdist){
dist[childnode] = dist[current]+childdist;
queue.add(p);
}
}
}
for(int j = 0;i<visited.length;j++){
System.out.println(dist[j]+" ");
}
}*/
public static int ceil(ArrayList<Long> al,long val){
int low = 0;
int high = al.size()-1;
int pos = 0;
while(low <= high){
int mid = (low+high)/2;
if(al.get(mid) == val){
pos = Math.max(pos,mid);
low = mid+1;
}
else if(al.get(mid) < val){
low = mid+1;
}
else{
high = mid-1;
}
}
return pos;
}
public static int floor(ArrayList<Long> al,long val){
int low = 0;
int high = al.size()-1;
int pos = high;
while(low <= high){
int mid = (low+high)/2;
if(al.get(mid) == val){
pos = Math.min(pos,mid);
high = mid-1;
}
else if(al.get(mid) < val){
low = mid+1;
}
else{
high = mid-1;
}
}
return pos;
}
public static boolean palin(String s){
for(int i = 0;i<s.length()/2;i++){
if(s.charAt(i) == s.charAt(s.length()-i-1)){
continue;
}
else{
return false;
}
}
return true;
}
public static long gcd(long a,long b){
if(b == 0L){
return a;
}
return gcd(b,a%b);
}
public static boolean check(ArrayList<Long> al,long mid,long m){
long cur = 0L;
for(int i = 0;i<al.size();i++){
if(al.get(i) <= mid){
cur++;
}
else{
cur += (al.get(i)/mid);
cur += (al.get(i)%mid != 0)?1L:0L;
}
}
return (cur <= m)?true:false;
}
// public static int bs(int sum,int low,int high){
// }
public static long fin(long n){
return ((long)n)*(n+1)/2;
}
public static int bceil(ArrayList<Integer> al,int val,int low,int high){
int index = 0;
while(low <= high){
int mid = low + (high-low)/2;
if(al.get(mid) <= val){
index = Math.max(mid,index);
low = mid+1;
}
else{
high = mid-1;
}
}
return index;
}
public static int bfloor(ArrayList<Integer> al,int val){
int low = 0;
int high = al.size()-1;
int index = -1;
while(low <= high){
int mid = low + (high-low)/2;
int mval = al.get(mid);
if(mval >= val){
high = mid-1;
index = Math.max(index,mid);
}
else{
low = mid+1;
}
}
return index;
}
public static String rev(String s){
StringBuffer sb = new StringBuffer(s);
sb.reverse();
return s+sb.toString();
}
public static String rev1(String s){
StringBuffer sb = new StringBuffer(s);
sb.reverse();
return sb.toString()+s;
}
public static long fact12(long a){
long l = 1L;
for(long i = 1;i<=a;i++){
l *= i;
}
// System.out.println(l);
return l;
}
public static boolean binary(ArrayList<Integer> al,int a1){
int low = 0;
int high = al.size();
while(low <= high){
int mid = (low+high)/2;
if(a1 == al.get(mid)){
return true;
}
else if(a1 > al.get(mid)){
low = mid+1;
}
else{
high = mid-1;
}
}
return false;
}
public static void primefact(int n){
for(int i = 2;i*i<=n;i++){
if(n%i == 0){
while(n%i == 0){
n /= i;
System.out.println(i);
}
}
}
System.out.println(n);
}
public static boolean prime(long n){
int count = 0;
for(long i = 1;i*i <= n;i++){
if(n%i == 0){
if(i*i == n){
count++;
}
else{
count += 2;
}
}
}
if(count == 2){
return true;
}
return false;
}
public static boolean bsearch(ArrayList<Integer> al,int low,int high,int req){
if(low > high || low < 0 || high < 0 || low >= al.size() || high >= al.size()){
return false;
}
int mid = (low+high)/2;
if(al.get(mid) == req){
return true;
}
else if(al.get(mid) > req){
high = mid-1;
bsearch(al, low, high, req);
}
else{
low = mid+1;
bsearch(al, low, high, req);
}
return false;
}
public static long fact1(int n){
long ans = 1L;
for(int i = 2;i<=n;i++){
ans *= i;
}
return ans;
}
public static String reverse(String s){
StringBuilder sb = new StringBuilder(s);
return sb.reverse().toString();
}
public static int find(int n){
return n*(n+1)/2;
}
public static double calc(int x1,int x2,int y1,int y2){
return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c5071dc150c6c33eac1c9246aa20781d | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class AAB {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
for(int i=1;i<=n;i++){
String s=in.next();
if(IsGood(s)){
System.out.println("YES");
}
else
System.out.println("NO");
}
}
static boolean IsGood(String s){
if(s.charAt(s.length()-1)!='B'){
return false;
}
int a=0,b=0;int i;
for( i=0;i<s.length()&&(a>=b);i++){
if(s.charAt(i)=='A'){
a++;
}
if(s.charAt(i)=='B'){
b++;
}
}
if(b>a) return false;
else if(i<2)
return false;
return true;
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | d3c0631712929ef58a0446b70f62a2d9 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(r.readLine());
int t = Integer.parseInt(st.nextToken());
outer:
for(int a = 0; a < t; a++) {
String s = r.readLine();
if(s.length() < 2 || s.charAt(s.length() - 1) == 'A') {
pw.println("NO");
continue;
}
int aCount = 0;
int bCount = 0;
int count = 0;
while(count < s.length()) {
if(s.charAt(count) == 'A') {
aCount++;
} else {
bCount++;
}
if(aCount < bCount) {
pw.println("NO");
continue outer;
}
count++;
}
pw.println("YES");
}
pw.close();
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 9dde11505837d3d92da9c36564e044cc | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | // Source: https://usaco.guide/general/io
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(r.readLine());
int t = Integer.parseInt(st.nextToken());
outer:
for(int a = 0; a < t; a++) {
String s = r.readLine();
boolean works = s.charAt(s.length() - 1) == 'B';
int aCount = 0;
int bCount = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'A') {
aCount++;
} else {
bCount++;
}
if(aCount < bCount) {
works = false;
}
}
if(works) { pw.println("YES"); }
else { pw.println("NO"); }
}
pw.close();
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 2e4f6d71df440fe35a7ad9825dee01b6 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | // Source: https://usaco.guide/general/io
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(r.readLine());
int t = Integer.parseInt(st.nextToken());
outer:
for(int a = 0; a < t; a++) {
String s = r.readLine();
boolean works = s.charAt(s.length() - 1) == 'B';
int sum = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'A') {
sum++;
} else {
sum--;
}
if(sum < 0) {
works = false;
}
}
if(works) { pw.println("YES"); }
else { pw.println("NO"); }
}
pw.close();
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c4dbb80021dc6d14e81717b6189223a4 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.*;
import java.sql.Array;
public class Simple{
static final Random random=new Random();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
//
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
long oi=random.nextInt(n), temp=a[(int)oi];
a[(int)oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair other){
return (int) (this.y - other.y);
}
public boolean equals(Pair other){
if(this.x == other.x && this.y == other.y)return true;
return false;
}
public int hashCode(){
return 31*x + y;
}
// @Override
// public int compareTo(Simple.Pair o) {
// // TODO Auto-generated method stub
// return 0;
// }
}
static int power(int x, int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
static int nCrModp(int n, int r, int p)
{
if (r > n - r)
r = n - r;
// The array C is going to store last
// row of pascal triangle at the end.
// And last entry of last row is nCr
int C[] = new int[r + 1];
C[0] = 1; // Top row of Pascal Triangle
// One by constructs remaining rows of Pascal
// Triangle from top to bottom
for (int i = 1; i <= n; i++) {
// Fill entries of current row using previous
// row values
for (int j = Math.min(i, r); j > 0; j--)
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public static class Node{
int root;
ArrayList<Node> al;
Node par;
public Node(int root,ArrayList<Node> al,Node par){
this.root = root;
this.al = al;
this.par = par;
}
}
// public static int helper(int arr[],int n ,int i,int j,int dp[][]){
// if(i==0 || j==0)return 0;
// if(dp[i][j]!=-1){
// return dp[i][j];
// }
// if(helper(arr, n, i-1, j-1, dp)>=0){
// return dp[i][j]=Math.max(helper(arr, n, i-1, j-1,dp)+arr[i-1], helper(arr, n, i-1, j,dp));
// }
// return dp[i][j] = helper(arr, n, i-1, j,dp);
// }
// public static void dfs(ArrayList<ArrayList<Integer>> al,int levelcount[],int node,int count){
// levelcount[count]++;
// for(Integer x : al.get(node)){
// dfs(al, levelcount, x, count+1);
// }
// }
public static long __gcd(long a, long b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
public static class DSU{
int n;
int par[];
int rank[];
public DSU(int n){
this.n = n;
par = new int[n+1];
rank = new int[n+1];
for(int i=1;i<=n;i++){
par[i] = i ;
rank[i] = 0;
}
}
public int findPar(int node){
if(node==par[node]){
return node;
}
return par[node] = findPar(par[node]);//path compression
}
public void union(int u,int v){
u = findPar(u);
v = findPar(v);
if(rank[u]<rank[v]){
par[u] = v;
}
else if(rank[u]>rank[v]){
par[v] = u;
}
else{
par[v] = u;
rank[u]++;
}
}
}
public static int helper(int arr[],int k,int n,int i,int dp[]){
if(i>k)return 1;
if(arr[i]==-1){
return dp[i] = helper(arr, k, n, 2*i, dp) + helper(arr, k, n, 2*i+1, dp);
}
else if(arr[i]==0){
return dp[i] = helper(arr, k, n, 2*i, dp);
}
else{
return dp[i] = helper(arr, k, n, 2*i+1, dp);
}
}
static void leftRotate(int arr[], int d, int n)
{
/* To handle if d >= n */
d = d % n;
int i, j, k, temp;
int g_c_d = gcd(d, n);
for (i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
temp = arr[i];
j = i;
while (true) {
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
public static void main(String args[]){
try {
FastReader s=new FastReader();
FastWriter out = new FastWriter();
// int testCases=s.nextInt();
int testCases = s.nextInt();
while(testCases-- > 0){
String str = s.next();
int n = str.length();
boolean bool = true;
if(n==1){
System.out.println("NO");
continue;
}
int i=0;
int counta = 0;
while(i<n){
if(i!=n-1 &&str.charAt(i)=='A' && str.charAt(i+1)=='B'){
i+=2;
}
else if(str.charAt(i)=='A'){
counta++;
i++;
}
else{
if(counta==0){
bool = false;
break;
}
counta--;
i++;
}
}
if(str.charAt(n-1)=='A' && counta>0)bool = false;
if(bool){
System.out.println("YES");
}
else{
System.out.println("NO");
}
// String str = s.next();
// int n = str.length();
// int len = 0;
// boolean bool = true;
// int lenb = 0;
// for(int i=0;i<n;i++){
// if(str.charAt(i)=='A'){
// len++;
// }
// else{
// int j = i;
// while(j<n && str.charAt(j)=='B'){
// lenb++;
// j++;
// }
// if(lenb>len){
// bool = false;
// break;
// }
// len =0;
// lenb =0;
// i = j-1;
// }
// }
// if(len!=0)bool = false;
// if(bool){
// System.out.println("YES");
// }
// else{
// System.out.println("NO");
// }
}
out.close();
} catch (Exception e) {
return;
}
// int t = s.nextInt();
// for(int t1 = 1;t1<=t;t1++){
// }
// out.close();
}
}
/*
4 2 2 7
0 2 5
-2 3
5
0*x1 + 1*x2 + 2*x3 + 3*x4
*/ | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | a4b7224ee609bcdd28656bd4fbf45c80 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.*;
import java.sql.Array;
public class Simple{
static final Random random=new Random();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
long oi=random.nextInt(n), temp=a[(int)oi];
a[(int)oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair other){
return (int) (this.y - other.y);
}
public boolean equals(Pair other){
if(this.x == other.x && this.y == other.y)return true;
return false;
}
public int hashCode(){
return 31*x + y;
}
// @Override
// public int compareTo(Simple.Pair o) {
// // TODO Auto-generated method stub
// return 0;
// }
}
static int power(int x, int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
static int nCrModp(int n, int r, int p)
{
if (r > n - r)
r = n - r;
// The array C is going to store last
// row of pascal triangle at the end.
// And last entry of last row is nCr
int C[] = new int[r + 1];
C[0] = 1; // Top row of Pascal Triangle
// One by constructs remaining rows of Pascal
// Triangle from top to bottom
for (int i = 1; i <= n; i++) {
// Fill entries of current row using previous
// row values
for (int j = Math.min(i, r); j > 0; j--)
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public static class Node{
int root;
ArrayList<Node> al;
Node par;
public Node(int root,ArrayList<Node> al,Node par){
this.root = root;
this.al = al;
this.par = par;
}
}
// public static int helper(int arr[],int n ,int i,int j,int dp[][]){
// if(i==0 || j==0)return 0;
// if(dp[i][j]!=-1){
// return dp[i][j];
// }
// if(helper(arr, n, i-1, j-1, dp)>=0){
// return dp[i][j]=Math.max(helper(arr, n, i-1, j-1,dp)+arr[i-1], helper(arr, n, i-1, j,dp));
// }
// return dp[i][j] = helper(arr, n, i-1, j,dp);
// }
// public static void dfs(ArrayList<ArrayList<Integer>> al,int levelcount[],int node,int count){
// levelcount[count]++;
// for(Integer x : al.get(node)){
// dfs(al, levelcount, x, count+1);
// }
// }
public static long __gcd(long a, long b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
public static class DSU{
int n;
int par[];
int rank[];
public DSU(int n){
this.n = n;
par = new int[n+1];
rank = new int[n+1];
for(int i=1;i<=n;i++){
par[i] = i ;
rank[i] = 0;
}
}
public int findPar(int node){
if(node==par[node]){
return node;
}
return par[node] = findPar(par[node]);//path compression
}
public void union(int u,int v){
u = findPar(u);
v = findPar(v);
if(rank[u]<rank[v]){
par[u] = v;
}
else if(rank[u]>rank[v]){
par[v] = u;
}
else{
par[v] = u;
rank[u]++;
}
}
}
public static int helper(int arr[],int k,int n,int i,int dp[]){
if(i>k)return 1;
if(arr[i]==-1){
return dp[i] = helper(arr, k, n, 2*i, dp) + helper(arr, k, n, 2*i+1, dp);
}
else if(arr[i]==0){
return dp[i] = helper(arr, k, n, 2*i, dp);
}
else{
return dp[i] = helper(arr, k, n, 2*i+1, dp);
}
}
static void leftRotate(int arr[], int d, int n)
{
/* To handle if d >= n */
d = d % n;
int i, j, k, temp;
int g_c_d = gcd(d, n);
for (i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
temp = arr[i];
j = i;
while (true) {
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
public static void main(String args[]){
try {
FastReader s=new FastReader();
FastWriter out = new FastWriter();
// int testCases=s.nextInt();
int testCases = s.nextInt();
while(testCases-- > 0){
String str = s.next();
int n = str.length();
boolean bool = true;
if(n==1){
System.out.println("NO");
continue;
}
int i=0;
int counta = 0;
while(i<n){
if(i!=n-1 &&str.charAt(i)=='A' && str.charAt(i+1)=='B'){
i+=2;
}
else if(str.charAt(i)=='A'){
counta++;
i++;
}
else{
if(counta==0){
bool = false;
break;
}
counta--;
i++;
}
}
if(str.charAt(n-1)=='A' && counta>0)bool = false;
if(bool){
System.out.println("YES");
}
else{
System.out.println("NO");
}
// String str = s.next();
// int n = str.length();
// int len = 0;
// boolean bool = true;
// int lenb = 0;
// for(int i=0;i<n;i++){
// if(str.charAt(i)=='A'){
// len++;
// }
// else{
// int j = i;
// while(j<n && str.charAt(j)=='B'){
// lenb++;
// j++;
// }
// if(lenb>len){
// bool = false;
// break;
// }
// len =0;
// lenb =0;
// i = j-1;
// }
// }
// if(len!=0)bool = false;
// if(bool){
// System.out.println("YES");
// }
// else{
// System.out.println("NO");
// }
}
out.close();
} catch (Exception e) {
return;
}
// int t = s.nextInt();
// for(int t1 = 1;t1<=t;t1++){
// }
// out.close();
}
}
/*
4 2 2 7
0 2 5
-2 3
5
0*x1 + 1*x2 + 2*x3 + 3*x4
*/ | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | f82d4e810d90982d5372c896add3f6e2 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void sort(long[] a) {
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < a.length; i++)
arr.add(a[i]);
Collections.sort(arr);
for (int i = 0; i < arr.size(); i++)
a[i] = arr.get(i);
}
public static Scanner obj = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static int i() {
return obj.nextInt();
}
public static void main(String[] args) {
int len = i();
while (len-- != 0) {
String s=obj.next();
Stack<Character> stack=new Stack<>();
boolean ans=true;
int c=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='A')stack.add('A');
else
{
c++;
if(stack.isEmpty()) {
ans=false;
break;
}
stack.pop();
}
}
if(c==0 || s.charAt(s.length()-1)=='A')ans=false;
if(ans)out.println("YES");
else out.println("NO");
}
out.flush();
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | cfb3e95034f1f867cea64212770bfeb2 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class ILoveAAAB {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s2;
int t, A, B;
boolean result;
t = scan.nextInt();
scan.nextLine();
for (int i = 1; i <= t; i++) {
result = true;
A = 0;
B = 0;
s2 = scan.nextLine();
for (int j = 0; j < s2.length(); j++) {
if (s2.charAt(j) == 'B') B++;
else A++;
if (A < B) result = false;
}
if (result && s2.charAt(s2.length() - 1) == 'B') System.out.println("YES");
else System.out.println("NO");
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 8715d42a2fb9852acba860649b3feee9 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s2;
int t, A, B;
boolean result;
t = scan.nextInt();
scan.nextLine();
for (int i = 1; i <= t; i++) {
result = true;
A = 0;
B = 0;
s2 = scan.nextLine();
char[] s2Array = s2.toCharArray();
for (char x : s2Array) {
if (x == 'B') B++;
else A++;
if (A < B) result = false;
}
if (result && s2Array[s2Array.length - 1] == 'B') System.out.println("YES");
else System.out.println("NO");
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c9fa69df256038efba421c9a4ea6a895 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s2;
int t, A, B;
boolean result;
t = scan.nextInt();
scan.nextLine();
for (int i = 1; i <= t; i++) {
result = true;
A = 0;
B = 0;
s2 = scan.nextLine();
char[] s2Array = s2.toCharArray();
for (char x : s2Array) {
if (x == 'B') {
if (A == 0) {
result = false;
break;
} else {
A--;
}
} else {
A++;
}
}
if (result && s2Array[s2Array.length - 1] == 'B')
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 1a4bcff44ea1a20717fe07d66867286c | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class test {
static Scanner scan;
private static boolean isSuitable(String word) {
ArrayList<String> wordArrayList = new ArrayList<>(Arrays.asList(word.split("")));
ArrayList<Integer> B_indexes = new ArrayList<>();
if (wordArrayList.size() <= 1 || wordArrayList.get(0).equals("B") || wordArrayList.get(wordArrayList.size() - 1).equals("A")) {
return false;
} else {
int aNum = 0, bNum = 0;
for (int i = 0; i < wordArrayList.size(); i++) {
if (wordArrayList.get(i).equals("A")) {
aNum++;
} else {
B_indexes.add(i);
bNum++;
}
if (aNum < bNum) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
scan = new Scanner(System.in);
int numberOfCases = scan.nextInt();
for (int i = 0; i < numberOfCases; i++) {
if (isSuitable(scan.next())) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | e03f48b91c167500418aa4759d97798d | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B {
static final FastReader sc = new FastReader();
static final PrintWriter out = new PrintWriter(System.out, true);
private static boolean debug = System.getProperty("ONLINE_JUDGE") == null;
private static void trace(Object... o) {
if (debug) {
System.err.println(Arrays.deepToString(o));
}
}
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
boolean ok = true;
if (s.charAt(s.length() - 1) != 'B')
ok = false;
int c = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A')
c++;
else
c--;
if (c < 0)
ok = false;
}
out.println((ok) ? "YES" : "NO");
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 3e487d3582ec688d2bc1b69d48f24b18 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
/**
*
* @author eslam
*/
public class IceCave {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader input = new FastReader();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int t = input.nextInt();
loop:
while (t-- > 0) {
String w = input.next();
if (w.length() == 1 || w.charAt(w.length() - 1) == 'A') {
log.write("NO\n");
continue;
}
Stack<Character> s = new Stack<>();
for (int i = 0; i < w.length(); i++) {
if (w.charAt(i) == 'A') {
s.add(w.charAt(i));
} else {
if (s.isEmpty()) {
log.write("NO\n");
continue loop;
} else {
s.pop();
}
}
}
log.write("YES\n");
}
log.flush();
}
static long mod(long a, long b) {
long r = a % b;
return r < 0 ? r + b : r;
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public static void print(int a[]) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = input.nextInt();
}
}
static class pair {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return (x * y) / GCD(x, y);
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 714d3033c97f732c44fbd5b6e91dbfcc | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | // import static java.lang.System.out;
import static java.lang.Math.*;
import static java.lang.System.*;
import java.lang.reflect.Array;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static FastReader sc;
static long mod = ((long) 1e9) + 7;
static long lmax=Long.MAX_VALUE;
static long lmin=Long.MIN_VALUE;
static int imax=Integer.MAX_VALUE;
static int imin=Integer.MIN_VALUE;
// static FastWriter out;
static FastWriter out;
public static void main(String hi[]) throws IOException {
// initializeIO();
out = new FastWriter();
sc = new FastReader();
long startTimeProg = System.currentTimeMillis();
long endTimeProg = Long.MAX_VALUE;
int t = sc.nextInt();
// int t=1;
// boolean[] seave=sieveOfEratosthenes((int)(1e5));
// int[] seave2=sieveOfEratosthenesInt((long)sqrt(1e9));
// debug(seave2);
while (t-- != 0) {
String s=sc.next();
int n=s.length();
int a=0,b=0;
boolean done=true;
int last=0;
for(int i=0;i<n;i++){
if(s.charAt(i)=='A')a++;
else b++;
if(s.charAt(i)=='A')last=0;
else last++;
if(b>a){
done=false;
}
}
// debug(a+" "+b);
if(done&&last!=0){
print("yes");
}
else print("no");
}
endTimeProg = System.currentTimeMillis();
debug("[finished : " + (endTimeProg - startTimeProg) + ".ms ]");
// System.out.println(String.format("%.9f", max));
}
private static long findchanges(int[] arr, int ind, long avg) {
long v=0;
long changes=0;
int n=arr.length;
for(int i=0;i<n;i++){
if(ind==i)continue;
v+=avg-arr[i]+v;
changes+=abs(avg-arr[i]+v);
}
return changes;
}
static int c=0;
private static int find(int[] arr, int i, int j) {
List<Integer> li=new ArrayList<>();
while (i<=j)li.add(arr[i++]);
c=0;
return chk(li);
}
private static int chk(List<Integer> li) {
if(li.size()<2)return c;
List<Integer> res=new ArrayList<>();
int n=li.size();
int v=0;
for(int i=0;i<n-1;i=i+2){
int l=li.get(i);
int r=li.get(i+1);
if(l+r>=10)c++;
res.add((l+r)%10);
}
return chk(res);
}
private static boolean isPeak(int[] arr,int i,int l,int r){
if(i==l||i==r)return false;
return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]);
}
private static boolean isPeak(long[] arr,int i,int l,int r){
if(i==l||i==r)return false;
return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]);
}
private static long kadens(List<Long> li, int l, int r) {
long max = Long.MIN_VALUE;
long ans = 0;
for (int i = l; i <= r; i++) {
ans += li.get(i);
max = max(ans, max);
if (ans < 0) ans = 0;
}
return max;
}
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
private static boolean isSorted(List<Integer> li) {
int n = li.size();
if (n <= 1) return true;
for (int i = 0; i < n - 1; i++) {
if (li.get(i) > li.get(i + 1)) return false;
}
return true;
}
static boolean isPowerOfTwo(long x) {
return x != 0 && ((x & (x - 1)) == 0);
}
private static boolean ispallindromeList(List<Integer> res) {
int l = 0, r = res.size() - 1;
while (l < r) {
if (res.get(l) != res.get(r)) return false;
l++;
r--;
}
return true;
}
private static class Pair {
int first = 0;
int sec = 0;
int[] arr;
char ch;
String s;
Map<Integer, Integer> map;
Pair(int first, int sec) {
this.first = first;
this.sec = sec;
}
Pair(int[] arr) {
this.map = new HashMap<>();
for (int x : arr) this.map.put(x, map.getOrDefault(x, 0) + 1);
this.arr = arr;
}
Pair(char ch, int first) {
this.ch = ch;
this.first = first;
}
Pair(String s, int first) {
this.s = s;
this.first = first;
}
}
private static int sizeOfSubstring(int st, int e) {
int s = e - st + 1;
return (s * (s + 1)) / 2;
}
private static Set<Long> factors(long n) {
Set<Long> res = new HashSet<>();
// res.add(n);
for (long i = 1; i * i <= (n); i++) {
if (n % i == 0) {
res.add(i);
if (n / i != i) {
res.add(n / i);
}
}
}
return res;
}
private static long fact(long n) {
if (n <= 2) return n;
return n * fact(n - 1);
}
private static long ncr(long n, long r) {
return fact(n) / (fact(r) * fact(n - r));
}
private static int lessThen(long[] nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums[mid] <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int lessThen(List<Long> nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int lessThen(List<Integer> nums, int val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int greaterThen(List<Long> nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static int greaterThen(List<Integer> nums, int val, boolean work) {
int i = -1, l = 0, r = nums.size() - 1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static int greaterThen(long[] nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums[mid] >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static long gcd(long[] arr) {
long ans = 0;
for (long x : arr) {
ans = gcd(x, ans);
}
return ans;
}
private static int gcd(int[] arr) {
int ans = 0;
for (int x : arr) {
ans = gcd(x, ans);
}
return ans;
}
private static long sumOfAp(long a, long n, long d) {
long val = (n * (2 * a + ((n - 1) * d)));
return val / 2;
}
//geometrics
private static double areaOftriangle(double x1, double y1, double x2, double y2, double x3, double y3) {
double[] mid_point = midOfaLine(x1, y1, x2, y2);
// debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3);
double height = distanceBetweenPoints(mid_point[0], mid_point[1], x3, y3);
double wight = distanceBetweenPoints(x1, y1, x2, y2);
// debug(height+" "+wight);
return (height * wight) / 2;
}
private static double distanceBetweenPoints(double x1, double y1, double x2, double y2) {
double x = x2 - x1;
double y = y2 - y1;
return (Math.pow(x, 2) + Math.pow(y, 2));
}
public static boolean isPerfectSquareByUsingSqrt(long n) {
if (n <= 0) {
return false;
}
double squareRoot = Math.sqrt(n);
long tst = (long) (squareRoot + 0.5);
return tst * tst == n;
}
private static double[] midOfaLine(double x1, double y1, double x2, double y2) {
double[] mid = new double[2];
mid[0] = (x1 + x2) / 2;
mid[1] = (y1 + y2) / 2;
return mid;
}
private static long sumOfN(long n) {
return (n * (n + 1)) / 2;
}
private static long power(long x, long y, long p) {
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
/* Function to calculate x raised to the power y in O(logn)*/
static long power(long x, long y) {
long temp;
if (y == 0)
return 1l;
temp = power(x, y / 2);
if (y % 2 == 0)
return (temp * temp);
else
return (x * temp * temp);
}
private static StringBuilder reverseString(String s) {
StringBuilder sb = new StringBuilder(s);
int l = 0, r = sb.length() - 1;
while (l <= r) {
char ch = sb.charAt(l);
sb.setCharAt(l, sb.charAt(r));
sb.setCharAt(r, ch);
l++;
r--;
}
return sb;
}
private static void swap(List<Integer> li, int i, int j) {
int t = li.get(i);
li.set(i, li.get(j));
li.set(j, t);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static String decimalToString(int x) {
return Integer.toBinaryString(x);
}
private static String decimalToString(long x) {
return Long.toBinaryString(x);
}
private static boolean isPallindrome(String s, int l, int r) {
while (l < r) {
if (s.charAt(l) != s.charAt(r)) return false;
l++;
r--;
}
return true;
}
private static boolean isSubsequence(String s, String t) {
if (s == null || t == null) return false;
Map<Character, List<Integer>> map = new HashMap<>(); //<character, index>
//preprocess t
for (int i = 0; i < t.length(); i++) {
char curr = t.charAt(i);
if (!map.containsKey(curr)) {
map.put(curr, new ArrayList<Integer>());
}
map.get(curr).add(i);
}
int prev = -1; //index of previous character
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.get(c) == null) {
return false;
} else {
List<Integer> list = map.get(c);
prev = lessThen(list, prev, false, 0, list.size() - 1);
if (prev == -1) {
return false;
}
prev++;
}
}
return true;
}
private static StringBuilder removeLeadingZero(StringBuilder sb) {
int i = 0;
while (i < sb.length() && sb.charAt(i) == '0') i++;
// debug("remove "+i);
if (i == sb.length()) return new StringBuilder();
return new StringBuilder(sb.substring(i, sb.length()));
}
private static StringBuilder removeEndingZero(StringBuilder sb) {
int i = sb.length() - 1;
while (i >= 0 && sb.charAt(i) == '0') i--;
// debug("remove "+i);
if (i < 0) return new StringBuilder();
return new StringBuilder(sb.substring(0, i + 1));
}
private static int stringToDecimal(String binaryString) {
// debug(decimalToString(n<<1));
return Integer.parseInt(binaryString, 2);
}
private static int stringToInt(String s) {
return Integer.parseInt(s);
}
private static String toString(long val) {
return String.valueOf(val);
}
private static void debug(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(long[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(List<int[]> arr) {
for (int[] a : arr) {
err.println(Arrays.toString(a));
}
}
private static void debug(float[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(double[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(boolean[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void print(String s) throws IOException {
out.println(s);
}
private static void debug(String s) throws IOException {
err.println(s);
}
private static int charToIntS(char c) {
return ((((int) (c - '0')) % 48));
}
private static void print(double s) throws IOException {
out.println(s);
}
private static void debug(char[] s1) throws IOException {
debug(Arrays.toString(s1));
}
private static void print(float s) throws IOException {
out.println(s);
}
private static void print(long s) throws IOException {
out.println(s);
}
private static void print(int s) throws IOException {
out.println(s);
}
private static void debug(double s) throws IOException {
err.println(s);
}
private static void debug(float s) throws IOException {
err.println(s);
}
private static void debug(long s) {
err.println(s);
}
private static void debug(int s) {
err.println(s);
}
private static boolean isPrime(long n) {
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
private static List<List<Integer>> readUndirectedGraph(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readUndirectedGraph(int[][] intervals, int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < intervals.length; i++) {
int x = intervals[i][0];
int y = intervals[i][1];
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int[][] intervals, int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < intervals.length; i++) {
int x = intervals[i][0];
int y = intervals[i][1];
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
static String[] readStringArray(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.next();
}
return arr;
}
private static Map<Character, Integer> freq(String s) {
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
return map;
}
private static Map<Long, Integer> freq(long[] arr) {
Map<Long, Integer> map = new HashMap<>();
for (long x : arr) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
return map;
}
private static Map<Integer, Integer> freq(int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
for (int x : arr) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
return map;
}
static boolean[] sieveOfEratosthenes(long n) {
boolean prime[] = new boolean[(int) n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static int[] sieveOfEratosthenesInt(long n) {
boolean prime[] = new boolean[(int) n + 1];
Set<Integer> li = new HashSet<>();
for (int i = 1; i <= n; i++) {
prime[i] = true;
li.add(i);
}
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) {
li.remove(i);
prime[i] = false;
}
}
}
int[] arr = new int[li.size()];
int i = 0;
for (int x : li) {
arr[i++] = x;
}
return arr;
}
static boolean isMemberAC(int a, int d, int x) {
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
static boolean isMemberAC(long a, long d, long x) {
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> li = new ArrayList<>();
for (int x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(int[] arr) {
int n = arr.length;
List<Integer> li = new ArrayList<>();
for (int x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sort(double[] arr) {
int n = arr.length;
List<Double> li = new ArrayList<>();
for (double x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(double[] arr) {
int n = arr.length;
List<Double> li = new ArrayList<>();
for (double x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(long[] arr) {
int n = arr.length;
List<Long> li = new ArrayList<>();
for (long x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> li = new ArrayList<>();
for (long x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static long sum(int[] arr) {
long sum = 0;
for (int x : arr) {
sum += x;
}
return sum;
}
private static long sum(long[] arr) {
long sum = 0;
for (long x : arr) {
sum += x;
}
return sum;
}
private static void initializeIO() {
try {
System.setIn(new FileInputStream("input"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.setErr(new PrintStream(new FileOutputStream("error.txt")));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr) {
int max = Integer.MIN_VALUE;
for (int x : arr) {
max = Math.max(max, x);
}
return max;
}
private static long maxOfArray(long[] arr) {
long max = Long.MIN_VALUE;
for (long x : arr) {
max = Math.max(max, x);
}
return max;
}
private static int[][] readIntIntervals(int n, int m) {
int[][] arr = new int[n][m];
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
arr[j][i] = sc.nextInt();
}
}
return arr;
}
private static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
private static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextDouble();
}
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
return arr;
}
private static void debug(String[] arr) {
err.println(Arrays.toString(arr));
}
private static void debug(Boolean[][] arr) {
for (int i = 0; i < arr.length; i++) err.println(Arrays.toString(arr[i]));
}
private static void debug(int[] arr) {
err.println(Arrays.toString(arr));
}
private static void debug(long[] arr) {
err.println(Arrays.toString(arr));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
BufferedWriter bw;
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
FastWriter() {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
<T> void print(T obj) throws IOException {
bw.write(obj.toString());
bw.flush();
}
void println() throws IOException {
print("\n");
}
<T> void println(T obj) throws IOException {
print(obj.toString() + "\n");
}
void print(int[] arr) throws IOException {
for (int x : arr) {
print(x + " ");
}
println();
}
void print(long[] arr) throws IOException {
for (long x : arr) {
print(x + " ");
}
println();
}
void print(double[] arr) throws IOException {
for (double x : arr) {
print(x + " ");
}
println();
}
void printCharN(char c, int n) throws IOException {
for (int i = 0; i < n; i++) {
print(c);
}
}
}
static class Dsu {
int[] parent, size;
Dsu(int n) {
parent = new int[n + 1];
size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int findParent(int u) {
if (parent[u] == u) return u;
return parent[u] = findParent(parent[u]);
}
private boolean union(int u, int v) {
// System.out.println("uf "+u+" "+v);
int pu = findParent(u);
// System.out.println("uf2 "+pu+" "+v);
int pv = findParent(v);
// System.out.println("uf3 " + u + " " + pv);
if (pu == pv) return false;
if (size[pu] <= size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
return true;
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 45caaa50e535f747236fae965bebe450 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class Aaab {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int antal = input.nextInt();
while(antal-- > 0){
String str = input.next();
boolean ok = false;
if(str.charAt(str.length() - 1) == 'B')
ok = true;
int sum = 0;
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i) == 'A')
sum++;
else
sum--;
if(sum < 0){
ok = false;
}
}
if(ok)
{System.out.println("yes");}
else
{
System.out.println("No");
}
}
input.close();
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | b27a9ff76376dd7ac9ec8c40e7c6140a | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class ArrayShuffling {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String n = scanner.nextLine();
for (int c = 0; c < Integer.parseInt(n); c++) {
String textList = scanner.nextLine();
// String[] textList = text.split(" ");
// List<String> textList = Arrays.asList(numbersArray);
int A = 0;
int B = 0;
if(textList.length() > 1 && textList.charAt(textList.length()-1) != 'A') {
for (int i = 0; i < textList.length(); i++) {
if(textList.charAt(i) == 'A') {
A++;
}else if(textList.charAt(i) == 'B') {
B++;
}
if(B>A) {
System.out.println("NO");
break;
}
}
if(B<=A) {
System.out.println("Yes");
}
}else {
System.out.println("NO");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 16a1761b0a072d690dda2c199013c6f6 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class A1 {
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.nextLine();
if (!s.contains("A") || !s.contains("B") || s.charAt(0) == 'B' || s.charAt(s.length() - 1) == 'A')
pw.println("NO");
else {
int countA = 0;
int countB = 0;
boolean f = true;
for (int i = 0; i < s.length() && f; i++) {
if (s.charAt(i) == 'A')
countA++;
else if (s.charAt(i) == 'B')
countB++;
if (countB > countA)
f = false;
}
pw.println(f ? "YES" : "NO");
}
}
pw.flush();
}
// public static long fibo(int n) {
// if (n == 0)
// return 0;
// if (n == 1)
// return 1;
// if (memo[n] != -1)
// return memo[n];
// return memo[n] = fibo(n - 1) + fibo(n - 2);
// }
// isPrime
// public static boolean isPrime(long n) {
// if (n <= 1)
// return false;
// if (n <= 3)
// return true;
// if (n % 2 == 0 || n % 3 == 0)
// return false;
// for (long i = 5; i * i <= n; i += 6) {
// if (n % i == 0 || n % (i + 2) == 0)
// return false;
// }
// return true;
// }
// get the nth fibonacci number using BigInteger
// public static BigInteger fib(int n) {
// BigInteger a = BigInteger.valueOf(0);
// BigInteger b = BigInteger.valueOf(1);
// for (int i = 0; i < n; i++) {
// BigInteger c = a.add(b);
// a = b;
// b = c;
// }
// return b;
// }
// // isPrime using BigInteger
// public static boolean isPrime(BigInteger n) {
// if (n.compareTo(BigInteger.valueOf(2)) < 0)
// return false;
// if (n.compareTo(BigInteger.valueOf(2)) == 0)
// return true;
// if (n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0)
// return false;
// BigInteger d = n.subtract(BigInteger.valueOf(1));
// BigInteger s = d.divide(BigInteger.valueOf(2));
// while (s.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0) {
// s = s.divide(BigInteger.valueOf(2));
// }
// for (BigInteger i = BigInteger.valueOf(2); i.compareTo(s) <= 0; i =
// i.add(BigInteger.valueOf(1))) {
// if (n.mod(i).compareTo(BigInteger.valueOf(0)) == 0)
// return false;
// }
// return true;
// }
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | e6e16cfd2c9c682ad8387668d8e2a3c4 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String backslash = sc.nextLine();
while(t-- > 0) {
String str = sc.nextLine();
if(good_string_possible(str)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
//for testing
// for(int i=0; i<100; i++) {
// String str = Integer.toBinaryString(i);
// str = str.replace('0', 'A');
// str = str.replace('1', 'B');
// str = 'A' + str;
// if(!good_string_possible(str)) {
// System.out.println(str + " not possible");
// } else {
// System.out.println(str + " possible");
// }
// }
}
public static boolean good_string_possible(String str) {
//last character is A
if(str.charAt(str.length() - 1) == 'A') {
return false;
}
//first character is B
if(str.charAt(0) == 'B') {
return false;
}
int countb = 0;
int counta = 0;
// for(int i=0; i<str.length(); i++) {
// if(str.charAt(i) == 'A') {
// if(countb > 0) {
// if(counta < countb) {
// return false;
// } else {
// counta = 0;
// countb = 0;
// }
// }
//
// counta++;
// } else {
// countb++;
// }
// }
// if(countb > 0) {
// if(counta < countb) {
// return false;
// }
// }
for(int i=0; i<str.length(); i++) {
if(str.charAt(i) == 'A') {
counta++;
} else {
countb++;
if(counta < countb) {
return false;
}
}
}
return counta >= countb;
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 6407b94f7f7cbb948f61cf98f66d1e9e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class calculate{
public static void main(String []args) throws IOException{
// FastReader sc=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
String s=sc.next();
int count=0;//int flag=0;
int count2=0;
int flag=0;
if(s.length()>1) {
if((s.charAt(0))=='B')
{
flag=1;
}
if((s.charAt(s.length()-1))=='A')
{
flag=1;
}
for(int i=0;i<s.length();i++) {
if (s.charAt(i) == 'A') {
count++;
}
else
{
count2++;
}
if(count2>count)
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
}
}
else
{
System.out.println("NO");
}
}
}
//Sort(a);
static final Random ra = new Random();
static void Sort(int arr[]) {
int n = arr.length;
for(int i = 0; i < n; i++) {
int j = ra.nextInt(n);
int temp =(int) arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
public static int[] swap(int arr[],int x,int y)
{
int temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
return arr;
}
//fast IO
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 43a7ac1ea9ce2315bfeb7b6275f80018 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static FastReader sc = new FastReader();
public static void main (String[] args) throws java.lang.Exception
{
int t = sc.nextInt();
while(t-->0){
solve(sc);
}
}
public static void solve(FastReader sc) throws IOException{
String s = s();
int n = s.length();
if(n==1 || s.charAt(0) == 'B' || s.charAt(n-1) == 'A'){
out.println("NO");
out.flush();return;
}
Stack<Character> st = new Stack<>();
for(int i = 0;i<n;++i){
if(s.charAt(i) == 'B'){
if(st.size()==0 || st.peek() == 'B'){
out.println("NO");
out.flush();return;
}else{
st.pop();
}
}else{
st.push('A');
}
}
out.println("YES");
out.flush();
}
/*
int [] arr = new int[n];
for(int i = 0;i<n;++i){
arr[i] = sc.nextInt();
}
*/
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static class Pair implements Comparable<Pair>{
int ind;int val;
Pair(int ind, int val){
this.ind=ind;
this.val=val;
}
public int compareTo(Pair o){
return this.val-o.val;
}
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | fac0e7435dec08d8cffa43f51db4a112 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
public class contest{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T= sc.nextInt();
for(int k=0;k<T;k++){
String s2=sc.next();
int l = s2.length();
int countA=0;
int countB=0;
int count=0;
for(int i=0;i<l;i++){
if(s2.charAt(i)==66){
countB++;
if(countA>=countB){
count++;
}
}
else{
countA++;
}
}
if(count==countB&& l>=2 && s2.charAt(l-1)==66){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 21c69d275fbc0af5dd0b270eee7b2cdd | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static ArrayList<Integer> prime = new ArrayList<>();
static long mod = 1000000007;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
int testCases = in.nextInt();
while ( testCases-- > 0 ) {
String s = in.next();
String ans = "YES";
int a = 0, b = 0;
for ( int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if ( ch == 'B' ) {
if ( a == 0 ) {
ans = "NO";
break;
} else a--;
} else a++;
}
if ( ans == "YES" && s.charAt(s.length() - 1) != 'B') ans = "NO";
out.println(ans);
}
out.close();
} catch (Exception e) {
return;
}
}
public static long pow( long k , long n ) {
long ans = 1;
while ( n > 0 ) {
if ( n % 2 == 1 ) {
ans += (ans * k) % mod;
n--;
} else {
k = (k * k) % mod;
n /= 2;
}
}
return ans % mod;
}
public static long fact(long n ) {
long ans = 0;
for ( long i = 1; i < n; ++i) {
ans += i;
}
return ans;
}
public static boolean isValid( long mid, long arr[] , long k ) {
long req = 0;
for ( int i = 1; i < arr.length; ++i) {
req += Math.min( (arr[i] - arr[i - 1] ) , mid );
}
req += mid;
if ( req >= k ) return true;
return false;
}
public static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
// private static void reverse( ArrayList<Integer> arr , int i, int j ) {
// while ( i <= j ) {
// swap( arr, i, j);
// i++; j--;
// }
// }
private static void swap( long arr[] , int i, int j ) {
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static boolean isPrime(long n ) {
if ( n == 1 ) return true;
for ( int i = 2; i <= Math.sqrt(n); i++) {
if ( n % i == 0 ) {
return false ;
}
}
return true;
}
private static boolean[] sieve() {
int n = 100000000;
boolean sieve[] = new boolean[n + 1];
Arrays.fill(sieve, true);
for ( int i = 2; i * i <= n; i++) {
for ( int j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
return sieve;
}
private static ArrayList<Integer> generatePrimes(int n ) {
boolean sieve[] = sieve();
for ( int i = 2; i <= n; i++) {
if ( sieve[i] == true ) {
prime.add(i);
}
}
return prime;
}
private static void segmentedSieve( int l , int r ) {
int n = (int) Math.sqrt(r);
ArrayList<Integer> pr = generatePrimes(n);
int dummy[] = new int[r - l + 1];
Arrays.fill(dummy, 1);
for ( int p : pr ) {
int firstMultiple = (l / p) * p;
if ( firstMultiple < l ) firstMultiple += p;
for ( int j = Math.max(firstMultiple, p * p); j <= r; j += p) {
dummy[j - l] = 0;
}
}
for ( int i = l; i <= r; i++) {
if ( dummy[i - l] == 1 ) {
System.out.println(i);
}
}
}
private static int[] primeFactors() {
int n = 1000000;
int prime[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
prime[i] = i;
}
for (int i = 2; i * i <= n; i++) {
if ( prime[i] == i ) {
for (int j = i * i; j <= n; j += i) {
if ( prime[j] == j ) {
prime[j] = i;
}
}
}
}
return prime;
}
private static boolean isPalindrome(String s ) {
int i = 0, j = s.length() - 1;
while ( i <= j ) {
if ( s.charAt(i) != s.charAt(j) ) {
return false;
}
i++; j--;
}
return true;
}
private static long power( long a , long b ) {
long ans = 1;
while ( b > 0 ) {
if ( (b & 1) != 0 ) {
ans = binMultiply(ans, a);
}
a = binMultiply(a, a );
b >>= 1;
}
return ans;
}
private static int GCD ( int a , int b ) {
if ( b == 0) return a;
return GCD( b , a % b);
}
private static long binMultiply(long a , long b ) {
long ans = 0;
while ( b > 0 ) {
if ( (b & 1) != 0 ) {
ans = (ans + a ); // if m is given in ques than use ans = ans+a % m ;
}
a = (a + a); // if m is given in ques than use a = (a+a)%m;
b >>= 1;
}
return ans;
}
private static int binarySearch(int l , int r , int[] arr , int find ) {
int mid = l + (r - l) / 2;
if ( arr[mid] == find ) {
return mid;
} else if ( arr[mid] > find ) {
return binarySearch(l, mid - 1, arr, find);
}
return binarySearch(mid + 1, r, arr, find);
}
private static int upper_bound(ArrayList<Integer> arr , int element ) {
int l = 0 ;
int h = arr.size();
int mid = 0;
while ( h - l > 0 ) {
mid = l + (h - l) / 2;
if ( arr.get(mid) <= element ) {
l = mid + 1;
} else {
h = mid ;
}
}
if ( arr.get(l) > element ) return l;
if ( arr.get(h) > element ) return h;
return -1;
}
private static int lower_bound(ArrayList<Integer> arr , int element ) {
int l = 0 ;
int h = arr.size();
int mid = 0;
while ( h - l > 0 ) {
mid = l + (h - l) / 2;
if ( arr.get(mid) < element ) {
l = mid + 1;
} else {
h = mid ;
}
}
if ( arr.get(l) >= element ) return l;
if ( arr.get(h) >= element ) return h;
return -1;
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 6ed19bd2fe9bbde376be6f8384e75a86 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class CodeForces {
public static void main(String[] args) throws FileNotFoundException {
FastScanner fs = new FastScanner();
int tt = fs.nextInt();
while(tt-- > 0) {
String a = fs.next();
boolean flag = true;
int val = 0;
if(a.charAt(a.length() - 1) == 'A' || a.charAt(0) == 'B') flag = false;
Stack<Character> stack = new Stack<>();
for(int i = 0; i < a.length(); i++) {
if(a.charAt(i) == 'A') {
val++;
}else {
val--;
}
if(val < 0) flag = false;
}
if(flag) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
public static int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b, a%b);
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for(int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | ea298d422965302633bc77ebc0eee026 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
public class E1672B {
public static void main(String[] args) {
FastIO io = new FastIO();
int t = io.nextInt();
while (t-- > 0) {
char[] s = io.next().toCharArray();
int score = 0;
for (int i = s.length - 1; i >= 0; i--) {
if (s[i] == 'B') score++;
else score = Math.max(0, score - 1);
}
io.println(score == 0 && s.length > 1 && s[s.length - 1] == 'B' ? "YES" : "NO");
}
io.close();
}
private static class FastIO extends PrintWriter {
private final InputStream stream;
private final byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 600b1a301a8b40518dfadb4373b95559 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;
public class theatre {
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String next() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) array[i] = nextLong();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[] ) throws Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int t=in.nextInt();
while(t-->0)
{
String s=in.next();
boolean p=true;
int n=s.length();
int a=0,b=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='B')
{
if(a==0)
{
p=false;
break;
}
else
{
a--;
}
}
else
{
a++;
}
}
if(p==true &&s.charAt(n-1)=='B')
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
// if(s.length()<2)
// {
// p=false;
// }
// else
// {
// s=s.replaceAll("AB", "");
//
// if(s.contains("B"))
// {
// p=false;
// }
// }
// if(p==true)
// {
// System.out.println("YES");
// }
// else
// {
// System.out.println("NO");
// }
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | da58fb976f14a7154dbd058e7190d233 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
String str=sc.next();
if(str.length()<2||str.charAt(0)=='B'||str.charAt(str.length()-1)!='B'){
System.out.println("NO");
continue;
}
int B=0,A=0,check=0;
for(int j=0;j<str.length();j++){
if(str.charAt(j)=='A'){
A++;
}
if(str.charAt(j)=='B'){
B++;
}
if(B>A){
check=1;
break;
}
// if(str.charAt(j)=='B'&&str.charAt(j+1)=='A'){
// if(B>A){
// check=1;
// break;
// }else{
// A=0;
// B=0;
// }
// }
}
// B++;
// if(B>A){
// check=1;
// }
if(check==1){
System.out.println("NO");
}
else{
System.out.println("YES");
}
}
// your code goes here
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 446855b3f5f3c3093272204ee553b6ca | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t--!=0) {
int countA = 0, countB = 0;
//String s1 = "";
boolean found = false;
String s2 = sc.nextLine();
if(s2.length()<2) {
System.out.println("NO");
}
else if(s2.startsWith("A") && s2.endsWith("B")) {
for(int i = 0; i<s2.length(); ++i) {
if(s2.charAt(i)=='A') {
++countA;
}else {
++countB;
countA = countA-countB;
--countB;
}
if(countB>countA) {
System.out.println("NO");
found = true;
break;
}
}
if(found) {
continue;
}
if(countA>=countB) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}else {
System.out.println("NO");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | ee5d1172aa25945aa452e4b6ac5a21c5 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class G {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder("");
static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
int t = fs.nextInt();
for (int tt = 0; tt < t; tt++) {
String s = fs.next();
int n = s.length();
if (n == 1) sb.append("NO\n");
else {
int cnt = 1;
boolean ok = true;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'A' && ok) ok = false;
if (s.charAt(i) == 'B' && ok) {
ok = false;
break;
}
if (!ok) {
if (s.charAt(i) == 'B') ok = true;
}
}
Stack<Character> stk = new Stack<>();
boolean ok2 = true;
if (!ok) {
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'A') stk.add('A');
if (s.charAt(i) == 'B' && !stk.isEmpty()) stk.pop();
else if (s.charAt(i) == 'B' && stk.isEmpty()) {
ok2 = false;
break;
}
}
if (stk.size()>0 && s.charAt(n-1) != 'B') ok2 = false;
}
sb.append(ok || ok2 ? "YES\n" : "NO\n");
}
}
pw.print(sb.toString());
pw.close();
}
static void sort(int[] a) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a)
al.add(i);
Collections.sort(al);
for (int i = 0; i < a.length; i++)
a[i] = al.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {while (!st.hasMoreTokens())try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() {return Double.parseDouble(next());}
int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) {a[i] = nextInt();} return a;}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 787994ae090697d11ad9eb54a1b7fa71 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.math.BigInteger;
import java.util.*;
import static java.lang.System.out;
import static java.lang.System.setProperty;
public class Round_780_Div_3 {
static Scanner str = new Scanner(System.in);
static ArrayList<Integer> list;
final int mod = 1000000007;
public static void main(String[] args) {
long T = str.nextLong();
while (T-- > 0) {
solve();
}
}
static void solve() {
String s = str.next();
if (s.length() < 2 || s.charAt(0) != 'A' || s.charAt(s.length() - 1) != 'B') {
out.println("NO");
return;
}
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'A'){
cnt++;
}
if(s.charAt(i) == 'B'){
cnt--;
}
if(cnt < 0){
out.println("NO");
return;
}
}
out.println("YES");
}
static boolean check(String s) {
if (checka(s) && checkb(s)) {
return true;
}
return false;
}
static boolean checka(String s) {
String a[] = s.split("AB");
for (int i = 0; i < a.length; i++) {
if (a[i].equals("B")) {
return false;
}
}
return true;
}
static boolean checkb(String s) {
String a[] = s.split("A");
int cnt = 0;
for (int i = 0; i < a.length; i++) {
if (a[i].equals("")) {
cnt++;
}
}
if(cnt == s.length()){
return false;
}
return true;
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 811bf0ce802496e68a18bd69b5504c2e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
public class template {
static class QuickReader
{
BufferedReader in;
StringTokenizer token;
public QuickReader(InputStream ins)
{
in=new BufferedReader(new InputStreamReader(ins));
token=new StringTokenizer("");
}
public boolean hasNext()
{
while (!token.hasMoreTokens())
{
try
{
String s = in.readLine();
if (s == null) return false;
token = new StringTokenizer(s);
} catch (IOException e)
{
throw new InputMismatchException();
}
}
return true;
}
public String next()
{
hasNext();
return token.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public int[] nextInts(int n)
{
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] nextLongs(int n)
{
long[] res = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException ignored) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
private QuickReader sc;
private PrintWriter ptk;
public template(QuickReader sc, PrintWriter ptk) {
this.sc = sc;
this.ptk = ptk;
}
public static void main(String[] args) {
QuickReader in = new QuickReader(System.in);
try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
{
new template(in, out).solve();
}
}
public static String sortString(String inputString)
{
// Converting input string to character array
char tempArray[] = inputString.toCharArray();
// Sorting temp array using
Arrays.sort(tempArray);
// Returning new sorted string
return new String(tempArray);
}
static int mostFrequent(int arr[], int n)
{
// Sort the array
Arrays.sort(arr);
// find the max frequency using linear
// traversal
int max_count = 1, res = arr[0];
int curr_count = 1;
for (int i = 1; i < n; i++)
{
if (arr[i] == arr[i - 1])
curr_count++;
else
{
if (curr_count > max_count)
{
max_count = curr_count;
res = arr[i - 1];
}
curr_count = 1;
}
}
// If last element is most frequent
if (curr_count > max_count)
{
max_count = curr_count;
res = arr[n - 1];
}
return res;
}
public int maxFrequency(int[] nums, int k) {
int l = 0;
int r = 0;
int maxWin = 0;
long totalSum = 0;
//sorting is needed to get relation between consecutive elements
Arrays.sort(nums);
while (r < nums.length) {
totalSum = totalSum + nums[r];
//when this condition occurs means we have to increase left pointer which will reduce our window
while ((long) nums[r] * (r - l + 1) > totalSum + k) {
totalSum = totalSum - nums[l];
l++;
}
maxWin = Math.max(maxWin, r - l + 1);
r++;
}
return maxWin;
}
public static void countFrequencies(ArrayList<String> list)
{
TreeMap<String, Integer> tmap = new TreeMap<String, Integer>();
for (String t : list) {
Integer c = tmap.get(t);
tmap.put(t, (c == null) ? 1 : c + 1);
}
for (Map.Entry m : tmap.entrySet())
System.out.println("Frequency of " + m.getKey() + " is " + m.getValue());
}
static int countPairs(long A1[], long A2[] , int n1, int n2, long K)
{
// Initialize pairs to 0
int res = 0;
// create map of elements of array A1
Map<Long, Long> m = new HashMap<Long, Long> ();
for (int i = 0; i < n1; ++i)
{
if(m.containsKey(A1[i]))
m.put(A1[i], m.get(A1[i]) + 1);
else
m.put(A1[i], 1L);
}
// count total pairs
for (int i = 0; i < n2; ++i) {
long temp = K - A2[i];
if (m.containsKey(temp) && m.get(temp) != 0) {
res++;
// Every element can be part
// of at most one pair.
m.put(temp, m.get(A1[i]) - 1);
}
}
// return total pairs
return res;
}
static long getPairsCount(long[] arr, int n, double k)
{
HashMap<Long,Long> m = new HashMap<>();
long count = 0;
for (int i = 0; i < n; i++) {
if (m.containsKey((long)(k - arr[i]))) {
count += m.get((long)(k - arr[i]));
}
if(m.containsKey(arr[i])){
m.put(arr[i], m.get(arr[i])+1);
}
else{
m.put(arr[i], 1L);
}
}
return count;
}
public static void maxPairs(
long[] nums, double k)
{
// Initialize a hashmap
Map<Long, Long> map
= new HashMap<>();
// Store the final result
int result = 0;
// Iterate over the array nums[]
for (long i : nums) {
// Decrement its frequency
// in map and increment
// the result by 1
if (map.containsKey(i) &&
map.get(i) > 0)
{
map.put(i, map.get(i) - 1);
result++;
}
// Increment its frequency by 1
// if it is already present in map.
// Otherwise, set its frequency to 1
else
{
map.put((long) (k - i),
map.getOrDefault((long)(k - i), 0L) + 1);
}
}
// Print the result
System.out.println(result);
}
static int countTriplets(int A[], int N)
{
// Stores the count
int ans = 0;
// Map to store frequency
// of array elements
HashMap<Integer,
Integer> map = new HashMap<Integer,
Integer>();
for(int j = N - 2; j >= 1; j--)
{
// Increment the frequency
// of A[j+1] as it can be
// a valid A[k]
if(map.containsKey(A[j + 1]))
map.put(A[j + 1], map.get(A[j + 1]) + 1);
else
map.put(A[j + 1], 1);
for(int i = 0; i < N; i++)
{
int target = A[i] * A[j];
// If target exists in the map
if (map.containsKey(target))
ans += map.get(target);
}
}
// Return the final count
return ans;
}
public void solve() {
int tt=sc.nextInt();
while (tt-->0){
String s=sc.next();
int b=0;
boolean boo=true;
for (int i = 0; i <s.length(); i++) {
if (s.charAt(i)=='B'){
b++;
}
}
int a=0;
for (int i = 0; i <s.length(); i++) {
if (s.charAt(i)=='A'){
a++;
}
}
boolean k=true;
int a1=0;
int b1=0;
for (int i = 0; i <s.length() ; i++) {
if (s.charAt(i)=='A'){
a1++;
}else {b1++;}
if (a1<b1){
k=false;
}
}
if (s.charAt(s.length()-1)!='B'){
k=false;
}
if (k){
System.out.println("YES");
}else System.out.println("No");
}
}
static long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
private static long gcd(long a, long b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 2630ae18ee01a76aa429edf7ee93e5f7 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class B {
//java -Xss515m Solution.java < input.txt
private static final String SPACE = "\\s+";
private static final int MOD = 1_000_000_007;
private static final Reader in = new Reader();
public static void main(String[] args) throws IOException {
int tt = in.nextInt();
while (tt-- > 0) {
String s = in.nextString();
solve(s);
}
}
private static void solve(String s) throws IOException {
if (s.charAt(s.length() - 1) != 'B' || s.charAt(0) != 'A') {
System.out.println("NO");
return;
}
int numA = 0;
for (char c : s.toCharArray()) {
if (c == 'A') {
numA++;
} else {
if (numA <= 0) {
System.out.println("NO");
return;
}
numA--;
}
}
System.out.println("YES");
}
// Utility functions
private static void shuffleSort(int[] nums) {
shuffle(nums);
Arrays.sort(nums);
}
private static void shuffleSort(long[] nums) {
shuffle(nums);
Arrays.sort(nums);
}
private static void swap(int a, int b, int[] nums) {
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
private static void swap(int a, int b, long[] nums) {
long temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
private static void shuffle(int[] nums) {
Random random = ThreadLocalRandom.current();
for (int i = nums.length - 1; i > 0; i--) {
swap(random.nextInt(i), i, nums);
}
}
private static void shuffle(long[] nums) {
Random random = ThreadLocalRandom.current();
for (int i = nums.length - 1; i > 0; i--) {
swap(random.nextInt(i), i, nums);
}
}
private static long pow(int base, int exp) {
long res = 1;
while (exp-- > 0) {
res *= base;
}
return res;
}
private static long pow(int base, int exp, Long[][] memo) {
if (exp == 0) return 1;
if (memo[base][exp] != null) return memo[base][exp];
return memo[base][exp] = base * pow(base, exp - 1, memo) % MOD;
}
private static class Reader {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int index = 0;
String[] tokens = {};
public int nextInt() throws IOException {
read();
return Integer.parseInt(tokens[index++]);
}
public long nextLong() throws IOException {
read();
return Long.parseLong(tokens[index++]);
}
public String nextString() throws IOException {
read();
return tokens[index++];
}
public int[] nextInts() throws IOException {
return Arrays.stream(in.readLine().split(SPACE)).mapToInt(Integer::parseInt).toArray();
}
public long[] nextLongs() throws IOException {
return Arrays.stream(in.readLine().split(SPACE)).mapToLong(Long::parseLong).toArray();
}
public String[] nextStrings() throws IOException {
return in.readLine().split(SPACE);
}
private void read() throws IOException {
if (index >= tokens.length) {
tokens = in.readLine().split(SPACE);
index = 0;
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 253baeda3c0c40df4de208dabaabab7b | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Codeforces {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int k = 0 ; k < t ;k++){
String s = sc.next();
int flag = 0;
int n = s.length();
if(s.length() == 1){
System.out.println("NO");
continue;
}
if(s.charAt(n-1) != 'B'){
System.out.println("NO");
continue;
}
for(int i = 0 ;i < n ;i++){
if(s.charAt(i) == 'A'){
flag++;
}else{
flag--;
}
if(flag < 0){
System.out.println("NO");
break;
}
}
if(flag >= 0) {
System.out.println("YES");
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 9ac6f5250383f4f04660a4cf60a3bf8b | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Codechef
{
public static Scanner scan = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception {
int testCases = scan.nextInt();
for (int vedant = 0; vedant < testCases; vedant++) {
solve();
}
}
private static void solve() {
// Solution goes over here.
String str = scan.next();
boolean flag = str.charAt(0) == 'A' && str.charAt(str.length() - 1) == 'B';
int aCount = 0;
int bCount = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'A') {
aCount++;
} else {
if (aCount > bCount) {
bCount++;
} else {
flag = false;
break;
}
}
}
if (flag && aCount >= bCount) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
private static StringBuilder removeZero(String str1) {
StringBuilder str = new StringBuilder(str1);
int leftPointer = 0;
int rightPointer = str1.length() - 1;
while (leftPointer < rightPointer) {
if (str.charAt(leftPointer) != str.charAt(rightPointer)) {
if (str.charAt(leftPointer) == 0) {
str.deleteCharAt(leftPointer);
} else {
str.deleteCharAt(rightPointer);
rightPointer--;
}
} else {
leftPointer++;
rightPointer--;
}
}
return str;
}
private static StringBuilder removeOne(String str1) {
StringBuilder str = new StringBuilder(str1);
int leftPointer = 0;
int rightPointer = str1.length() - 1;
while (leftPointer < rightPointer) {
if (str.charAt(leftPointer) != str.charAt(rightPointer)) {
if (str.charAt(leftPointer) == 1) {
str.deleteCharAt(leftPointer);
} else {
str.deleteCharAt(rightPointer);
rightPointer--;
}
} else {
leftPointer++;
rightPointer--;
}
}
return str;
}
// abcadcadba
private static boolean isConsonants(char charAt) {
charAt = Character.toLowerCase(charAt);
if (charAt == 'a' ||
charAt == 'e' ||
charAt == 'i' ||
charAt == 'o' ||
charAt == 'u') {
return false;
} else {
return true;
}
}
private static long getMaximumFromList(List<Long> arr) {
long max = Long.MIN_VALUE;
for (int i = 0; i < arr.size(); i++) {
max = Math.max(max, arr.get(i));
}
return max;
}
public static int binarySearch(int[] arr, int target) {
int starting = 0;
int ending = arr.length - 1;
while (starting < ending) {
int mid = starting + (ending - starting) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
public static int binarySearch(long[] arr, long target) {
int starting = 0;
int ending = arr.length - 1;
while (starting < ending) {
int mid = starting + (ending - starting) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
public static int gcd(int a, int b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public static long gcd(long a, long b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 57e73e5132d1665b3baf91768b3469fc | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Solution{
public static void main(String[] args) {
TaskA solver = new TaskA();
int t = in.nextInt();
for (int i = 1; i <= t ; i++) {
solver.solve(i, in, out);
}
// solver.solve(1, in, out);
out.flush();
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
String s=in.next();
int ans=0;
for(int i=0;i<s.length();i++) {
if(s.charAt(i)=='A') {
ans++;
}
else {
ans--;
}
if(ans<0) {
println("NO");
return;
}
}
if(s.charAt(s.length()-1)!='B') {
println("NO");return;
}
println("YES");
}
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int len(long x) {
String s=String.valueOf(x);
return s.length();
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void printArr(int[]arr){
for(int i=0;i<arr.length;i++) {
print(arr[i]+" ");
}
print("\n");
}
static void printArr(long[]arr){
for(int i=0;i<arr.length;i++) {
print(arr[i]+" ");
}
print("\n");
}
static int[]input(int n){
int[]arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
}
return arr;
}
static int[]input(){
int n= in.nextInt();
int[]arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
}
return arr;
}
////////////////////////////////////////////////////////
static class Pair {
int first;
int second;
Pair(int x, int y)
{
this.first = x;
this.second = y;
}
}
static void sortS(Pair arr[])
{
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.second - p2.second;
}
});
}
static void sortF(Pair arr[])
{
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.first - p2.first;
}
});
}
/////////////////////////////////////////////////////////////
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
static void println(long c) {
out.println(c);
}
static void print(long c) {
out.print(c);
}
static void print(int c) {
out.print(c);
}
static void println(int x) {
out.println(x);
}
static void print(String s) {
out.print(s);
}
static void println(String s) {
out.println(s);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | fe3d2eaed21025b88bedfcd280fd4882 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class ILoveAAB {
public static boolean isValidString(String s) {
if(s.length() <= 1) return false;
int bCount = 0, aCount = 0;
for(int i=0;i<s.length();i++) {
if(s.charAt(i) == 'A') aCount += 1;
else bCount += 1;
if(bCount > aCount) {
// System.out.println(i + " " + aCount + " " + bCount);
return false; // Since if B count > A then the string cannot be accepted the valid patterns are AB, AAB, AAAB,...
}
}
return bCount > 0 && s.charAt(s.length()-1) == 'B';
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
try {
int t = s.nextInt();
for(int i=0;i<t;i++) {
String inp = s.next();
System.out.println(isValidString(inp)?"YES":"NO");
}
} catch(Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
} finally {
s.close();
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 2f1ebc21d42bbc3bacf39cc8aa9b4337 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class P1672B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String s = sc.next();
int bin1=0,bin2=0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A')
bin1++;
else
bin2++;
if(bin2>bin1)
break;
}
if (bin1 >= bin2 && s.charAt(0) == 'A' && s.charAt(s.length() - 1) == 'B')
System.out.println("YES");
else{
System.out.println("NO");
bin1 = bin2 = 0;
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 4ae5d11da0b0d881c7acd98f0937f958 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
import java.time.*;
import static java.lang.Math.*;
@SuppressWarnings("unused")
public class A {
static boolean DEBUG = false;
static Reader fs;
static PrintWriter pw;
static void solve() {
char a[] = fs.next().toCharArray();
int n = a.length;
boolean ans = true;
int count = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 'A')
count++;
else
count--;
ans &= count >= 0;
}
ans &= n > 1;
ans &= a[n-1] == 'B';
pw.println(ans ? "YES" : "NO");
}
public static void main(String[] args) throws IOException {
if (args.length == 2) {
System.setIn(new FileInputStream("D:\\program\\javaCPEclipse\\CodeForces\\src\\input.txt"));
System.setErr(new PrintStream("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt"));
DEBUG = true;
}
Instant start = Instant.now();
fs = new Reader();
pw = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
solve();
}
Instant end = Instant.now();
if (DEBUG) {
pw.println(Duration.between(start, end));
}
pw.close();
}
static void sort(int a[]) {
ArrayList<Integer> l = new ArrayList<Integer>();
for (int x : a)
l.add(x);
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}
public static void print(long a, long b, long c, PrintWriter pw) {
pw.println(a + " " + b + " " + c);
return;
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[][] read2Array(int n, int m) {
int a[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 53c8073c8a71e92e1887c875cb072d7d | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = Integer.parseInt(in.nextLine());
while (t-- > 0) {
char[] ch = in.next().toCharArray();
boolean ans = true;
int a = 0;
int b = 0;
if (ch[0] == 'B' || ch[ch.length - 1] == 'A') ans = false;
for (int i = 0; i < ch.length && ans; i++ ) {
if (ch[i] == 'A') a++;
else b++;
if (a < b) ans = false;
}
System.out.println(ans ? "YES" : "NO");
}
in.close();
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 4c46c19c73f93fab96a4ccd7e3a5a02e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
public void prayGod() throws IOException {
int t = nextInt();
while (t-- > 0) {
String s = next();
int ca = 0, cb = 0;
int n = s.length();
boolean possible = true;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'A')
ca++;
else
cb++;
if (ca < cb) {
possible = false;
break;
}
}
if (cb <= 0 || s.charAt(n - 1) == 'A')
possible = false;
printVerdict(possible);
}
}
public int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
public long binpow(int a, int b) {
if (b < 0)
return 0;
long ret = 1, curr = a;
while (b > 0) {
if (b % 2 == 1)
ret = (ret * curr) % mod;
b /= 2;
curr = (curr * curr) % mod;
}
return ret;
}
public void printVerdict(boolean verdict) {
if (verdict)
out.println(VERDICT_YES);
else
out.println(VERDICT_NO);
}
static final String VERDICT_YES = "YES";
static final String VERDICT_NO = "NO";
static final boolean RUN_TIMING = true;
static final boolean AUTOFLUSH = false;
static final boolean FILE_INPUT = false;
static final boolean FILE_OUTPUT = false;
static int iinf = 0x3f3f3f3f;
static long inf = (long) 1e18 + 10;
static long mod = (long) 998244353;
static long inv2 = (long) 499122177;
static char[] inputBuffer = new char[1 << 20];
static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), AUTOFLUSH);
// int data-type
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
public void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println();
}
// long data-type
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
public static void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println();
}
public void sort(long[] a) {
shuffle(a);
Arrays.sort(a);
}
// double data-type
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++)
arr[i] = nextDouble();
return arr;
}
public static void printArray(double[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println();
}
// Generic type
public <T> void sort(T[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T> void printArray(T[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println();
}
public String next() throws IOException {
int len = 0;
int c;
do {
c = in.read();
} while (Character.isWhitespace(c) && c != -1);
if (c == -1) {
throw new NoSuchElementException("Reached EOF");
}
do {
inputBuffer[len] = (char) c;
len++;
c = in.read();
} while (!Character.isWhitespace(c) && c != -1);
while (c != '\n' && Character.isWhitespace(c) && c != -1) {
c = in.read();
}
if (c != -1 && c != '\n') {
in.unread(c);
}
return new String(inputBuffer, 0, len);
}
public String nextLine() throws IOException {
int len = 0;
int c;
while ((c = in.read()) != '\n' && c != -1) {
if (c == '\r') {
continue;
}
inputBuffer[len] = (char) c;
len++;
}
return new String(inputBuffer, 0, len);
}
public boolean hasNext() throws IOException {
String line = nextLine();
if (line.isEmpty()) {
return false;
}
in.unread('\n');
in.unread(line.toCharArray());
return true;
}
public void shuffle(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int j = (int) (Math.random() * (n - i));
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public void shuffle(long[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int j = (int) (Math.random() * (n - i));
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public void shuffle(Object[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int j = (int) (Math.random() * (n - i));
Object temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public static void main(String[] args) throws IOException {
if (FILE_INPUT)
in = new PushbackReader(new BufferedReader(new FileReader(new File("output.txt"))), 1 << 20);
if (FILE_OUTPUT)
out = new PrintWriter(new FileWriter(new File("output.txt")));
long time = 0;
time -= System.nanoTime();
new B().prayGod();
time += System.nanoTime();
if (RUN_TIMING)
System.err.printf("%.3f ms%n", time / 1000000.0);
out.flush();
in.close();
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 370dd17527a529ec00ae35450f264f97 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.util.Scanner;
public class ILoveAAAB {
public static void main(String[] args) {
Scanner s=new Scanner (System.in);
int t=s.nextInt();
for (int i=0;i<t;i++)
{
int ans=0;
int h=1;
int x=0;
int y=0;
String a=s.next();
if (a.length()>=2&&a.charAt(a.length()-1)=='B'&&a.charAt(0)=='A')
{
// System.out.println("yyyyy");
for (int j=0;j<a.length();j++)
{
//System.out.println("j="+j);
if (a.charAt(j)=='A')
{
x++;
}
else y++;
if (y>x)
{
h=0;
break;
}
else h++;
}
if (h>0)
System.out.println("YES");
else System.out.println("NO");
}
else System.out.println("NO");
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 5221484ce3907ad18eda9b5caff29961 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.io.*;
import java.util.*;
public class Solution{
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int sib;
boolean inj;
Pair(int x, boolean y) {
sib=x;
inj=y;
}
}
static long mod = 1000000007;
static boolean ans= false;
static StringBuffer sb = new StringBuffer("");
public static void main(String[] args) throws Exception
{
//Read input from user
//Scanner scn = new Scanner(System.in);
FastReader scn = new FastReader();
int t = scn.nextInt();
while(t>0) {
String s = scn.next();
int n = s.length();
int c1=0,c2=0;
boolean b = true;
if(s.charAt(0)=='B') {
b=false;
System.out.println("NO");
t--;
continue;
}
for(int i=0;i<n;i++) {
if(s.charAt(i)=='A') {
c1++;
}else {
c2++;
}
if(c2>c1) {
b=false;
break;
}
}
if(s.charAt(n-1)!='B') {
b=false;
}
if(b==false) {
System.out.println("NO");
}else {
System.out.println("YES");
}
t--;
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c6f4cfaab9a242d7bcb6ef695d3ad67e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/*
Every B needs an A
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
long T = Integer.parseInt(reader.readLine().split("\\s")[0]);
for (int t = 0; t < T; t++) {
String s2 = reader.readLine().split("\\s")[0];
String answer = solve(s2);
System.out.println(answer);
}
reader.close();
}
public static String solve(String s2) {
long sum = 0;
int i;
for (i = 0; i < s2.length(); i++) {
if (s2.charAt(i) == 'A') {
sum += 1;
} else {
sum -= 1;
}
if (sum < 0) {
return "NO";
}
}
if (s2.charAt(i-1) == 'A') {
return "NO";
}
return "YES";
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 930e6657f96f534ccb6a71e1d7bed5fc | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Submit {
public static void main(String... strings) {
try (FastReader r = new FastReader()) {
int t = r.nextInt();
L: while (t-- > 0) {
String str = r.next();
int a = 0;
int b = 0;
if (!str.endsWith("B")) {
System.out.println("NO");
continue;
}
for (char c : str.toCharArray()) {
if (c == 'A') {
a++;
} else {
b++;
if (b > a) {
System.out.println("NO");
continue L;
}
}
}
System.out.println("YES");
}
}
}
}
class FastReader implements AutoCloseable {
private BufferedReader reader;
private StringTokenizer tokenizer;
public FastReader() {
this.reader = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreElements()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
public int nextInt() {
while (tokenizer == null || !tokenizer.hasMoreElements()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return Integer.parseInt(tokenizer.nextToken());
}
public String nextLine() {
String line = "";
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
public char nextChar() {
char character = ' ';
try {
character = (char) reader.read();
} catch (IOException ex) {
ex.printStackTrace();
}
return character;
}
@Override
public void close() {
try {
this.reader.close();
} catch (IOException closingEx) {
// ignore
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c6ec9626c60ecd37c04a3b9d68ebfdeb | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int trials = sc.nextInt();
while (trials > 0){
String s = sc.next();
if (s.length() == 1)
System.out.println("No");
else if (s.startsWith("B"))
System.out.println("No");
else {
if (s.endsWith("B")) {
int counterA = 0;
boolean flag = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A')
counterA++;
else if (s.charAt(i) == 'B'){
if (counterA==0){
flag = false;
break;
}
else counterA--;
}
}
if (flag)
System.out.println("Yes");
else System.out.println("No");
}
else System.out.println("No");
}
trials--;
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 0a1d0a5636f0831fcdee437e46c68970 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import javax.management.MBeanRegistration;
import javax.swing.Timer;
import java.io.*;
import java.util.*;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private FastWriter wr;
private Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
public Reader(String path) throws FileNotFoundException {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(path)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public int ni() throws IOException {
return rd.nextInt();
}
public void yn(boolean flag){
if(flag){
wr.println("YES");
}else {
wr.println("NO");
}
}
public long nl() throws IOException {
return rd.nextLong();
}
char nc() throws IOException {
return rd.next().charAt(0);
}
public String ns() throws IOException {
return rd.nextLine();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S, K> {
private F first;
private S second;
private K third;
Pair(F first, S second, K third) {
this.first = first;
this.second = second;
this.third = third;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<F, S, K> pair = (Pair<F, S, K>) o;
return first == pair.first && second == pair.second && third == pair.third;
}
@Override
public int hashCode() {
return Objects.hash(first, second, third);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader();
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(input.getPath());
wr = new FastWriter(output.getPath());
} else {
rd = new Reader();
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
boolean[] sieve;
public void solve() throws IOException {
int t = 1;
t = ni();
while (t-- > 0) {
go();
}
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public void go() throws IOException {
String st=ns();
int counterA=0;
boolean flag=true;
for(int i=0;i<st.length();i++){
if(st.charAt(i)=='A'){
counterA++;
}else {
counterA--;
}
if(counterA<0){
flag=false;
break;
}
}
yn(flag && st.charAt(st.length()-1)=='B');
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 3f0dca0c4ad4d949e5a265c6594ad3cd | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main
{
InputStream is;
PrintWriter out = new PrintWriter(System.out); ;
String INPUT = "";
void run() throws Exception
{
is = System.in;
solve();
out.flush();
out.close();
}
public static void main(String[] args) throws Exception { new Main().run(); }
public byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
public int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){ ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; }
public boolean isSpaceChar(int c)
{
return !(c >= 33 && c <= 126);
}
public int skip()
{
// continue; String; charAt; println(); ArrayList; Integer; Long;
// long; Queue; Deque; LinkedList; Pair; double; binarySearch;
// s.toCharArray; length(); length; getOrDefault; break;
// Map.Entry<Integer, Integer> e; HashMap; TreeMap; Character;
int b;
while((b = readByte()) != -1 && isSpaceChar(b));
return b;
}
public double nd()
{
return Double.parseDouble(ns());
}
public char nc()
{
return (char)skip();
}
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b)))
{
sb.appendCodePoint(b); b = readByte();
}
return sb.toString();
}
private int ni()
{
return (int)nl();
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-')
{
minus = true; b = readByte();
}
while(true)
{
if(b >= '0' && b <= '9')
{
num = num * 10 + (b - '0');
}
else { return minus ? -num : num; } b = readByte();
}
}
class Pair
{
int first;
int second;
Pair(int a, int b)
{
first = a;
second = b;
}
}
// KMP ALGORITHM
void KMPSearch(String pat, String txt) {
int M = pat.length(); int N = txt.length();
int lps[] = new int[M]; int j = 0;
computeLPSArray(pat, M, lps); int i = 0;
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
j = lps[j - 1];
}
else if (i < N && pat.charAt(j) != txt.charAt(i)) {
if (j != 0) j = lps[j - 1];
else i = i + 1;
}
}
}
void computeLPSArray(String pat, int M, int lps[]) {
int len = 0; int i = 1; lps[0] = 0;
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else {
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i] = len;
i++;
}
}
}
}
int FirstAndLastOccurrenceOfAnElement(int[] arr, int target, boolean findStartIndex)
{
int ans = -1;
int n = arr.length;
int low = 0; int high = n-1;
while(low <= high)
{
int mid = low + (high - low)/2;
if(arr[mid] > target) high = mid-1;
else if(arr[mid] < target) low = mid+1;
else
{
ans = mid;
if(findStartIndex) high = mid-1;
else low = mid+1;
}
}
return ans;
}
// Print All Subsequence.
static ArrayList<Integer> qwerty = new ArrayList<>();
void printAllSubsequence(int[] arr, int index, int n)
{
if(index >= n) {
for(int i=0; i<qwerty.size(); i++) out.print(qwerty.get(i) + " ");
if(qwerty.size() == 0) out.print(" "); out.println();
return;
}
qwerty.add(arr[index]); printAllSubsequence(arr, index+1, n);
qwerty.remove(qwerty.size()-1); printAllSubsequence(arr, index+1, n);
}
// Print All Subsequence.
// Print All Subsequence. with sum k.
static ArrayList<Integer> qwerty2 = new ArrayList<>();
void printSubsequenceWithSumK(int[] arr, int index, int K, int sum, int n)
{
if(index >= n) {
if(sum == K)
{
for(int i=0; i<qwerty2.size(); i++)
out.print(qwerty2.get(i) + " ");
out.println();
}
return;
}
qwerty2.add(arr[index]);
sum += arr[index];
printSubsequenceWithSumK(arr, index+1, K, sum, n);
sum -= arr[index];
qwerty2.remove(qwerty2.size()-1);
printSubsequenceWithSumK(arr, index+1, K, sum, n);
}
// Print All Subsequence. with sum k.
int[] na(int n)
{
int[] arr = new int[n];
for(int i=0; i<n; i++) arr[i]=ni();
return arr;
}
long[] nal(int n)
{
long[] arr = new long[n];
for(int i=0; i<n; i++) arr[i]=nl();
return arr;
}
void solve()
{
int test_case = ni();
while(test_case-- > 0)
{
String s = ns();
int n = s.length();
if(n == 1 || s.charAt(s.length()-1) == 'A') out.println("NO");
else {
Stack<Character> stack = new Stack<>();
boolean f = false;
for(int i=0; i<n; i++) {
if(s.charAt(i) == 'A') {
stack.push(s.charAt(i));
} else {
if(stack.isEmpty()) {
f = true;
break;
}
else {
if(stack.peek() == 'A') {
stack.pop();
} else {
f = true;
break;
}
}
}
}
if(f) out.println("NO");
else out.println("YES");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 9326ea149f5a296e16ae56b1df9eef6d | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class CodeforcesGlobalRound20_B1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
String s = sc.next();
boolean good = true;
int a = 0;
int b = 0;
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == 'A') a++;
else b++;
if(a<b){
good=false;
}
}
if(s.charAt(s.length()-1)!='B'){
good=false;
}
if (good) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 2f15f31a95b7c3fe26b34c5519a0ffb7 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.io.*;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class AAAB {
void solveCase(FScanner scanner) throws IOException {
String s = scanner.next();
if(s.length()<2 || !s.endsWith("B")||!s.startsWith("A")) {
out.println("NO");
out.flush();
return;
}
int n = s.length();
boolean fail = false;
int numB = 0;
boolean seenB = false;
for (int k = n-1; k >=0; k--) {
char curr = s.charAt(k);
if (curr == 'B') {
seenB = true;
numB++;
} else {
if(!seenB) {fail=true; break;}
numB = Math.max(0, numB-1);
}
}
if(fail||numB>0) fail=true;
out.println(fail?"NO":"YES");
out.flush();
}
//-----------------------------------------
InputStream sin;
PrintWriter out;
AAAB() {
this(System.in, System.out);
}
AAAB(InputStream sin, OutputStream sout) {
this.sin = sin;
out = new PrintWriter(new BufferedOutputStream(sout));
}
public FScanner createScanner() {
return new FScanner(sin);
}
void solve(FScanner scanner) throws IOException {
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
solveCase(scanner);
}
}
public static void main(String[] args) {
AAAB task = new AAAB();
FScanner scanner = task.createScanner();
try {
task.solve(scanner);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static class FScanner {
private BufferedReader bufferedReader;
public FScanner(InputStream inputStream) {
this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
}
public int nextInt() throws IOException {
String line = bufferedReader.readLine();
return Integer.parseInt(line);
}
public String next() throws IOException {
String line = bufferedReader.readLine();
return line;
}
public int[] nextIntArr(int n) throws IOException {
String line = bufferedReader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line);
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = Integer.parseInt(tokenizer.nextToken());
}
return result;
}
public long[] nextLongArr(int n) throws IOException {
String line = bufferedReader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line);
long[] result = new long[n];
for (int i = 0; i < n; i++) {
result[i] = Long.parseLong(tokenizer.nextToken());
}
return result;
}
public double[] nextDoubleArr(int n) throws IOException {
String line = bufferedReader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line);
double[] result = new double[n];
for (int i = 0; i < n; i++) {
result[i] = Double.parseDouble(tokenizer.nextToken());
}
return result;
}
public String[] nextStringArr(int n) throws IOException {
String line = bufferedReader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line);
String[] result = new String[n];
for (int i = 0; i < n; i++) {
result[i] = tokenizer.nextToken();
}
return result;
}
// --------------
long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long gcdAll(long[] arr, int start) {
long g = arr[start];
if (arr.length > 2) {
for (int j = start + 2; j < arr.length; j += 2) {
g = gcd(arr[j], g);
}
}
return g;
}
void reverse(int[] arr) {
int n = arr.length;
for (int i = 0; i < n / 2; i++) {
int tmp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = tmp;
}
}
}
}
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | a5e6176a7890b6daa28389c81c79d72d | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.util.*;
public class StringLove {
public static void main(String[] args) {
Scanner Abhi = new Scanner(System.in); // Abhishek Das Codechef 1*//
int t = Abhi.nextInt(); // long/ int
Abhi.nextLine();
while(t-->0){
// long/int
String s2 = Abhi.nextLine();
int countA = 0, countB =0;
int n= s2.length();
for(int i =0; i<n; i++) {
if(s2.charAt(i)=='A')
countA++;
else {
countB++;
}
if(countA<countB) {
n=0;
break;
}
}
if(n < 2) System.out.println("NO");
else if(s2.charAt(0) =='B' || s2.charAt(n-1) == 'A') {
System.out.println("NO");
}
else {
System.out.println("YES");
}
}
}
}
//4
//AABAB
//ABB
//AAAAAAAAB
//A
| Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 98c553e49d1e038f9e90e3ab7ec15334 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
import java.io.*;
public class NewProgramJava {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T > 0) {
T--;
String str = sc.next();
int aS = 0;
int bS = 0;
int flag = 1;
for (int i = 0; i<str.length(); i++){
if (str.charAt(i) == 'A'){
aS++;
}
else {
bS++;
}
if (bS>aS){
flag = 0;
}
}
if (str.charAt(str.length()-1) !='B'){
flag = 0;
}
if (flag == 1){
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | ea0ffc1b3052512f08c578d65ad784c1 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.*;
public class Forcodechef{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int flag=0;
int n;
String str;
while(t-->0){
flag=0;
str=sc.next();
if(str.length()==1||str.charAt(str.length()-1)!='B'||str.charAt(0)=='B')
System.out.println("NO");
else {
int [] A=new int[str.length()];
int [] B=new int[str.length()];
if(str.charAt(0)=='B')
{
B[0]=1;
A[0]=0;
}
else{
A[0]=1;
B[0]=0;
}
for(int i=1;i<str.length();i++){
if(str.charAt(i)=='B')
{
B[i]=B[i-1]+1;
A[i]=A[i-1];
}
else{
B[i]=B[i-1];
A[i]=A[i-1]+1;
}
}
for(int i=1;i<str.length();i++){
if(B[i]>A[i])
{
flag=1;
break;
}
}
if(flag==0)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | ca8c68b98ae9542e1be99d4b0e6b49b2 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
public class ILoveAAAB {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
pls:
while (t-- > 0) {
String s = sc.next();
int a = 0;
int b = 0;
if (s.length() == 1) {
System.out.println("NO");
continue;
}
if (s.charAt(s.length() - 1) != 'B') {
System.out.println("NO");
continue;
}
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
a++;
} else {
b++;
if (b > a) {
System.out.println("NO");
continue pls;
}
}
}
System.out.println("YES");
}
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | c07b928b0829948fbfb81150fce6fd5e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes |
import java.io.*;
import java.util.*;
public class EG {
static boolean palindrome(String s,int i,int j) {
if(j==s.length())return false;
while(i<j) {
if(s.charAt(i)!=s.charAt(j))return false;
i++;j--;
}
return true;
}
static int valid(String s,int i) {
Stack<Character>st=new Stack<>();
int n=s.length();
return -1;
}
static int msb(long num){
int c=0;
while(num!=0) {
num>>=1;
c++;
}
return c-1;
}
static boolean isPrime(long n, int k)
{
// Corner cases
if (n <= 1 || n == 4) return false;
if (n <= 3) return true;
// Try k times
while (k > 0)
{
// Pick a random number in [2..n-2]
// Above corner cases make sure that n > 4
int a = 2 + (int)(Math.random() % (n - 4));
// Fermat's little theorem
if (Math.pow(a,n - 1) != 1)
return false;
k--;
}
return true;
}
static void swap(int a[],int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
static boolean collinear(int x1, int y1, int x2,
int y2, int x3, int y3)
{
int a = x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2);
if (a == 0)return true;
else return false;
}
public static void main(String[] args) throws ArithmeticException{
FastScanner fs=new FastScanner();
int t=fs.nextInt();
while(t-->0){
boolean ans=true;
String s=fs.next();
int n=s.length();
int i=0,ca=0,cb=0;
for(;i<n;i++){
if(s.charAt(i)=='A') {
ca++;
}
if(s.charAt(i)=='B') {
cb++;
}
if(cb>ca)ans=false;
}
if(ans && s.charAt(n-1)=='B')System.out.println("YES");
else System.out.println("NO");
}
}
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | 7599d9c6ad94b222e08876e2c18b53d5 | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.StringTokenizer;
public class B {
{
MULTI_TEST = true;
FILE_NAME = "";
NEED_FILE_IO = false;
INF = (long) 1e18;
} // fields
void solve() {
String s =rs();
if(!s.contains("B") || s.startsWith("B") || !s.endsWith("B")) {
out.println("NO");
return;
} else {
int cntA = 0, cntB = 0;
int n = s.length();
for(int i = 0; i < n; i++) {
if(s.charAt(i) == 'A') {
cntA++;
} else {
cntB++;
if(cntB > cntA) {
out.println("NO");
return;
}
}
}
out.println("YES");
}
}
public static void main(String[] args) {
new B().run();
} //main
@SuppressWarnings("unused")
long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
@SuppressWarnings("unused")
long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
@SuppressWarnings("unused")
int gcd(int a, int b) {
return (int) gcd((long) a, b);
}
@SuppressWarnings("unused")
int lcm(int a, int b) {
return (int) lcm(a, (long) b);
}
@SuppressWarnings("unused")
int sqrtInt(int x) {
return (int) sqrtLong(x);
}
@SuppressWarnings("unused")
long sqrtLong(long x) {
long root = (long) Math.sqrt(x);
while (root * root > x) --root;
while ((root + 1) * (root + 1) <= x) ++root;
return root;
}
@SuppressWarnings("unused")
int cbrtLong(long x) {
int cbrt = (int) Math.cbrt(x);
while ((long) cbrt * cbrt >= x) {
cbrt--;
}
while ((long) (cbrt + 1) * (cbrt + 1) <= x) cbrt++;
return cbrt;
}
@SuppressWarnings("unused")
long binpow(long a, long power) {
return binpow(a, power, Long.MAX_VALUE);
}
@SuppressWarnings("unused")
long binpow(long a, long power, long modulo) {
if (power == 0) return 1 % modulo;
if (power % 2 == 1) {
long b = binpow(a, power - 1, modulo) % modulo;
return ((a % modulo) * b) % modulo;
} else {
long b = binpow(a, power / 2, modulo) % modulo;
return (b * b) % modulo;
}
}
@SuppressWarnings("unused")
long fastMod(String s1, long n2) {
long num = 0;
for (int i = 0; i < s1.length() - 1; i++) {
num += Integer.parseInt(String.valueOf(s1.charAt(i)));
num *= 10;
if (num >= n2) {
num = num % n2;
}
}
return (num + Integer.parseInt(String.valueOf(s1.charAt(s1.length() - 1)))) % n2;
}
@SuppressWarnings("unused")
long factorialMod(long n, long p) {
long res = 1;
while (n > 1) {
res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p;
for (int i = 2; i <= n % p; ++i)
res = (res * i) % p;
n /= p;
}
return res % p;
}
@SuppressWarnings("unused")
boolean isPrime(int number) {
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
return false;
}
}
return number > 1;
}
@SuppressWarnings("unused")
boolean[] primes(int border) {
boolean[] isPrimes = new boolean[border + 1];
Arrays.fill(isPrimes, true);
isPrimes[0] = false;
isPrimes[1] = false;
for (int i = 2; i < border + 1; i++) {
if (!isPrimes[i]) continue;
for (int k = i * 2; k < border; k += i) {
isPrimes[k] = false;
}
}
return isPrimes;
} //Number theory
@SuppressWarnings("unused")
void sort(int[] a) {
int n = a.length;
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = a[i];
}
Arrays.sort(arr);
for (int i = 0; i < n; i++) {
a[i] = arr[i];
}
}
@SuppressWarnings("unused")
void sort(long[] a) {
int n = a.length;
Long[] arr = new Long[n];
for (int i = 0; i < n; i++) {
arr[i] = a[i];
}
Arrays.sort(arr);
for (int i = 0; i < n; i++) {
a[i] = arr[i];
}
}
@SuppressWarnings("unused")
int max(int[] a) {
int n = a.length;
int max = Integer.MIN_VALUE;
for (int j : a) {
if (j > max) max = j;
}
return max;
}
@SuppressWarnings("unused")
long max(long[] a) {
int n = a.length;
long max = Long.MIN_VALUE;
for (long l : a) {
if (l > max) max = l;
}
return max;
}
@SuppressWarnings("unused")
int maxIndex(int[] a) {
int n = a.length;
int max = Integer.MIN_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
int maxIndex(long[] a) {
int n = a.length;
long max = Long.MIN_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
int min(int[] a) {
int n = a.length;
int min = Integer.MAX_VALUE;
for (int j : a) {
if (j < min) min = j;
}
return min;
}
@SuppressWarnings("unused")
int minIndex(int[] a) {
int n = a.length;
int min = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] < min) {
min = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
int minIndex(long[] a) {
int n = a.length;
long min = Long.MAX_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] < min) {
min = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
long min(long[] a) {
int n = a.length;
long min = Long.MAX_VALUE;
for (long l : a) {
if (l < min) min = l;
}
return min;
}
@SuppressWarnings("unused")
long sum(int[] a) {
int n = a.length;
long sum = 0;
for (int j : a) {
sum += j;
}
return sum;
}
@SuppressWarnings("unused")
long sum(long[] a) {
int n = a.length;
long sum = 0;
for (long l : a) {
sum += l;
}
return sum;
} //Arrays Operations
@SuppressWarnings("unused")
String readLine() {
try {
return in.readLine();
} catch (Exception e) {
throw new InputMismatchException();
}
}
@SuppressWarnings("unused")
String rs() {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(readLine());
}
return tok.nextToken();
}
@SuppressWarnings("unused")
int ri() {
return Integer.parseInt(rs());
}
@SuppressWarnings("unused")
long rl() {
return Long.parseLong(rs());
}
@SuppressWarnings("unused")
double rd() {
return Double.parseDouble(rs());
}
@SuppressWarnings("unused")
int[] ria(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ri();
}
return a;
}
@SuppressWarnings("unused")
int[] riawd(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ri() - 1;
}
return a;
}
@SuppressWarnings("unused")
long[] rla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = rl();
}
return a;
}
@SuppressWarnings("unused")
private boolean yesNo(boolean yes, String yesString, String noString) {
out.println(yes ? yesString : noString);
return yes;
} //fastIO
void run() {
try {
long start = System.currentTimeMillis();
initIO();
if (MULTI_TEST) {
long t = rl();
while (t-- > 0) {
solve();
}
} else {
solve();
}
out.close();
System.err.println("Time(ms) - " + (System.currentTimeMillis() - start));
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void initConsoleIO() {
out = new PrintWriter(System.out);
in = new BufferedReader(new InputStreamReader(System.in));
}
void initFileIO(String inName, String outName) throws FileNotFoundException {
out = new PrintWriter(outName);
in = new BufferedReader(new FileReader(inName));
}
void initIO() throws FileNotFoundException {
if (!FILE_NAME.isEmpty()) {
initFileIO(FILE_NAME + ".in", FILE_NAME + ".out");
} else {
if (NEED_FILE_IO && new File("input.txt").exists()) {
initFileIO("input.txt", "output.txt");
} else {
initConsoleIO();
}
}
tok = new StringTokenizer("");
} //initIO
private final String FILE_NAME;
private final boolean MULTI_TEST;
private final boolean NEED_FILE_IO;
private final long INF;
BufferedReader in;
PrintWriter out;
StringTokenizer tok; //fields
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output | |
PASSED | fba124c88866025c50190bc359c75b5e | train_108.jsonl | 1650722700 | Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations? | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
public class CF1910{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testCases = scan.nextInt();
scan.nextLine();
while(testCases>0){
String string = scan.nextLine();
if(string.charAt(0)!='B' && string.length()>=2 && string.charAt(string.length()-1)=='B'){
if(isMadeFromGoodStrings(string)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}else{
System.out.println("NO");
}
testCases--;
}
}
public static boolean isMadeFromGoodStrings(String string){
int[] cummulativeACounterArray = new int[string.length()];
int aCounter = 0;
for(int i=0; i<string.length(); i++){
if(string.charAt(i)=='A'){
aCounter++;
}
cummulativeACounterArray[i] = aCounter;
}
int[] consecutiveBArray = new int[string.length()];
int i=string.length()-1;
while(i>=0){
if(string.charAt(i)=='B'){
int bCounter = 0;
int j=i;
while(j>=0 && string.charAt(j)=='B'){
bCounter++;
j--;
}
consecutiveBArray[j+1] = bCounter;
i=j;
}
i--;
}
i=string.length()-1;
boolean flag = true;
while(i>=0){
if(consecutiveBArray[i]>0){
int numberOfBS = consecutiveBArray[i];
int numberOfAAtHead = cummulativeACounterArray[i];
int extraA = numberOfAAtHead-numberOfBS;
int numberOfBSAhead = i- numberOfAAtHead;
if(numberOfBSAhead>extraA){
flag =false;
break;
}
}
i--;
}
return flag;
}
} | Java | ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"] | 1 second | ["YES\nNO\nYES\nNO"] | NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0b9be2f076cfa13cdc76c489bf1ea416 | Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.