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
3e0712a96b294d710bcd493c8847559e
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.lang.Math; public class infinite { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t-->0){ String s = sc.nextLine(); String q = sc.nextLine(); long sw = 0; long qw = 0; for (int i = 0; i < s.length(); i++) { if(s.charAt(i)=='a')sw++; } for (int i = 0; i < q.length(); i++) { if(q.charAt(i)=='a')qw++; } if(qw!=0 && q.length()>1) System.out.println(-1); else if(qw==1 && q.length()==1) System.out.println(1); else System.out.println((long) Math.pow(2, sw)); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
bf4833018c43a7c90e34dadac230d1f7
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public class vanita { public static void main(String[] args){ Scanner in =new Scanner(System.in); int cases = in.nextInt(); boolean infinite = false; boolean other = false; boolean test = false; for (int i = 0; i < cases; i++){ int count = 0; infinite = false; String word = in.next(); String replacement = in.next(); other = false; for (int j = 0; j < replacement.length();j++){ if(replacement.charAt(j) != 'a'){ other = true; } if(replacement.charAt(j) == 'a'){ infinite = true; } } for (int k = 0; k < word.length(); k++){ count += word.length() +1; } if (infinite == true && (other == true || replacement.length() > 1)){ System.out.println("-1"); continue; } if(infinite == true){ if(replacement.length() > word.length()){ System.out.println("2"); continue; } else{ System.out.println("1"); continue; } } System.out.println((long )Math.pow(2,word.length())); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
01cb0d9fd3667585dfa75e33fcf87132
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); long x=sc.nextLong(); String c=sc.nextLine(); for(int i=0;i<x;i++){ String s=sc.nextLine(); String t=sc.nextLine(); //System.out.println(t); if(t.equals("a")){ System.out.println(1); } else if(t.contains("a")){ System.out.println(-1); } else{ if(t.length()==0) System.out.println(s.length()); else System.out.println((long)Math.pow(2,s.length())); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
9c4338b9271fa489b48b61f4d77803c3
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.lang.*; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class A_769 { 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 void sort(long[] arr) { ArrayList<Long> a = new ArrayList<>(); for (long i : arr) { a.add(i); } Collections.sort(a); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long fact(long number) { if(number == 0 || number == 1) { return 1; } else { return number * fact(number - 1); } } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); // StringBuilder as = new StringBuilder(); HashMap<String, Integer> map = new HashMap<>(); int z = 0; for(char ch = 'a'; ch <='z'; ch++){ for(char bh = 'a'; bh<='z'; bh++){ if(ch != bh){ String a = ""+ch+bh; map.put(a,++z); } } } // System.out.println(map); int t = sc.nextInt(); test: while (t-- > 0) { String a = sc.next(); String b = sc.next(); Set<Character> ss = new HashSet<>(); for(char ch: b.toCharArray()){ ss.add(ch); } if(ss.size() == 1 && b.length()==1 && b.charAt(0)=='a'){ System.out.println(1); }else if(b.contains("a") && b.length() > 1){ System.out.println("-1"); }else{ long ans = (long)(Math.pow(2,a.length())); System.out.println(ans); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
5cb530ecc9b043bdcbf37d4095460062
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
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(), b = fs.next(); int count1 = 0; for(int i = 0; i < a.length(); i++) { if(a.charAt(i) == 'a') count1++; } String ref = a; StringBuilder sb = new StringBuilder(); for(int i = 0; i < a.length(); i++) { if(a.charAt(i) == 'a') { sb.append(a.substring(0, i)); sb.append(b); if(i+1 <= a.length())sb.append(a.substring(i+1)); break; } } // System.out.println(sb.toString()); if(a.length() == 0) { System.out.println(0); } else if(ref.equals(sb.toString())) { System.out.println("1"); }else { int count3 = 0; for(int i = 0; i < sb.toString().length(); i++) { if(sb.toString().charAt(i) == 'a') { count3++; } } if(count3 < count1) { System.out.println(1l << count1); }else { System.out.println(-1); } } } } static int fact(int num) { int ans = 1; for(int i = 1; i <= num; i++) { ans *= i; } return ans; } 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
fddcc9a675beae740fd52cf36aacd4dc
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.security.KeyPair; 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.Stack; import java.util.TreeMap; import java.util.TreeSet; /* Name of the class has to be "Main" only if the class is public. */ public class temp { abstract class sort implements Comparator<ArrayList<Integer>>{ @Override public int compare(ArrayList<Integer> a,ArrayList<Integer> b) { return a.get(0)-b.get(0); } } private static Comparator sort; public static void main (String[] args) throws Exception { FastScanner sc= new FastScanner(); int tt = sc.nextInt(); while(tt-- >0){ String s=sc.next(); String t=sc.next(); boolean bob=false; for(int i=0;i<t.length();i++) if(t.charAt(i)=='a') bob=true; if(bob && t.length()==1) { System.out.println(1); }else if(bob && t.length()>1) { System.out.println(-1); }else { long ans=(long)Math.pow(2, s.length()); System.out.println(ans); } } } public static int fac(int n) { if(n==0) return 1; return n*fac(n-1); } public static boolean palin(String res) { StringBuilder sb=new StringBuilder(res); String temp=sb.reverse().toString(); return(temp.equals(res)); } 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 int gcd(int a, int b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } 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; } static 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
23d72981b6758c53393810c5afcf298a
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.Scanner; public class Practice { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int p=sc.nextInt(); while(p-- >0) { String s=sc.next(); String t=sc.next(); boolean bob=false; for(int i=0;i<t.length();i++) if(t.charAt(i)=='a') bob=true; if(t.length()==1 && bob) System.out.println(1); else if(t.length()>1 && bob) System.out.println(-1); else System.out.println((long)Math.pow(2, s.length())); } } public static boolean fun(int nums[]) { int ans=0; for(int x:nums) if(x==0) ans++; return (ans==nums.length); } public static int rec(int n) { int ans=0; int mult=1; while(n!=0) { if(n%10!=0) { ans+=mult*(n%10); mult*=10; } n/=10; } return ans; } public static boolean sorted(String s) { for(int i=1;i<s.length();i++) { int x=s.charAt(i); int y=s.charAt(i-1); if(x<y) return false; } return true; } public static int rec(int[]nums,int i,int j) { int n=nums.length; int ans=0; while(i<n && j>=0) { if(nums[i]>0 && nums[j]<0) { int res=Math.min(nums[i],-nums[j]); nums[i]-=res;nums[j]+=res; if(j>i) ans+=res; } i++;j--; } return ans; } private static void shuffleArray(int[] nums) { //shuffleArray(nums); int n = nums.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = nums[i]; int randomPos = i + rnd.nextInt(n-i); nums[i] = nums[randomPos]; nums[randomPos] = tmp; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
071abf083cf5ef9831c06b7a8e216060
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; public class temp{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0){ String s=sc.next(); String r=sc.next(); if(r.equals("a")) System.out.println(1); else if(r.indexOf('a')!=-1) System.out.println(-1); else{ long ans=(long)Math.pow(2,s.length()); System.out.println(ans); } t--; } sc.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
a12e7b5367227ca07df445af20838fb8
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); for(int casesTot = scan.nextInt(), cases =1;cases<=casesTot;cases++){ String str = scan.next(); String sub = scan.next(); if(sub.contains("a")){ boolean inf = false; boolean found=false; for(int i = 0;i<sub.length();i++){ if(sub.charAt(i)!='a'){ inf=true; break; }else{ if(found){ inf=true; break; }else{ found=true; } } } System.out.println(inf?"-1":"1"); }else{ BigInteger ans = BigInteger.valueOf(2); ans=ans.pow(str.length()); System.out.println(ans); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
e7c9ddef50117434b0c4f45132a1689b
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.lang.System; import static java.lang.System.out; import static java.lang.System.setOut; public class CodeForces { public static void main(String[] args) { FastReader fastReader = new FastReader(); long n = fastReader.nextLong(); for (int i = 0; i < n; i++) { String s = fastReader.nextToken(); String s2 = fastReader.nextToken(); if(s2.equals("a")) out.println(1); else if(s2.contains("a")) out.println(-1); else out.println((long) Math.pow(2,s.length())); } } static String yes(){return "YES";} static String no(){return "NO";} private static boolean isSorted(int[] ar) { for (int i = 1; i < ar.length; i++) { if(ar[i] < ar[i-1]) return false; } return true; } static int findGCD(int x, int y) { int r = 0, a, b; a = Math.max(x, y); // a is greater number b = Math.min(x, y); // b is smaller number r = b; while (a % b != 0) { r = a % b; a = b; b = r; } return r; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } String nextLine() { String str = ""; try { if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { 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; } long[] readLongArray(int n) { long[] a = new long[n]; for (long i = 0; i < n; i++) { a[(int) i] = nextLong(); } return a; } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
0617689a8f639c60502f35b52ff1ca79
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Test { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int t = sc.nextInt(); while (t-- > 0) { String str1 = sc.next(); String str2 = sc.next(); int aIndex = str2.indexOf('a'); if (aIndex != -1 && str2.length() > 1) { System.out.println("-1"); } else if (aIndex != -1 && str2.length() == 1) { System.out.println("1"); } else if (aIndex == -1) { long ans = (long)(Math.pow(2.0, (double)str1.length())); System.out.println(ans); } } } sc.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
657a4634c303cd4e5d9383755d1c2b9c
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class Main { static ArrayList<Integer> palindromes; static int dp[][]; static int mod = 1000000007; static boolean containA (String s){ int n = s.length(); for(int i = 0;i<n;i++){ if(s.charAt(i)=='a'){ return true; } } return false; } static HashSet<String> set ; static StringBuilder sb; static String t; 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) { String s = sc.next(); String t = sc.next(); set = new HashSet<>(); if(t.length() == 1 && t.charAt(0)=='a'){ pw.println(1); } else if(containA(t)){ pw.println(-1); } else{ long d = (1<<s.length()); pw.println(powof2(s.length())); } } pw.flush(); pw.close(); } public static long powof2 (long n){ long ans = 1; while(n>0){ ans = ans*2; n--; } return ans; } static class Pair implements Comparable { int x; int y; Pair(int i, int j) { this.x = i; this.y = j; } @Override public int compareTo(Object o) { Pair p = (Pair) o; return this.y - p.y; } } 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 boolean hasNext() { // TODO Auto-generated method stub return false; } 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
ab2ca27075da217843ffe10d27ccfecf
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
//package HackerRankpractice; import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import javax.lang.model.util.ElementScanner6; import javax.sound.midi.SysexMessage; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; public class Solution { static HashMap<Integer, Integer> createHashMap(int arr[]) { HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>(); for (int i = 0; i < arr.length; i++) { Integer c = hmap.get(arr[i]); if (hmap.get(arr[i]) == null) { hmap.put(arr[i], 1); } else { hmap.put(arr[i], ++c); } } return hmap; } static int parseInt(String str) { return Integer.parseInt(str); } static String noZeros(char[] num) { String str = ""; for (int i = 0; i < num.length; i++) { if (num[i] == '0') continue; else str += num[i]; } return str; } public static void Sort2DArrayBasedOnColumnNumber(int[][] array, final int columnNumber) { Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] first, int[] second) { if (first[columnNumber - 1] > second[columnNumber - 1]) return 1; else return -1; } }); } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk; int t = parseInt(br.readLine()); String str; String str2; boolean flag; int counter; for (int i = 1; i <= t; i++) { counter = 0; flag = true; str = br.readLine(); str2 = br.readLine(); for (int j = 0; j < str2.length(); j++) { if (str2.charAt(j) == 'a') counter++; } if (counter == 1 && counter == str2.length()) { System.out.println(1); continue; } else if (counter == 1 && counter < str2.length()) flag = false; else if (counter > 0) flag = false; if (!flag) System.out.println(-1); else System.out.println((long) Math.pow(2, str.length())); } } } /* * * 64 as * * * 96 * 8 * 1 8 2 7 3 6 4 5 * -1 0 2 7 9 12 16 20 * count=16 * * BRWBRWBRW * * * x=7 * 2 1 2 * count=3 * 3 2 3 * count=5 * 4 3 4 * count=7 * 5 4 5 * count=8 * 6 5 6 * 7 6 7 * 8 7 8 * 9 8 9 * count=11 * ans+=(x-sum)/(i+1)+1 * -1 2 0 * -1 0 2 * sum=0 * sum=-1 * 1 2 4 2 1 * 4 2 2 1 1 * 4 2 2 1 1 * * 2 4 5 5 2 * 4 5 5 2 2 * 5 5 2 2 4 * * 2 * 2 2 4 5 5 * * * * 4 * 2 10 1 7 * 3 * sum=-1 * sum * 7 9 * 8 * 6 * 3 * 7 * 12 * 6 * -1 * 7 * 16 * * * * 10 10 * 9 * 11 * 14 * 10 * 5 * 11 * 18 * 10 * 1 * 11 * +10 -5=5 * 5 7 * 6 * 4 * 1 * 5 * 10 * 6 * -1 * 3 * * 2 * * * * sum=15 * sum<=x * count+=n * count=5 * sum+=n * sun=20 * count=10 * sum=25 * sum=18 * * * * * * * indeces={0,1,6,7} * 5-1=4 */
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
9bc6eca57338d130a30d84ab7d7f3593
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
//package solution; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Solution implements Runnable { public void solve() throws Exception { int cse = sc.nextInt(); while (cse-- > 0) { String s = in.readLine(); String t = in.readLine(); if (t.length() == 1 && t.contains("a")) { out.println(1); continue; } if (!t.contains("a")) { out.println((long)Math.pow(2.0, s.length())); } else { out.println(-1); } } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Solution(), "", (1 << 26)); thread.start(); thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
9fe79972e87fcb95b4d762ebc4d3e633
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static long c(long n, long k) { long r = 1; k = Math.min(k, n - k); for (int i = 1; i <= k; i++) { r *= n--; r/=i; } return r; } public static long fac(long n) { return n == 0 ? 1 : n * fac(n - 1); } 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(); String c = sc.next(); if (c.equals("a")) pw.println(1); else if (c.contains("a")) pw.println(-1); else { long res = 1; for (int i = 1; i <= s.length(); i++) res += c(s.length(), i); pw.println(res); } } pw.flush(); } static class Pair { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } } 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 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 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
77702ebf3a4f942c445148a677cc4602
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
/* package codechef; // don't place package name! */ 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 { try { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-- > 0) { String s=sc.next(); String x=sc.next(); int a=0; for(int i=0; i<x.length(); i++) { if(x.charAt(i)=='a') a++; } if(x.equals("a")) System.out.println(1); else if(a!=0) System.out.println(-1); else System.out.println((long)Math.pow(2, s.length())); } } catch(Exception e) { } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
a540f88498456a3b1edbf247047109ac
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class C { public static void main(String[] args) throws IOException { Reader sc = new Reader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { String s1 = sc.next(); String s2 = sc.next(); if(s2.length() > 1) { int flag = 0; for(int i = 0;i < s2.length();i++) { if(s2.charAt(i) == 'a') { flag = 1; break; } } if(flag == 1) out.println(-1); else out.println((long)Math.pow(2,s1.length())); } else { if(s2.charAt(0) == 'a') out.println(1); else if(s1.length() == 1) out.println(2); else out.println((long)Math.pow(2,s1.length())); } out.flush(); } } 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 { if(st.hasMoreTokens()) str = st.nextToken("\n"); else str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } // ********************* GCD Function ********************* //int gcd(int a, int b){ // if (b == 0) // return a; // return gcd(b, a % b); //} // ********************* Prime Number Function ********************* //int isPrime(int n){ // if(n < 2) // return 0; // if(n < 4) // return 1; // if(n % 2 == 0 or n % 3 == 0) // return 0; // for(int i = 5; i*i <= n; i += 6) // if(n % i == 0 or n % (i+2) == 0) // return 0; // return 1; //} // ********************* Custom Pair Class ********************* //class Pair implements Comparable<Pair> { // int a,b; // public Pair(int a,int b) { // this.a = a; // this.b = b; // } // @Override // public int compareTo(Pair other) { // if(this.b == other.b) // return Integer.compare(this.a,other.a); // return Integer.compare(this.b,other.b); // } //} // ****************** Segment Tree ****************** //public class SegmentTreeNode { // public SegmentTreeNode left; // public SegmentTreeNode right; // public int Start; // public int End; // public int Sum; // public SegmentTreeNode(int start, int end) { // Start = start; // End = end; // Sum = 0; // } //} //public SegmentTreeNode buildTree(int start, int end) { // if(start > end) // return null; // SegmentTreeNode node = new SegmentTreeNode(start, end); // if(start == end) // return node; // int mid = start + (end - start) / 2; // node.left = buildTree(start, mid); // node.right = buildTree(mid + 1, end); // return node; //} //public void update(SegmentTreeNode node, int index) { // if(node == null) // return; // if(node.Start == index && node.End == index) { // node.Sum += 1; // return; // } // int mid = node.Start + (node.End - node.Start) / 2; // if(index <= mid) // update(node.left, index); // else // update(node.right, index); // node.Sum = node.left.Sum + node.right.Sum; //} //public int SumRange(SegmentTreeNode root, int start, int end) { // if(root == null || start > end) // return 0; // if(root.Start == start && root.End == end) // return root.Sum; // int mid = root.Start + (root.End - root.Start) / 2; // if(end <= mid) // return SumRange(root.left, start, end); // else if(start > mid) // return SumRange(root.right, start, end); // return SumRange(root.left, start, mid) + SumRange(root.right, mid + 1, end); //}
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
ebcb02ca4f84898679b8e1e6ac642170
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
// Problem: C. Infinite Replacement // Contest: Codeforces - Codeforces Round #786 (Div. 3) // URL: https://codeforces.com/contest/1674/problem/C // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) import java.util.*; public class Mytemplate { static boolean ans=false; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while (t>0){ t--; String s1=sc.next(); String s2=sc.next(); long n=s1.length(); if(s2.length()==1 && s2.charAt(0)=='a'){ System.out.println(1); }else{ boolean tt=false; for(char c:s2.toCharArray()){ if(c=='a'){ tt=true; break; } } if(tt){ System.out.println(-1); }else{ long ans=1; System.out.println(ans<<n); } } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
783ddbaf25f029b587b920be995dd0c5
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class Main { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.rtf")); PrintStream out = new PrintStream(new FileOutputStream("output.rtf")); System.setOut(out); } catch (Exception e) { 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; } } // end of fast i/o code public static void main(String[] args) { FastReader read = new FastReader(); int tests = read.nextInt(); for(int i = 0; i < tests; i++){ String s = read.nextLine(); String t = read.nextLine(); int aCount = 0; for(int j = 0; j < t.length();j++){ if(t.charAt(j) == 'a') aCount++; } if(t.equals("a")) System.out.println(1); else if(aCount != 0) System.out.println(-1); else System.out.println((long) Math.pow(2, s.length())); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
14cdc7db31ad5b1c380ffcadc462f167
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class C_Infinite_Replacement { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int tc = sc.nextInt(); while (tc-- > 0) { String s = sc.next(), t = sc.next(); if (t.length() > 1 && count(t, 'a') >= 1) out.println(-1); else if (t.charAt(0) == 'a') out.println(1); else if (!t.contains("a")) out.println((long)Math.pow(2,s.length())); } out.close(); } public static PrintWriter out; public static long mod = (long) 1e9 + 7; public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void SieveOfEratosthenes(int n, boolean prime[]) { prime[0] = false; prime[1] = false; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) for (int i = p * p; i <= n; i += p) prime[i] = false; } } static void dfs(int root, boolean[] vis, int[] value, ArrayList[] gr, int prev) { vis[root] = true; value[root] = 3 - prev; prev = 3 - prev; for (int i = 0; i < gr[root].size(); i++) { int next = (int) gr[root].get(i); if (!vis[next]) dfs(next, vis, value, gr, prev); } } static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } static boolean isPrime(long n) { for (long i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } static int abs(int a) { return a > 0 ? a : -a; } static int max(int a, int b) { return a > b ? a : b; } static int min(int a, int b) { return a < b ? a : b; } static long pow(long n, long m) { if (m == 0) return 1; long temp = pow(n, m / 2); long res = ((temp * temp) % mod); if (m % 2 == 0) return res; return (res * n) % mod; } static long modular_add(long a, long b) { return ((a % mod) + (b % mod)) % mod; } static long modular_sub(long a, long b) { return ((a % mod) - (b % mod) + mod) % mod; } static long modular_mult(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static int gcd(int n1, int n2) { if (n2 != 0) return gcd(n2, n1 % n2); else return n1; } static class Pair { int u, v; Pair(int u, int v) { this.u = u; this.v = v; } static void sort(Pair[] coll) { List<Pair> al = new ArrayList<>(Arrays.asList(coll)); Collections.sort(al, new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return p1.u - p2.u; } }); for (int i = 0; i < al.size(); i++) { coll[i] = al.get(i); } } } static void sort(int[] a) { ArrayList<Integer> list = new ArrayList<>(); for (int i : a) list.add(i); Collections.sort(list); for (int i = 0; i < a.length; i++) a[i] = list.get(i); } static void sort(long a[]) { ArrayList<Long> list = new ArrayList<>(); for (long i : a) list.add(i); Collections.sort(list); for (int i = 0; i < a.length; i++) a[i] = list.get(i); } static int[] array(int n, int value) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = value; return a; } static long sum(long a[]) { long sum = 0; for (long i : a) sum += i; return (sum); } static long count(long a[], long x) { long c = 0; for (long i : a) if (i == x) c++; return (c); } static int sum(int a[]) { int sum = 0; for (int i : a) sum += i; return (sum); } static int count(int a[], int x) { int c = 0; for (int i : a) if (i == x) c++; return (c); } static int count(String s, char ch) { int c = 0; char x[] = s.toCharArray(); for (char i : x) if (ch == i) c++; return (c); } static int[] freq(int a[], int n) { int f[] = new int[n + 1]; for (int i : a) f[i]++; return f; } static int[] pos(int a[], int n) { int f[] = new int[n + 1]; for (int i = 0; i < n; i++) f[a[i]] = i; return f; } static boolean isPalindrome(String s) { StringBuilder sb = new StringBuilder(); sb.append(s); String str = String.valueOf(sb.reverse()); if (s.equals(str)) return true; else return false; } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
484ae6f68f39c7d38842ded17b8fe026
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class C { static FastScanner sc; static PrintWriter pw; static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni(); return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl(); return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long count(char a, String set) { long cnt = 0; for(int i = 0; i < set.length(); i++) { if(set.charAt(i) == 'a')cnt++; } return cnt; } static long getCount(String x, String y, int m, int n) { long cnt = 0; Set<Character> set = new HashSet<>(); for(int i = 0; i < n; i++)set.add(y.charAt(i)); cnt = count('a',y); if( (set.size()>=2 && cnt>0) || cnt>=2 ) return -1; else if(cnt==1) return 1; else return (long)1 << x.length(); } public static void main(String[] args) { // TODO Auto-generated method stub try{ pw = new PrintWriter(System.out); sc = new FastScanner(); int t = sc.ni(); // pw.println("Hello"); while(t-- > 0){ String a = sc.nextLine(); String b = sc.nextLine(); long res = getCount(a,b,a.length(),b.length()); pw.println(res); } pw.close(); }catch (Exception e) { return; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
0c53a1b5e14e55b182adc0d462c769a1
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int mod = 1000_000_007; static long mod1 = 998244353; static boolean memory = true; static FastScanner f; static PrintWriter pw; static double eps = 1e-6; static int oo = (int) 1e9; static boolean fileIO = false; public static void solve() throws Exception { String s = f.nextLine(); String t = f.nextLine(); if (t.equals("a")) { pn("1"); return; } for (int i = 0; i < t.length(); ++i) { if (t.charAt(i) == 'a') { pn("-1"); return; } } long ans = 1; for (int i = 0; i < s.length(); ++i) ans *= 2l; pn(ans); } public static void main(String[] args) throws Exception { if (memory) new Thread(null, new Runnable() { public void run() { try { Main.run(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }, "", 1 << 28).start(); else { Main.run(); } } static void run() throws Exception { if (System.getProperty("ONLINE_JUDGE") == null) { f = new FastScanner(""); File file = new File("!out.txt"); pw = new PrintWriter(file); } else { f = new FastScanner(); pw = new PrintWriter(System.out); } int t = f.ni(); int i = 1; while (t --> 0) { //pn("Case #" + i++ + ": "); solve(); } pw.flush(); pw.close(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String str) throws Exception { try { br = new BufferedReader(new FileReader("!a.txt")); } catch (Exception e) { e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int ni() throws IOException { return Integer.parseInt(next()); } public long nl() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nd() throws IOException { return Double.parseDouble(next()); } } public static void pn(Object... o) { for (int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " " : "\n")); } public static void p(Object... o) { for (int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " " : "")); } public static void pni(Object... o) { for (Object obj : o) pw.print(obj + " "); pw.println(); pw.flush(); } public 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); } public static void sort(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); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
02a06a8e4e24abae3c387818eeaa808f
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class InfiniteReplacement { public static void main(String args[]) { int i,j,k,t,n; Scanner sc=new Scanner(System.in); t=sc.nextInt(); while(t-->0) { String s=sc.next(); String x=sc.next(); int len1=s.length(); int len2=x.length(); int count=0; for(i=0;i<len2;i++) { if(x.charAt(i)=='a') { count++; } } if(count==1&&len2==1) { System.out.println("1"); } else if(count>=1) { System.out.println("-1"); } else if(count==0) { long sum=1; sum=sum<<len1; System.out.println(sum); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
3851fe9ee8d5ce8e8c757270547432d0
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; //import javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static int INF = Integer.MAX_VALUE; static int NINF = Integer.MIN_VALUE; static long mod = 1000000007l; public static void main (String[] args) throws java.lang.Exception { //check if you have to take product or the constraints are big int T = i(); while(T-- > 0){ String s = in.nextLine(); String t = in.nextLine(); if(t.length() > 1){ if(t.contains("a")){ out.println(-1); continue; }else{ out.println(1l << (long)s.length()); } continue; } if(t.charAt(0) == 'a'){ out.println(1); continue; } out.println(1l << (long)s.length()); } out.close(); } public static void sort(int[] arr){ ArrayList<Integer> ls = new ArrayList<>(); 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 void reverse(int[] arr){ int n = arr.length; for(int i = 0;i < n/2;i++){ int temp = arr[i]; arr[i] = arr[n-1-i]; arr[n-1-i] = temp; } } public static void sort(long[] arr){ ArrayList<Long> ls = new ArrayList<>(); for(long x : arr){ ls.add(x); } Collections.sort(ls); for(int i = 0;i < arr.length;i++){ arr[i] = ls.get(i); } } public static void reverse(long[] arr){ int n = arr.length; for(int i = 0;i < n/2;i++){ long temp = arr[i]; arr[i] = arr[n-1-i]; arr[n-1-i] = temp; } } public static void print(int[] arr){ int n = arr.length; for(int i = 0;i < n;i++){ out.print(arr[i] + " "); } out.println(); } public static void print(ArrayList<Integer> arr){ int n = arr.size(); for(int i = 0;i < n;i++){ out.print(arr.get(i) + " "); } 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; } } class pair implements Comparable<pair> { int x; int y; public pair(int x, int 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 Integer(x).hashCode() * 31 + new Integer(y).hashCode(); // } public int compareTo(pair other) { return Integer.compare(this.y,other.y); } } 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; } } // in.nextLine().toCharArray();
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
6151d17ce5de32c5bb48c8dcf60fb48e
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); Scanner sc = new Scanner(System.in); //int test_case = Integer.parseInt(br.readLine()); long test_case = sc.nextLong(); for (long test = 0; test < test_case; test++) { String s = sc.next(); String t = sc.next(); if(t.equals("a")){ System.out.println(1); }else if(t.contains("a")){ System.out.println(-1); }else{ System.out.println((long) Math.pow(2, s.length())); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
f472a085c60c840ab91bf9201beb8198
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
//package kriti; import java.math.*; import java.io.*; import java.util.*; public class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); public static void main(String args[])throws IOException { int t =i(); outer:while(t-->0) { String s=S(); String y =S(); char[] x = y.toCharArray(); int cnt=0; for(int i=0;i<y.length();i++) { if(x[i]=='a')cnt++; } if(cnt==y.length() && cnt==1) { out.println(1); } else if(cnt>0 && cnt<y.length() || (cnt==y.length() && cnt>1) ){ out.println(-1); } else { out.println((long)Math.pow(2,s.length())); } } out.close(); // // } static boolean checkres(int a, int b, long[] A) { Stack<Long> st = new Stack<>(); for(int i=0;i<A.length;i++) { st.add(A[i]); } int bf=0; int af=0;int cntb=0; int cnta=0; while( st.size()>0 && st.peek()!=b) { st.pop(); } if(st.size()==0)return false; while(st.size()>0 && st.peek()!=a) { st.pop(); } if(st.size()>=1)return true; return false; } static void BubbleSort(int[] A , int n, int i) { if(i==n-1 && n==0) return ; if(n>i) { if(A[i]>A[i+1]) { Swap(A, i,i+1); } BubbleSort(A,n, i=i+1); } else { BubbleSort(A,n=n-1,0); } } static int[] Swap(int[] A, int i, int j) { int temp = A[i]; A[i]=A[j]; A[j]=temp; return A; } static String ChartoString(char[] x) { String ans=""; for(char i:x) { ans+=i; } return ans; } static int HCF(int num1, int num2) { int temp1 = num1; int temp2 = num2; while(temp2 != 0){ int temp = temp2; temp2 = temp1%temp2; temp1 = temp; } int hcf = temp1; return hcf; } static boolean palindrome(String s) { char[] x = s.toCharArray(); int i=0; int r= x.length-1; while(i<r) { if(x[i]!=x[r]) { return false; } i++; r--; } return true; } static void sorting(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 boolean equal(int[] A) { int cnt=1;int prev=A[0]; for(int i=1;i<A.length;i++) { if(A[i]==prev)cnt++; } if(cnt==A.length)return true; return false; } static int findGcd(int x, int y) { if (x == 0) return y; return findGcd(y % x, x); } static int findLcm(int x, int y) { return (x / findGcd(x, y)) * y; } public static int checkTriangle(long a, long b, long c) { if (a + b <= c || a + c <= b || b + c <= a) return 0; else return 1; } static boolean isSorted(long A[]) { for(int i=1; i<A.length; i++)if(A[i]<=A[i-1])return false; return true; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } 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(ArrayList<Long> A) { for(long a:A)System.out.print(a); System.out.println(); } 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 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 int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String S() { return in.next(); } 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; } } 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
8d6cfb781b528b742c70f1d5d265d26e
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class dict { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); sc.nextLine(); while(t-->0) { int flag=0,m=0,n=0; String a=sc.nextLine(); String b=sc.nextLine(); n=a.length(); for(int i=0;i<b.length();i++) { if(b.charAt(i)!='a') flag=1; else m++; if((m>0 && flag==1) || (m>1)) break; } if((m>0 && flag==1) || (m>1)) System.out.println("-1"); else if(flag==0 && m==1) System.out.println("1"); else if(flag==1 && m==0) System.out.println((long)Math.pow(2, n)); } sc.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
7b7a1ebfd92b5aa795be0b4a2f6c9f11
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.lang.*; public class Main{ static FastReader in; static PrintWriter o; public static void solve() throws Exception { int t1 = in.nextInt(); while (t1-- > 0) { String s = in.next(); String t = in.next(); int tA = 0; int sA = 0; int tLen = t.length(); int sLen = s.length(); for (char c : t.toCharArray()) if (c == 'a') tA++; for (char c : s.toCharArray()) if (c == 'a') sA++; long ans = 0; // No a in s and t if (sA == 0 && tA == 0) { ans = 1; } // a in s, not in t if (sA != 0 && tA == 0) { ans = (long) Math.pow(2, sA); } // a not in s, but in t if (sA == 0 && tA != 0) { ans = 1; } // a in s, in t if (sA != 0 && tA != 0) { if (tLen == 1) { ans = 1; } else { ans = -1; } } o.println(ans); } o.close(); return; } public static void main(String[] args) throws Exception { o = new PrintWriter(System.out); in = new FastReader(); solve(); System.exit(0); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws FileNotFoundException { 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
8497620b16de0ec998884401caa01da6
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; import java.math.BigInteger; import java.math.BigDecimal; public class C { public static void main(String args[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); m: while(t-->0) { String s1=in.next(),s2=in.next(); int f=0; if(s2.equals("a")) { out.println(1); continue m; } for(int i=0;i<s2.length();i++) { char c=s2.charAt(i); if(c=='a') { f=1; break; } } if(f==1) { out.println(-1); continue m; } else { long ans=(long)Math.pow(2,s1.length()); out.println(ans); } } out.close(); } static double factorial(int n) { int i=1; double fact=1; while(i<=n) { fact=fact*i; i++; } return fact; } //findPermutation() method to find permutations i.e. nPr value static double findPermutation(int n, int r) { //finding nPr value double nPr=factorial(n)/factorial(n-r); //returning nPr value return nPr; } 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 checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
fd2d04532f176160d382612ba477163b
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
/*############################################################################################################ ########################################## >>>> Diaa12360 <<<< ############################################### ########################################### Just Nothing ################################################# #################################### If You Need it, Fight For IT; ######################################### ###############################################.-. 1 5 9 2 .-.################################################ ############################################################################################################*/ import java.io.*; import java.lang.reflect.Array; 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 // 200000 public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // in = new BufferedReader(new FileReader("src/input")); StringBuilder out = new StringBuilder(); StringTokenizer tk; int t = ints(in.readLine()); while (t-- > 0) { String s = in.readLine(); String temp = in.readLine(); char[] test = temp.toCharArray(); int count = 0; for (char x : test) { if (x == 'a') count++; } if (count == 0) { long ans = (long)Math.pow(2, s.length()); out.append(ans); } else if (count == 1 && test.length == 1) { out.append("1"); } else { out.append("-1"); } out.append('\n'); } System.out.println(out); } static List<List<Integer>> sortByColum(List<List<Integer>> arr) { List<List<Integer>> a = new ArrayList<>(); for (int i = 0; i < arr.size(); i++) { ArrayList<Integer> x = new ArrayList<>(); for (int j = 0; j < arr.get(i).size(); j++) { x.add(arr.get(j).get(i)); } a.add(x); } a.sort(Comparator.comparing(o -> o.get(0))); List<List<Integer>> a1 = new ArrayList<>(); for (int i = 0; i < a.size(); i++) { ArrayList<Integer> x = new ArrayList<>(); for (int j = 0; j < a.get(i).size(); j++) { x.add(a.get(j).get(i)); } a1.add(x); } return a1; } 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; } } class pair implements Comparable<pair> { int first; int second; pair(int f, int s) { first = f; second = s; } @Override public int compareTo(pair o) { return 0; } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
d90abb2af85cc697c44137f419197068
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class Main { static final int MOD = 1000000007; static InputReader in = new InputReader(System.in); static BufferOutput out = new BufferOutput(); public static void main(String[] args) { for(int i = in.nextInt();i>0;i--) { String s = in.next(); String s2 = in.next(); if(s2.contains("a") && s2.length() > 1) { System.out.println(-1); } else if(s2.equals("a")) { System.out.println(1); } else { long ans = 1; for(int o = 0;o<s.length();o++)ans*=2; System.out.println(ans); } } } static class BufferOutput { private DataOutputStream dout; final private int BUFFER_SIZE = 1 << 16; private byte[] buffer; private int pointer = 0; public BufferOutput() { buffer = new byte[BUFFER_SIZE]; dout = new DataOutputStream(System.out); } public BufferOutput(OutputStream out) { buffer = new byte[BUFFER_SIZE]; dout = new DataOutputStream(out); } public void writeBytes(byte arr[]) throws IOException { int bytesToWrite = arr.length; if (pointer + bytesToWrite >= BUFFER_SIZE) { flush(); } for (int i = 0; i < bytesToWrite; i++) { buffer[pointer++] = arr[i]; } } public void writeString(String str) throws IOException { writeBytes(str.getBytes()); } public void flush() throws IOException { dout.write(buffer, 0, pointer); dout.flush(); pointer = 0; } public void close() throws IOException{ dout.close(); } } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public String nextLine() { StringBuilder sb = new StringBuilder(); int b = readByte(); while (b != 10) { sb.appendCodePoint(b); b = readByte(); } if (sb.length() == 0) return ""; return sb.toString().substring(0, sb.length() - 1); } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
feda162eafe2620472e03213daf8d9ab
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; public class Codeforces786_3 { public static void main(String[] args) { Scanner input=new Scanner(System.in); int t=input.nextInt(); for(int i=0;i<t;i++){ String s= input.next(); String s1= input.next(); int flag=0; long count=1; for(int j=0;j<s1.length();j++){ if(s1.charAt(j)=='a'){ flag=1; } } if(flag==1&&s1.length()==1){ System.out.println(1); } else if(flag==1){ System.out.println(-1); } // else if(s.length()==1){ // System.out.println(2); // } else{ for(int j=0;j<s.length();j++){ count=count*2; } System.out.println(count); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
6799f34688dd43d6ad6b23faba4a20cc
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
/* !!!!Hello World,Prakhar here!!!! codechef handle prakhar_3011 codeforces handle prakhar_30 trying to get good at CP PEACE OUT......... */ /* */ import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { String s=sc.next(); String s2=sc.next();int count=0; char cs[]=s.toCharArray(); char ct[]=s2.toCharArray(); for(int i=0;i<ct.length;i++){ if(ct[i]=='a'){ count++; } } if(count==1&&count==ct.length){ System.out.println(1); } else if(count<=ct.length&&count!=0) { System.out.println(-1); } else { System.out.println((long)Math.pow(2, s.length())); } } } 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 revsort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } /* ......FAST SCANNER template taken from secondthread...... */ 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()); } 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; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
aeec31fa4e33337d2cb2f22edbd021ef
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.*; import java.lang.Math; import java.util.StringTokenizer; public class C { public static void main(String[] args) { int t; String str; char a[]; char b[]; FastReader in = new FastReader(); t = in.nextInt(); outer: while(t-- > 0) { str = in.next(); a = str.toCharArray(); b = in.next().toCharArray(); long ans = -1; if(b.length > 1) { for(int i = 0; i < b.length;i++) { if(a[0] == b[i]) { System.out.println(ans); continue outer; } } } if(b.length == 1 && a[0] == b[0]) { System.out.println(1); continue; } if(a.length >= 2) { ans = (long)Math.pow(2, a.length); System.out.println(ans); continue; }else{ System.out.println(2); } } } 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()); } int[] readArray(int n){ int[] a = new int[n]; for(int i = 0; i < n;i++){ a[i] = nextInt(); } return a; } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
8a7a530a41c49502ef0aceeb8aaac30c
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class A10 { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int q = Integer.parseInt(br.readLine()); while(q-->0) { String s = br.readLine(); String t = br.readLine(); long res = 0; if(t.contains("a")&&t.length()>1)res = -1; else if(t.equals("a")) res = 1; else res = (long)(Math.pow(2.0,(double)s.length())); pw.println(res); } pw.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
bdc07162775a6857b0d646847ba045eb
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.awt.datatransfer.StringSelection; import java.util.*; import javax.management.Query; import java.io.*; public class practice { static String s; static int n,k; static int[] a; static long[][] memo; 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 s1=sc.next(); String s2=sc.next(); if(s2.length()==1&&s2.charAt(0)=='a'){ pw.println(1); continue; } else if(s2.contains("a")){ pw.println(-1); } else { if(s1.length()>30) pw.println(1l*(1<<30)*(1<<s1.length()-30)); else pw.println((1<<s1.length())); } // pw.println(1l*(1<<25)); } pw.close(); } static long nCr(int n, int r) { return 1l*fact(n) / 1l*(fact(r) * fact(n - r)); } static long fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } private static double customLog(double base, double logNumber) { return Math.log(logNumber) / Math.log(base); } 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
45a4bacbc8b3dcb4d7d32211fbb17d38
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class Main { public static long fact(int n,int a){ if(a==0) return 1; long x = fact(n,a/2); x = x*x; if(a%2==1) x = x*n; return x; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); // int m = 1000000007; int t = sc.nextInt(); sc.nextLine(); while(t-->0){ String s = sc.nextLine(); String b = sc.nextLine(); char[] sa = s.toCharArray(); char[] ba = b.toCharArray(); int slen = sa.length; Arrays.sort(ba); if(ba[0]=='a'){ if(ba[ba.length-1]=='a'){ if(ba.length==1) System.out.println("1"); else System.out.println("-1"); }else System.out.println("-1"); }else{ System.out.println(fact(2,slen)); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
c6c141c0d22d515722dc34cd0ee9d29c
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
//package practice; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // Taking input int t = Integer.parseInt(br.readLine().trim()); while(t-- > 0) { // int n = Integer.parseInt(br.readLine()); // int n = Integer.parseInt(br.readLine().trim()); // String[] s = br.readLine().split(" "); String s1 = br.readLine(); String s2 = br.readLine(); char[] c = new char[26]; for(char c1:s2.toCharArray()) { c[c1 - 'a']++; } if(c[0] == s2.length()) { if(c[0] == 1) { bw.write(1+"\n"); } else { bw.write(-1+"\n"); } } else if(c[0] > 0) { bw.write(-1+"\n"); } // else if(s2.length() == 1) { // bw.write((s1.length()+1)+"\n"); // } else { long res = (long)Math.pow(2, s1.length()); bw.write(res+"\n"); } } bw.flush(); bw.close(); br.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
b4756fbc7084b7eb94fb53ae91d1cdad
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { new C().run(); } BufferedReader br; PrintWriter out; long mod = (long) (1e9 + 7), inf = (long) (3e18); class pair { int F, S; pair(int f, int s) { F = f; S = s; } } // ---------------------- solve -------------------------- void solve() { int t=1; t = ni(); // comment for no test cases while(t-- > 0) { //TODO: String s1= ns(); String s2=ns(); boolean rec=false; boolean containsA=false; for(int i=0; i<s2.length(); i++){ if(s2.charAt(i)=='a'){ containsA=true; } if(s2.charAt(i)=='a' && s2.length()>1){ rec=true; break; } } if(s1.length()>=1 && rec){ out.println(-1); continue; } // if(containsA){ // out.println(1); // continue; // } if(containsA && s1.length()>=1 || s2.length()==0){ out.println(1); continue; } out.println(pow(2, s1.length(), inf)); } } // -------- I/O Template ------------- public long pow(long A, long B, long C) { if(A==0) return 0; if(B==0) return 1; long n=pow(A, B/2, C); if(B%2==0){ return (long)((n*n)%C + C )%C; } else{ return (long)(((n*n)%C * A)%C +C)%C; } } char nc() { return ns().charAt(0); } String nLine() { try { return br.readLine(); } catch(IOException e) { return "-1"; } } double nd() { return Double.parseDouble(ns()); } long nl() { return Long.parseLong(ns()); } int ni() { return Integer.parseInt(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(int n) { long a[] = new long[n]; for(int i = 0; i < n; i++) a[i] = nl(); return a; } StringTokenizer ip; String ns() { if(ip == null || !ip.hasMoreTokens()) { try { ip = new StringTokenizer(br.readLine()); if(ip == null || !ip.hasMoreTokens()) ip = new StringTokenizer(br.readLine()); } catch(IOException e) { throw new InputMismatchException(); } } return ip.nextToken(); } void run() { try { if (System.getProperty("ONLINE_JUDGE") == null) { br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt")); out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt"); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } catch (FileNotFoundException e) { System.out.println(e); } solve(); out.flush(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
b9fe914fa12b7564ee72883b5eee0b3d
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class abhinandan6065_Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int quary = sc.nextInt(); while (quary-- > 0) { String ss = sc.next(); String ss2 = sc.next(); int len = ss2.length(); int n = ss.length(); long a = 0; long ans = 0; for (int i = 0; i < ss2.length(); i++) { if (ss2.charAt(i) == 'a') { a++; } } if (len == 1 && a == 1) { ans = 1; } else if (len > 1 && a > 0) { ans = -1; } else { ans = (long) 1 << n; } sb.append(ans + "\n"); } System.out.print(sb); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
7b7aaae87abb91bd7c0cf7d43fd762b7
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solution { static PrintWriter pw; static FastScanner s; public static void main(String[] args) throws Exception { pw=new PrintWriter(System.out); s=new FastScanner(System.in); int t=s.nextInt(); while(t-->0) { String str=s.next(); String replace=s.next(); boolean che=check(replace); if(str.length()>0 && replace.contains("a") && replace.length()>1 ) { pw.println(-1); continue; } else if(replace.length()==0) { pw.println(str.length()); } else { if(che) pw.println((long)Math.pow(2, str.length())); else pw.println(1); } } pw.flush(); } static boolean check(String str) { for(int i=0;i<str.length();i++) { if(str.charAt(i)!='a') return true; } return false; } } class FastScanner{ BufferedReader br; StringTokenizer st; public FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public FastScanner(String s) throws Exception { br = new BufferedReader(new FileReader(new File(s))); } public String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
b756d7e47cfac543c4d3c618ed612229
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
// package CP; import java.util.*; public class Practice { static long pow(long a,long b){ if(b==0) return 1; long ans=0L; if(b%2==0){ long val = pow(a,b/2); ans+=val*val; }else{ long val=pow(a,b/2); ans+=val*val*2; } return ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); sc.nextLine(); while(t--!=0){ String s=sc.nextLine(); String d=sc.nextLine(); int cnta=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='a') cnta++; } int cnt=0; for(int j=0;j<d.length();j++){ if(d.charAt(j)=='a') cnt++; } if(cnta==0){ System.out.println(1); }else if(cnt==0){ System.out.println(pow(2*1L,cnta*1L)); }else if((cnt==1 && cnt==d.length())){ System.out.println(1); }else{ System.out.println(-1); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
99ff3aedb28fb072c9e6bf6f32862bd8
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; import java.util.*; import static java.lang.System.out; import static java.lang.Math.*; public class pre273 { 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 class MultiSet<K> { TreeMap<K, Integer> map; MultiSet() { map = new TreeMap<>(); } void add(K a) { map.put(a, map.getOrDefault(a, 0) + 1); } boolean contains(K a) { return map.containsKey(a); } void remove(K a) { map.put(a, map.get(a) - 1); if (map.get(a) == 0) map.remove(a); } int occrence(K a) { return map.get(a); } K floor(K a) { return map.floorKey(a); } K ceil(K a) { return map.ceilingKey(a); } @Override public String toString() { ArrayList<K> set = new ArrayList<>(); for (Map.Entry<K, Integer> i : map.entrySet()) { for (int j = 1; j <= i.getValue(); j++) set.add(i.getKey()); } return set.toString(); } } static class Pair<K, V> { K value1; V value2; Pair(K a, V b) { value1 = a; value2 = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair<?, ?> p = (Pair<?, ?>) o; return Objects.equals(this.value1, p.value1) && Objects.equals(this.value2, p.value2); } @Override public int hashCode() { int result = this.value1.hashCode(); result = 31 * result + this.value2.hashCode(); return result; } @Override public String toString() { return ("[" + value1 + " <=> " + value2 + "]"); } } static ArrayList<Integer> primes; static void setPrimes(int n) { boolean p[] = new boolean[n]; Arrays.fill(p, true); for (int i = 2; i < p.length; i++) { if (p[i]) { primes.add(i); for (int j = i * 2; j < p.length; j += i) p[j] = false; } } } static int mod = (int) (1e9 + 7); static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static void main(String args[]) { FastReader obj = new FastReader(); int tc = obj.nextInt(); while(tc--!=0){ char s[] = obj.next().toCharArray(),t[] = obj.next().toCharArray(); boolean inf = false; for(int i=0;i<t.length;i++) if(t[i]=='a') inf = true; if(inf && t.length>1){ out.println(-1); }else if(inf){ out.println(1); } else { out.println(1l<<s.length); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
c250e5813f1c6e7fc70dd9b47257fd32
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0) { String a = scan.next(); String b = scan.next(); int n = a.length(); if(a.length() == 0) { System.out.println(0); } else if(b.indexOf('a') == -1) { System.out.println((long)Math.pow(2, n)); } else if(b.equals("a")) { System.out.println(1); } else { System.out.println(-1); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
f58740e5d0d40f8de7a2af2f54886df2
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class Main { //static int [] arr; //static boolean prime[] = new boolean[1000]; //static int l; //static String s; static StringBuilder sb; //static HashSet<L> hs; //static HashSet<Long> hs = new HashSet<Long>(); // static int ans; // static boolean checked []; static final int mod = 1000000007; //static int[][] dp; static boolean visited[][]; //static int[] w ,v; //static int n; // static int arr[][]; //static long ans; static Scanner sc; static PrintWriter out; static int n,m,w,t; //static char [] a , b; static StringBuilder sb; static int ans; static int grid [] []; public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); out = new PrintWriter(System.out); t =sc.nextInt(); while(t-->0) { String s = sc.next(); String t = sc.next(); int ca = 0; for (char c : s.toCharArray()) if(c=='a') ca++; boolean ta = false; for (char c : t.toCharArray()) if(c == 'a') ta = true; //out.println(ca); if(t.equals("a")) out.println(1); else if(ta) { out.println(-1); } else { long res =(long) Math.pow(2, ca); out.println(res); } // abca ccbca abccc ccbcc } out.close(); } public static void solve() { //out.println(cost);, } // public static int trace(int idx,int cost){ // if(cost> t || idx == m) // return 0; // int take = v[idx] + solve(idx+1,cost + 3*w*d[idx]); // //int leave = solve(idx+1,t); // if(dp[idx][cost] == take){ // sb.append(d[idx]+" "+v[idx]+"\n"); // return 1 + trace(idx+1,cost+3*w*d[idx]); // } // // return trace(idx+1,cost); // } private static void reverse(long[] arr) { // TODO Auto-generated method stub } // recursive implementation static long arrayLCM(int[] arr, int idx) { // lcm(a,b) = (a*b/gcd(a,b)) if (idx == arr.length - 1){ return arr[idx]; } long a = arr[idx]; long b = arrayLCM(arr, idx+1); return (a*b/gcd(a,b)); // } static int longestSubarrWthSumDivByK(int arr[], int n, int k) { // unordered map 'um' implemented as // hash table HashMap<Integer, Integer> um= new HashMap<Integer, Integer>(); // 'mod_arr[i]' stores (sum[0..i] % k) int mod_arr[]= new int[n]; int max_len = 0; long curr_sum = 0; // traverse arr[] and build up the // array 'mod_arr[]' for (int i = 0; i < n; i++) { curr_sum += arr[i]; // as the sum can be negative, // taking modulo twice mod_arr[i] = (int)((curr_sum % k) + k) % k; // if true then sum(0..i) is // divisible by k if (mod_arr[i] == 0) // update 'max' max_len = i + 1; // if value 'mod_arr[i]' not present in 'um' // then store it in 'um' with index of its // first occurrence else if (um.containsKey(mod_arr[i]) == false) um.put(mod_arr[i] , i); else // if true, then update 'max' if (max_len < (i - um.get(mod_arr[i]))) max_len = i - um.get(mod_arr[i]); } // return the required length of longest subarray // with sum divisible by 'k' return max_len; } static int longestSubArrayOfSumK(int[] arr, int n, int k) { // HashMap to store (sum, index) tuples HashMap<Integer, Integer> map = new HashMap<>(); int sum = 0, maxLen = 0; // traverse the given array for (int i = 0; i < n; i++) { // accumulate sum sum += arr[i]; // when subarray starts from index '0' if (sum == k) maxLen = i + 1; // make an entry for 'sum' if it is // not present in 'map' if (!map.containsKey(sum)) { map.put(sum, i); } // check if 'sum-k' is present in 'map' // or not if (map.containsKey(sum - k)) { // update maxLength if (maxLen < (i - map.get(sum - k))) maxLen = i - map.get(sum - k); } } return maxLen; } static boolean isPrime(long n) { if (n == 2 || n == 3) return true; if (n <= 1 || n % 2 == 0 || n % 3 == 0) return false; // To check through all numbers of the form 6k ± 1 for (long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static long smallestDivisor(long n){ // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (long i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } static long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(int n) { long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static boolean isSorted (int [] arr) { for(int i = 0 ; i < arr.length -1 ; i++) if(arr[i]>arr[i+1]) return false; return true; } // static void findsubsequences(String s, String ans){ // if (s.length() == 0) { // if(ans!="") // if(ans.length()!=l) // al.add(Long.parseLong(ans)); // return; // } // // // We add adding 1st character in string // findsubsequences(s.substring(1), ans + s.charAt(0)); // // // Not adding first character of the string // // because the concept of subsequence either // // character will present or not // findsubsequences(s.substring(1), ans); //} static void sieve(int n,boolean[] prime,List<Integer> al) { for(int i=0;i<=n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p]) { al.add(p); for(int i = p*p; i <= n; i += p) prime[i] = false; } } } //Print all prime numbers // for(int i = 2; i <= n; i++) // { // if(prime[i] == true) // System.out.print(i + " "); // } public static void reverse(Object [] arr) { int i = 0; int j = arr.length-1; while(i<j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } } class Pair implements Comparable <Pair>{ int a ; int b ; public Pair(int a , int b) { this.a = a; this.b = b; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.a-o.a; } public String toString() { return "( " + this.a + " , " + this.b + " )\n"; } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public int[][] nextInt2DArr(int l, int w) throws IOException { int [][] arr = new int[l][w]; for(int i = 0 ; i < l ; i++) for(int j = 0 ; j<w ; j++) arr[i][j]= Integer.parseInt(next()); return arr; } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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 { return Double.parseDouble(next()); } public int[] nextIntArr(int length) throws IOException { int[] arr = new int[length]; for(int i = 0 ; i < length ; i++) arr[i] = Integer.parseInt(next()); return arr; } public long[] nextLongArr(int length) throws IOException { long[] arr = new long[length]; for(int i = 0 ; i < length ; i++) arr[i] = Long.parseLong(next()); return arr; } public boolean ready() throws IOException { return br.ready(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
8e08b33951b59cdeac07d3d07377faa1
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class C_Infinite_Replacement { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); // try { // out = new PrintWriter("output.txt"); // } catch (Exception e) { // e.printStackTrace(); // } int test = in.nextInt(); while (test-- > 0) { String s = in.next(); String t = in.next(); if (t.equals("a")) out.println(1); else if (t.indexOf("a") != -1) out.println(-1); else out.println((long) Math.pow(2, s.length())); } // if t contains a then we always have chance to again replace that 'a' hence infinite possibilities // but if t has only 1 char a then we get the same string again and again hence only 1 unique string // in other cases replace all a one by one till we exhaust all a's // so for each a we have 2 options replace or don't replace 2 * 2 * 2.... = 2 ^ s.length() out.flush(); out.close(); } } // For fast input output class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { // br = new BufferedReader(new InputStreamReader(System.in)); try { br = new BufferedReader(new FileReader("input.txt")); } catch(Exception e) { 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[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } String[] readStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } } // end of fast i/o code
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
1d869682ce6a36f1a6f4e15aa5c84570
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
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 Main { static FastScanner scan = new FastScanner(); public static void main(String[] args) throws Exception { int n = scan.nextInt(); for(int i = 0; i < n; i++) { String x = scan.next(); String y = scan.next(); if(y.contains("a") && y.length() > 1) { System.out.println(-1); } else if (y.contains("a") && y.length() == 1) { System.out.println(1); } else { long answer = (long) Math.pow(2,x.length()); System.out.println(answer); } } } 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: -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; } } 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); } } public 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
1cbc234e77d742c6086ef586c70e3fad
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { public Main() { FastScanner input = new FastScanner(System.in); StringBuilder output = new StringBuilder(); int q = input.nextInt(); for (int i = 0; i < q; i++) { String s = input.next(); String t = input.next(); if (t.contains("a")){ if (t.length() > 1) { output.append(-1); } else { output.append(1); } } else { output.append((long) Math.pow(2, s.length())); } output.append("\n"); } System.out.println(output); } public static void main(String[] args) { new Main(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(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 int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
6b06773f573be2736ea401fd3b4639c6
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class Main { /* static int parent[]; static int rank[]; static int dy[] = new int[]{1,-1,0,0}; static int dx[] = new int[]{0,0,1,-1}; */ public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); while(t-- > 0){ String s = sc.nextLine(); String p = sc.nextLine(); int cnt = 0; for(int i=0;i<p.length();i++){ if(p.charAt(i)=='a') cnt++; } if(cnt>0){ if(p.length() == 1) System.out.println("1"); else { System.out.println("-1"); } } else { long ans = (long)Math.pow(2,s.length()); System.out.println(ans); } } } } /* DIJKSHTRA'S PriorityQueue<int[]> pq = new PriorityQueue<>((a,b)->(a[2]-b[2])); pq.add(new int[]{0,0,0}); int dis[][] = new int[n][n]; for(int a[]:dis) Arrays.fill(a,Integer.MAX_VALUE); dis[0][0] = 0; while(!pq.isEmpty()){ int temp[] = pq.poll(); int row = temp[0]; int col = temp[1]; for(int i=0;i<4;i++){ int r = row+dy[i]; int c = col+dx[i]; if(r>=0 && r<n && c>=0 && c<n){ int time = grid[row][col]>=grid[r][c]?1:grid[r][c]-grid[row][col]+1; if(dis[r][c] > temp[2]+time){ dis[r][c] = temp[2]+time; pq.add(new int[]{r,c,dis[r][c]}); } } } } DIJKSHTRA'S */ /* FIND PARENT public static int findParent(int s){ if(parent[s] == s) return s; return parent[s] = findParent(parent[s]); } FIND PARENT */ /* UNION public static void union(int a,int b){ a = findParent(a); b = findParent(b); if(rank[a] > rank[b]) parent[b] = a; else if(rank[b] > rank[a]) parent[b] = a; else { parent[b] = a; rank[a]++; } parent[a] = b; } UNION */ /* KMP_STRING_MATCHING public static int KMPSearch(String txt, String pat) { 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) { return i-j; } else if (i < N && pat.charAt(j) != txt.charAt(i)) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } return -1; } public static 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++; } } } } KMP_STRING_MATCHING */
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
8013be0436a1fad696dfe5eb5b542dca
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; import java.lang.reflect.Array; public class CodeForces { class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader(new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { int t = (int) Math.random() * a.length; int x = a[t]; a[t] = a[i]; a[i] = x; } } long lcm(long a, long b) { long val = a * b; return val / gcd(a, b); } void swap(long a[], long[] b, int i) { long temp = a[i]; a[i] = b[i]; b[i] = temp; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long mod = (long) 1e9 + 7, c; FastReader in; PrintWriter o; public static void main(String[] args) throws IOException { new CodeForces().run(); } void run() throws IOException { in = new FastReader(); OutputStream op = System.out; o = new PrintWriter(op); int t; t = 1; t = in.nextInt(); for (int i = 1; i <= t; i++) { helper(); o.println(); } o.flush(); o.close(); } public void helper() { String s=in.nextLine(); String t=in.nextLine(); if(t.length()>1 && t.contains("a")){ o.print(-1); } else if(t.length()==1 && t.charAt(0)=='a'){ o.print(1); } else{ long ans=(long)1<<s.length(); o.print(ans); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
31fb5ae5108ce87b13a10a7ee84cad0e
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
//package com.company; import java.util.*; import java.io.*; public class InfiniteReplacement { private static long solve(String s, String t) { if(t.equals("a")) return 1; else if(t.contains("a")) return -1; return (long)Math.pow(2, s.length()); } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(br.readLine()); while(--q >= 0) { String s = br.readLine(); String t = br.readLine(); System.out.println(solve(s, t)); } br.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
c9679f807f049e6653f0a44ec969f1c9
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class Cf22 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); for(int i =0;i<k;i++) { String a = sc.next(); String s = sc.next(); if(s.contains("a") && s.length()>1) { System.out.println(-1); } else if (s.contains("a")){ System.out.println(1); } else { System.out.println((long)(Math.pow(2,a.length()))); } } } } // // // //public static void 3(String[] args) { // Scanner sc = new Scanner(System.in); // int k = sc.nextInt(); // for(int i =0;i<k;i++) { // int n = sc.nextInt(); // int min = Integer.MAX_VALUE; // ArrayList<Integer> ar = new ArrayList<>(); // for(int j=0;j<n;j++) { // int u = sc.nextInt(); // min = Math.min(u, min); // ar.add(u); // } // int res =0; // for(int j=0;j<n;j++) { // res+= ar.get(j)-min; // } // System.out.println(res); // // // } // //}
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
55ba231b9f82d74645de8e86097c96d8
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; public class InfiniteReplacement { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); while (n-- > 0) { String str = sc.next(); String t = sc.next(); String ans = ""; int as = 0; for (int i = 0 ;i < t.length() ; i++) { if (t.charAt(i) == 'a') { // ans += sad(str,t,i,i); as++; } } long s = (long)Math.pow(2, str.length()); System.out.println(t.equals("a")?1:as > 0 ?-1 : s); } } public static String sad(String str, String v, int pos1, int pos2) { String start = str.substring(0, pos1); start += v; start += str.substring(pos2 + 1, str.length()); return start; } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
740aad71a14676cc1c75a9b66e393b96
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class InfiniteReplacement { public static PrintWriter out; public static void main(String[] args)throws IOException{ Scanner sc=new Scanner(); out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { String str=sc.next(); String str1=sc.next(); int count=1; if(str1.charAt(0)=='a'&&str.contains("a")&&str1.length()>1) { out.println(-1); continue; }else if(!str1.contains("a")) { int count1=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='a') { count1++; } } out.println((long)(Math.pow(2, count1))); }else if(str1.charAt(0)=='a'&&str.contains("a")&&str1.length()==1) { out.println(1); continue; }else { out.println(-1); } } out.close(); } public static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
fcd850f3afbfa5b0ad684591d80575a4
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class Solution{ public static void main(String args[]){ FastReader sc = new FastReader(); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < t; i++){ String str1 = sc.nextLine().trim(); String str2 = sc.nextLine().trim(); long temp = 0; if(str2.indexOf("a") >= 0){ if(str2.length() == 1){ sb.append(1).append("\n"); }else{ sb.append(-1).append("\n"); } continue; } temp = (long) Math.pow(2, str1.length()); sb.append(temp).append("\n"); } System.out.println(sb); } 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
6cf566c54b78ee82f64ef2267b897e88
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; /* * Author: Atuer */ public class Main { // ==== Solve Code ====// static int INF = 2000000010; public static void csh() { } public static void main(String[] args) throws IOException { // csh(); int t = in.nextInt(); while (t-- > 0) { // out.printf("%.0f\n",Math.pow(2l, 50)); solve(); out.flush(); } out.close(); } public static void solve() { char[] a = in.next().toCharArray(); char[] b = in.next().toCharArray(); // out.println(a.length); boolean aflag = false; for (int i = 0; i < b.length; i++) { if (b[i] == 'a') aflag = true; } if (aflag && b.length == 1) { out.println(1); } else if (aflag) { out.println(-1); } else { out.println((long) Math.pow(2, a.length)); } } public static class Node { int x, y, k; public Node(int x, int y, int k) { this.x = x; this.y = y; this.k = k; } } // ==== Solve Code ====// // ==== Template ==== // public static long cnm(int a, int b) { long sum = 1; int i = a, j = 1; while (j <= b) { sum = sum * i / j; i--; j++; } return sum; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static void gbSort(int[] a, int l, int r) { if (l < r) { int m = (l + r) >> 1; gbSort(a, l, m); gbSort(a, m + 1, r); int[] t = new int[r - l + 1]; int idx = 0, i = l, j = m + 1; while (i <= m && j <= r) if (a[i] <= a[j]) t[idx++] = a[i++]; else t[idx++] = a[j++]; while (i <= m) t[idx++] = a[i++]; while (j <= r) t[idx++] = a[j++]; for (int z = 0; z < t.length; z++) a[l + z] = t[z]; } } // ==== Template ==== // // ==== IO ==== // static InputStream inputStream = System.in; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(System.out); 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(); } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } // ==== IO ==== // }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
5a8cb9449dc70a72fa2da3a8f8dfef64
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; /* * Author: Atuer */ public class Main { // ==== Solve Code ====// static int INF = 2000000010; public static void csh() { } public static void main(String[] args) throws IOException { // csh(); int t = in.nextInt(); while (t-- > 0) { // out.printf("%.0f\n",Math.pow(2l, 50)); solve(); out.flush(); } out.close(); } public static void solve() { char[] a = in.next().toCharArray(); char[] b = in.next().toCharArray(); // out.println(a.length); boolean aflag = false; for (int i = 0; i < b.length; i++) { if (b[i] == 'a') aflag = true; } if (aflag && b.length == 1) { out.println(1); } else if (aflag) { out.println(-1); } else { out.println((long) Math.pow(2, a.length)); } } public static class Node { int x, y, k; public Node(int x, int y, int k) { this.x = x; this.y = y; this.k = k; } } // ==== Solve Code ====// // ==== Template ==== // public static long cnm(int a, int b) { long sum = 1; int i = a, j = 1; while (j <= b) { sum = sum * i / j; i--; j++; } return sum; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static void gbSort(int[] a, int l, int r) { if (l < r) { int m = (l + r) >> 1; gbSort(a, l, m); gbSort(a, m + 1, r); int[] t = new int[r - l + 1]; int idx = 0, i = l, j = m + 1; while (i <= m && j <= r) if (a[i] <= a[j]) t[idx++] = a[i++]; else t[idx++] = a[j++]; while (i <= m) t[idx++] = a[i++]; while (j <= r) t[idx++] = a[j++]; for (int z = 0; z < t.length; z++) a[l + z] = t[z]; } } // ==== Template ==== // // ==== IO ==== // static InputStream inputStream = System.in; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(System.out); 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(); } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } // ==== IO ==== // }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
7237b5f893275ee25acf93af9dcd73da
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; 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.StringTokenizer; public class CodeForces { static int mod = (int)1e9+7; public static void main(String[] args) throws InterruptedException { // PrintWriter out = new PrintWriter("output.txt"); // File input = new File("input.txt"); // FastScanner fs = new FastScanner(input); FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int testNumber =fs.nextInt(); for (int T =0;T<testNumber;T++){ String s =fs.next(); String t =fs.next(); if(t.equals("a")){ out.print("1\n"); }else if (t.contains("a")){ out.print("-1\n"); } else { out.print(FastPower((long)2, (long)s.length())+"\n"); } } out.flush(); } static long modInverse( long n, long p) { return FastPower(n, p - 2, p); } static int[] factorials(int max,int mod){ int [] ans = new int[max+1]; ans[0]=1; for (int i=1;i<=max;i++){ ans[i]=ans[i-1]*i; ans[i]%=mod; } return ans; } static String toBinary(int num,int bits){ String res =Integer.toBinaryString(num); while(res.length()<bits)res="0"+res; return res; } static String toBinary(long num,int bits){ String res =Long.toBinaryString(bits); while(res.length()<bits)res="0"+res; return res; } static long LCM(long a,long b){ return a*b/gcd(a,b); } static long FastPower(long x,long p,long mod){ if(p==0)return 1; long ans =FastPower(x, p/2,mod); ans%=mod; ans*=ans; ans%=mod; if(p%2==1)ans*=x; ans%=mod; return ans; } static double FastPower(double x,int p){ if(p==0)return 1.0; double ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static int FastPowerInt(int x,int p){ if(p==0)return 1; int ans =FastPowerInt(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static long FastPower(long x,long p){ if(p==0)return 1; long ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public FastScanner(File f){ try { br=new BufferedReader(new FileReader(f)); st=new StringTokenizer(""); } catch(FileNotFoundException e){ br=new BufferedReader(new InputStreamReader(System.in)); 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()); } 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; } long[] readLongArray(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()); } } public static long factorial(int n){ if(n==0)return 1; return (long)n*factorial(n-1); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void sort (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sortReversed (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sort (long[]a){ ArrayList<Long> b = new ArrayList<>(); for(long i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static ArrayList<Integer> 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; } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } static int binarySearchSmallerOrEqual(int arr[], int key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } static int binarySearchSmallerOrEqual(long arr[], long key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } public static int binarySearchStrictlySmaller(int[] arr, int target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } public static int binarySearchStrictlySmaller(long[] arr, long target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static int binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static void init(int[]arr,int val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static void init(int[][]arr,int val){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ arr[i][j]=val; } } } static void init(long[]arr,long val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static<T> void init(ArrayList<ArrayList<T>>arr,int n){ for(int i=0;i<n;i++){ arr.add(new ArrayList()); } } static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target) { int start = 0, end = arr.size()-1; if(end == 0) return -1; if (target > arr.get(end).y) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid).y >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearchStrictlyGreater(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] <= target) { start = mid + 1; } else { ans = mid; end = mid - 1; } } return ans; } public static long pow(long n, long pow) { if (pow == 0) { return 1; } long retval = n; for (long i = 2; i <= pow; i++) { retval *= n; } return retval; } static String reverse(String s){ StringBuffer b = new StringBuffer(s); b.reverse(); return b.toString(); } static String charToString (char[] arr){ String t=""; for(char c :arr){ t+=c; } return t; } int[] copy (int [] arr , int start){ int[] res = new int[arr.length-start]; for (int i=start;i<arr.length;i++)res[i-start]=arr[i]; return res; } static int[] swap(int [] A,int l,int r){ int[] B=new int[A.length]; for (int i=0;i<l;i++){ B[i]=A[i]; } int k=0; for (int i=r;i>=l;i--){ B[l+k]=A[i]; k++; } for (int i=r+1;i<A.length;i++){ B[i]=A[i]; } return B; } static int mex (int[] d){ int [] a = Arrays.copyOf(d, d.length); sort(a); if(a[0]!=0)return 0; int ans=1; for(int i=1;i<a.length;i++){ if(a[i]==a[i-1])continue; if(a[i]==a[i-1]+1)ans++; else break; } return ans; } static int[] mexes(int[] arr){ int[] freq = new int [100000+7]; for (int i:arr)freq[i]++; int maxMex =0; for (int i=0;i<=100000+7;i++){ if(freq[i]!=0)maxMex++; else break; } int []ans = new int[arr.length]; ans[arr.length-1] = maxMex; for (int i=arr.length-2;i>=0;i--){ freq[arr[i+1]]--; if(freq[arr[i+1]]<=0){ if(arr[i+1]<maxMex) maxMex=arr[i+1]; ans[i]=maxMex; } else { ans[i]=ans[i+1]; } } return ans; } static int [] freq (int[]arr,int max){ int []b = new int[max]; for (int i:arr)b[i]++; return b; } static int[] prefixSum(int[] arr){ int [] a = new int[arr.length]; a[0]=arr[0]; for (int i=1;i<arr.length;i++)a[i]=a[i-1]+arr[i]; return a; } static class Pair { int x; int y; int extra; public Pair(int x,int y){ this.x=x; this.y=y; } public Pair(int x,int y,int extra){ this.x=x; this.y=y; this.extra=extra; } // @Override // public boolean equals(Object o) { // if(o instanceof Pair){ // if(o.hashCode()!=hashCode()){ // return false; // } else { // return x==((Pair)o).x&&y==((Pair)o).y; // } // } // // return false; // // } // // // // // // @Override // public int hashCode() { // return x+(int)y*2; // } // // } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
4aecaf3774037da127f56cab02be34c3
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code h Scanner sc = new Scanner(System.in); int t = sc.nextInt(); // String[] arr = new String[651]; // for(char i ='a';i<'z';i++){ // for (char j = 'a'; j < 'z'; j++) { // if(i==j){continue;} // else{ // // } // } // } for (int i = 0; i <t ; i++) { String s = sc.next(); String q = sc.next(); System.out.println(solve(s,q)); } } public static long solve(String s,String t){ if(t.equals("a")){ return 1; } for(char c:t.toCharArray()){ if(c=='a'){ return -1; } } return (1L<<s.length()); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
4c63b39343253345e8ca5fcbc2c617e4
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import org.w3c.dom.ls.LSOutput; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- != 0){ String first = scan.next(); String second = scan.next(); boolean isContainsA = false; boolean isContainsOthers = false; long numberOfStrings = 1; int countA = 0; for(int i = 0 ; i < second.length() ; i++){ if(second.charAt(i) == 'a') { isContainsA = true; countA++; } else isContainsOthers = true; } if((isContainsA && isContainsOthers) || countA >1) numberOfStrings = -1; else if (isContainsA ) numberOfStrings = 1; else numberOfStrings = (long)Math.pow(2,first.length()); System.out.println(numberOfStrings); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
13a0a0177f0efb6c705419b651fa75ee
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
//---#ON_MY_WAY--- //---#THE_SILENT_ONE--- import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class C { static FastReader x = new FastReader(); static OutputStream outputStream = System.out; static PrintWriter out = new PrintWriter(outputStream); /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) throws NumberFormatException, IOException { long startTime = System.nanoTime(); int mod = 1000000007; int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { String s = x.next(); String k = x.next(); if(k.length()==1&&k.charAt(0)=='a') str.append(1); else { if(k.contains("a")) str.append(-1); else { long ans = pow(2, s.length()); str.append(ans); } } str.append("\n"); t--; } out.println(str); out.flush(); long endTime = System.nanoTime(); //System.out.println((endTime-startTime)/1000000000.0); } /*--------------------------------------------FAST I/O-------------------------------*/ 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; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------HELPER---------------------------------*/ static class pair implements Comparable<pair> { int x, y; public pair(int a, int b) { x = a; y = b; } @Override public int hashCode() { int hash = 3; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final pair other = (pair) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(C.pair o) { if(this.x==o.x) return this.y-o.y; return this.x-o.x; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sortint(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); } return a; } static long[] sortlong(long a[]) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < al.size(); i++) { a[i] = al.get(i); } return a; } static long pow(long x, long y) { long result = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y = y / 2; } else { result = result * x; y = y - 1; } } return result; } static long pow(long x, long y, long mod) { long result = 1; x %= mod; while (y > 0) { if (y % 2 == 0) { x = (x % mod * x % mod) % mod; y /= 2; } else { result = (result % mod * x % mod) % mod; y--; } } return result; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int etf(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res /= i; res *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res /= n; res *= (n - 1); } return res; } 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); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
14b9eb4af5e9b7653243cf1abd4214a0
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; /** * @author Sabirov Jahongir **/ public class Other { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int test = cin.nextInt(); while (test -- > 0) { String first = cin.next(); String second = cin.next(); if(second.contains("a") && second.length() >= 2 ) { System.out.println(-1); }else{ if(second.equals("a")){ System.out.println(1); }else{ System.out.println(1L << first.length()); } } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
36b58d1fc422df78a8f9a73934d8b9e8
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.*; import java.lang.*; import java.io.*; public class codeforces_round_786_C { static BufferedReader br; static PrintWriter out; static StringTokenizer st; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); int z=nextInt(); while(z-->0){ String s=next(); String t=next(); double sa=0; int ta=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='a'){ sa++; } } for(int i=0;i<t.length();i++){ if(t.charAt(i)=='a'){ ta++; } } if(ta>1||(ta==1&&t.length()>1)){ System.out.println(-1); continue; } if(ta==1){ System.out.println(1); continue; } if(ta==0){ long ans=(long)Math.pow(2, sa); System.out.println(ans); } } out.close(); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static long nextLong() throws IOException { return Long.parseLong(next()); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static char nextCharacter() throws IOException { return next().charAt(0); } static String nextLine() throws IOException { return br.readLine().trim(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
342a8988cc29471592174d80181a432e
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
// package CodeforcesRound786Div3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastReader fr = new FastReader(); int t = fr.nextInt(); while (t-- > 0) { String s1 = fr.nextLine(); String s2 = fr.nextLine(); int countA = 0; for (int i = 0; i < s2.length(); i++) { if (s2.charAt(i) == 'a') { countA++; } } if (s1.length() >= 1 && countA > 0 && countA <= s2.length() && s2.length() > 1) { System.out.println(-1); } else if (s1.length() == 1 && s2.length() >= 1 && countA == 0) { System.out.println(2); } else { if (s2.length() == 1 && countA == 1) { System.out.println(1); } else { long total = (long) Math.pow(2, s1.length()); System.out.println(total); } } } } public int gcd(int a, int b) { if (b==0) return a; return gcd(b,a%b); } 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
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
eac630acac4266bc9cb9a5bd86dff1fe
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class Codeforces { public static long power(int x, int y) { long temp; if (y == 0) return 1; if (y == 1) return x; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int q = sc.nextInt(); while(q-- > 0) { String s = sc.next(); String t = sc.next(); int ta = 0; for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == 'a') ta++; } if(ta == 0) { System.out.println(power(2, s.length())); } else if (ta == 1 && t.length() == 1) { System.out.println(1); } else { System.out.println(-1); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
c04e207bc599d86b84b3f18c1fc92db0
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { // try { // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream(new FileOutputStream("output.txt"))); // } catch (Exception e) { // System.err.println("Error"); // } Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); while(t-- > 0) { String s = in.nextLine(); String b = in.nextLine(); System.out.println(solve(s, b)); } } static long solve(String s, String t) { boolean check = false; int cnt = 0; for(char ch : t.toCharArray()) { if(ch == 'a') check = true; } for(char ch : s.toCharArray()) { if(ch == 'a') cnt++; } if(check) { if(t.equals("a")) return 1; if(t.length() > 1) return -1; } return (long)Math.pow(2, s.length()); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
db2847fb94b88f80e58df33216072e81
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.PrintWriter; import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /* * 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b */ public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); long tcs = in.nextInt(); in.nextLine(); while (tcs-- > 0) { String s = in.nextLine(); String t = in.nextLine(); if (t.equals("a")) { out.println(1); } else if (t.contains("a")) { out.println(-1); } else { long n = s.length(); BigInteger fact = BigInteger.ZERO; char[] arr = s.toCharArray(); for (int i = 0; i < n; i++) { arr[i] = 'b'; fact = fact.add(countDistinctPermutations(new String(arr))); } out.println(fact.add(BigInteger.ONE)); } } out.close(); in.close(); } static final int MAX_CHAR = 26; // Utility function to find factorial of n. static Map<Integer, BigInteger> cache = new HashMap<>(); public static BigInteger factorial(int value){ if(value < 0){ throw new IllegalArgumentException("Value must be positive"); } if (cache.containsKey(value)) { return cache.get(value); } BigInteger result = BigInteger.ONE; for (int i = 2; i <= value; i++) { result = result.multiply(BigInteger.valueOf(i)); } cache.put(value, result); return result; } // Returns count of distinct permutations // of str. static BigInteger countDistinctPermutations(String str) { int length = str.length(); int[] freq = new int[MAX_CHAR]; // finding frequency of all the lower case // alphabet and storing them in array of // integer for (int i = 0; i < length; i++) if (str.charAt(i) >= 'a') freq[str.charAt(i) - 'a']++; // finding factorial of number of appearances // and multiplying them since they are // repeating alphabets BigInteger fact = BigInteger.ONE; for (int i = 0; i < MAX_CHAR; i++) fact = fact.multiply(factorial(freq[i])); // finding factorial of size of string and // dividing it by factorial found after // multiplying return factorial(length).divide(fact); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
c9b263cb24bf8816b6ce834dff16d99f
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.BigInteger; public class C_Infinite_Replacement { static Scanner scn = new Scanner(System.in); public static void iamganduwhocodes() { String str1 = scn.next(); String str2 = scn.next(); long size = str1.length(); long res = 1 * (long) Math.pow(2, size); if (str2.equals("a")) System.out.println(1); else { boolean flag = false; for (int i = 0; i < str2.length(); i++) if (str2.charAt(i) == 'a') flag = true; if (flag) System.out.println(-1); else System.out.println(res); } } public static void main(String[] args) throws java.lang.Exception { long tuchal = scn.nextLong(); while (tuchal-- > 0) { iamganduwhocodes(); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
321f70a18069127ac2155b47233ed876
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class Main2 { static void solve() { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { // int a = scan.nextInt(); String s = scan.next(); String ss = scan.next(); // int[] arr = new int[n]; // for (int j = 0; j < n; j++) { // arr[j] = scan.nextInt(); // } solveCase(s, ss); } } static void solveCase(String s, String t) { if(t == null || t.length() == 0) { System.out.println(1); return; } int cntA = 0; for(char ch : t.toCharArray()) { if(ch == 'a') { cntA++; } } int cntOther = t.length() - cntA; if(cntOther == 0) { if(cntA == 1) { System.out.println(1); } else { System.out.println(-1); } return; } if(cntA > 0) { System.out.println(-1); } else { long val = (long) (Math.pow(2,s.length())); System.out.println(val); // if(s.length() == 1) { // System.out.println(2); // } else { // // } } } public static void main(String[] args) { solve(); } static class Node implements Comparable<Node> { long val; Node parent; List<Node> children; long max; boolean isLeave; public Node(int val) { this.val = val; this.max = val; isLeave = true; children = new ArrayList<>(); } @Override public int compareTo(Node node) { if(this.max < node.max) { return -1; } else if(this.max > node.max) { return 1; } return 0; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
fc0f38bf6d93f7006c7f9dbe64b3b726
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; import java.util.*; public class C { public static long solve(Scanner sc) { String s = sc.next(); String t = sc.next(); if (t.equals("a")) return 1; boolean bool = false; int cnt = 0; for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == 'a'){ bool = true; cnt++; } } if (bool) return -1; long res = (long)Math.pow(2, s.length()); return res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { System.out.println(solve(sc)); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
f93684e3a6437f43e57e12b23b567445
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.awt.image.ImageProducer; import java.util.*; public class Solution { static boolean prime[] = new boolean[1000001]; static Set<Long> cubes=new HashSet<>(); static { long N = 1000000000000L; // // // for(int i=1;i*i<=n;i++) // { // long x=i*i; // set.add(x); // } for (long i = 1; i * i * i <= N; i++) { cubes.add(i * i * i); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); sc.nextLine(); while(t-->0) { String s=sc.nextLine(); String st=sc.nextLine(); boolean aflag=false; boolean oflag=false; for(int i=0;i<st.length();i++) { if(st.charAt(i)=='a') { aflag=true; } if(st.charAt(i)!='a') { oflag=true; } } if(st.length()==1 && st.charAt(0)=='a') { System.out.println(1); } else if(aflag==true) { System.out.println(-1); } else { System.out.println((long)Math.pow(2,s.length())); } } } public static boolean isPal(String str) { for(int i=0;i<str.length()/2;i++) { if (str.charAt(i) != str.charAt(str.length() - 1 - i)) return false; } return true; } // public static int[] reverse(int arr[],int start,int end) // { // for(int i=start;i<=end;i++) // { // int temp=arr[i]; // arr[i]=arr[i+1]; // arr[i+1]=temp; // } // return arr; // } static boolean checkPerfectSquare(double number) { //calculating the square root of the given number double sqrt=Math.sqrt(number); //finds the floor value of the square root and comparing it with zero return ((sqrt - Math.floor(sqrt)) == 0); } static void sieveOfEratosthenes(int n) { 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; } } // Print all prime numbers // for(int i = 2; i <= n; i++) // { // if(prime[i] == true) // System.out.print(i + " "); // } } public static boolean isPrime(int n) { for(int i=2;i*i<=n;i++) { if(n%i==0) return false; } return true; } public static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
2a1eb75d984b0ba57025fa4b771f8331
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { 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 fs = new FastReader(); int q = fs.nextInt(); while(q-->0){ String s = fs.next(); String t = fs.next(); if(t.length()==1 && t.charAt(0)=='a'){ System.out.println(1); }else if(t.indexOf("a")!=-1){ System.out.println(-1); }else{ int n = s.length(); // int res = (); System.out.println(1L<<n); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
c7b11463515c3eddec908352d7e3fa31
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
/* package codechef; // don't place package name! */ 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 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine());// n is no of test cases for (int i = 0; i < n; i++) { String s=(br.readLine()); String t=(br.readLine()); fnc(s,t); } } public static void fnc(String s,String t){ if(t.length()==1 && t.charAt(0)=='a'){ System.out.println(1); return; } else{ int flag=0; for(int i=0;i<t.length();i++){ if(t.charAt(i)=='a'){ flag=1; break; } } if(flag==1){ System.out.println(-1); return; } else{ int n=s.length(); long ans=(long)Math.pow(2,n); System.out.println(ans); return; } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
05c620610bcc4f104654a84e8e1e9ae7
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long tt = sc.nextLong(); while (tt-- > 0) { String s = sc.next(); String t = sc.next(); long a = 0; for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == 'a') a++; } if (a > 0) { if (t.length() == 1) System.out.println(1); else System.out.println(-1); } else { long x = s.length(); System.out.println((long) Math.pow(2, x)); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
f79139488929ba2abc596bc6ab136d2d
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class ProbC { public static void main(String[] args) throws IOException{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int cases = Integer.parseInt(bf.readLine()); for(int caseNum=0; caseNum<cases; caseNum++) { String s = bf.readLine(); String t = bf.readLine(); int n = t.length(); long ans; boolean containsA = false; for(int i=0; i<n; i++) { if(t.charAt(i) == 'a') { containsA = true; break; } } if(containsA) { if(n > 1) ans = -1; else ans = 1; } else { ans = (long)Math.pow(2, s.length()); } sb.append(ans+"\n"); } System.out.println(sb); bf.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
b6b31fee15ef1b162988507f60a3ca57
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.lang.Math.*; public class Main { static int fact(int i){ int ans = 1; while(i!=0) { ans *= i; i--; } return ans; } public static void main(String[] args) throws IOException { int t = nextInt(); for (int i = 0; i < t; i++) { String m = next(); String n = next(); if(n.equals("a")) out.println("1"); else{ boolean b = true; for (int j = 0; j < n.length(); j++) { if(n.charAt(j) == 'a'){ out.println(-1); b = false; break; } } if(!b) continue; out.println((long) Math.pow(2, m.length())); } } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static String next() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
774de8429a1a9e25ce4872bc7d369063
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; import java.util.function.DoubleToLongFunction; public class Codeforces786{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void solve() { int x = sc.nextInt(); int y = sc.nextInt(); if(y%x!=0){ out.println(0+" "+0); }else{ out.println(1+" "+(y/x)); } } static void solve2(){ String str = "abcdefghijklmnopqrstuvwxyz"; HashMap<String,Integer> map = new HashMap<>(); int ind = 1; for(int i = 0;i<26;i++){ for(int j = 0;j<26;j++){ if(i==j) continue; map.put((str.charAt(i)+""+str.charAt(j)),ind++); } } int n = sc.nextInt(); for(int i = 0;i<n;i++){ String st = sc.nextLine(); out.println(map.get(st)); } } static void solve3(){ String s = sc.nextLine(); String t = sc.nextLine(); boolean flag = true; for(int i = 0;i<t.length();i++){ if(t.charAt(i)!='a') flag = false; } if(t.contains("a") && t.length()>1){ out.println(-1); return; } if(t.contains("a") && t.length()==1){ out.println(1); return; } long ans = 1; for(int i = 1;i<=s.length();i++){ ans *=2; } // ans = pow(2,s.length()); out.println(ans); // out.println(s.length()+1); } static void solve4(){ } static void solve5(){ } static void swap(char arr[][],int i,int j){ for(int k = j;k>0;k--){ if(arr[i][k]=='.'&& arr[i][k-1]=='*'){ char temp = arr[i][k]; arr[i][k] = arr[i][k-1]; arr[i][k-1] = temp; } } } static int search(int pre,int suf[],int i,int j){ while(i<=j){ int mid = (i+j)/2; if(suf[mid]==pre) return mid; else if(suf[mid]<pre) j = mid-1; else i = mid+1; } return Integer.MIN_VALUE; } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ // solve(); // solve2(); solve3(); // solve4(); // solve5(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } 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; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
08ebc0f06b7f01de6c3be2ef754d774f
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
/* package codechef; // don't place package name! */ 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(); sc.nextLine(); while(t-->0) { String s1 = sc.nextLine(); String s2 = sc.nextLine(); int l1 = s1.length(); int l2 = s2.length(); int ans=1; int cntOfA=0; for(char c : s2.toCharArray()) if(c=='a') cntOfA++; if(cntOfA==1 && l2==1) System.out.println(ans); else if(cntOfA ==0) System.out.println((long)ans+(long)Math.pow(2,l1)-1); else System.out.println(-1); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
fff0e57b8567c84dfbb74bc3020aff86
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import javax.swing.*; import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.lang.*; import java.io.*; import java.math.*; import java.util.stream.Stream; // Please name your class Main public class Main { static FastScanner fs=new FastScanner(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int Int() { return Integer.parseInt(next()); } long Long() { return Long.parseLong(next()); } String Str(){ return next(); } } public static void main (String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //reading /writing file //Scanner in=new Scanner(System.in); //Scanner in=new Scanner(new File("input.txt")); //PrintWriter pr=new PrintWriter("output.txt") int T=Int(); for(int t=0;t<T;t++){ Solution sol1=new Solution(out,fs); sol1.solution(T,t); } out.flush(); } public static int[] Arr(int n){ int A[]=new int[n]; for(int i=0;i<n;i++)A[i]=Int(); return A; } public static int Int(){ return fs.Int(); } public static long Long(){ return fs.Long(); } public static String Str(){ return fs.Str(); } } class Solution { PrintWriter out; int INF = Integer.MAX_VALUE; int NINF = Integer.MIN_VALUE; int MOD = 998244353; int mod = 998244353; Main.FastScanner fs; public Solution(PrintWriter out, Main.FastScanner fs) { this.out = out; this.fs = fs; } public void add(Map<Integer, Integer> f, int key) { Integer cnt = f.get(key); if(cnt == null) { f.put(key, 1); } else { f.put(key, cnt + 1); } } public void del(Map<Integer, Integer> f, int key) { Integer cnt = f.get(key); if(cnt == 1) { f.remove(key); } else { f.put(key, cnt - 1); } } public void msg(String s) { System.out.println(s); } public void solution(int all, int testcase) { String s = fs.Str(); String t = fs.Str(); boolean find = false; for(int i = 0; i < t.length(); i++) { if(t.charAt(i) == 'a')find = true; } long res = 1; for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'a') { res *= 2; } } if(find) { if(t.length() == 1) { out.println(1); }else { out.println(-1); } } else { out.println(res); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
15df75704eaad5d5e487fb63f5047336
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class InfiniteReplacement { public static void main(String[] args) { try { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int testcases = in.nextInt(); while (testcases-- > 0) { String s = in.next(), t = in.next(); long ans = 1; if (t.contains("a")) { if (t.length() > 1) ans = -1; } else { ans=(long)Math.pow(2, s.length()); } out.println(ans); } out.close(); } catch (Exception e) { // TODO: handle exception return; } } 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; static void dbg(Object... o) { if (LOCAL) System.err.println(Arrays.deepToString(o)); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
d46ffdd74442878eb5d521ce72471a12
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; import java.util.StringTokenizer; public class InfReplacement implements Runnable { public static void main(String [] args) { new Thread(null, new InfReplacement(), "whatever", 1<<26).start(); } public void run() { FastScanner scanner = new FastScanner(System.in); StringBuilder answers = new StringBuilder(); int tests = scanner.nextInt(); for (int i = 0; i < tests; i++) { String s = scanner.next(); String t = scanner.next(); long answer = 1; if (t.equals("a")) { answer = 1; } else if (t.length() > 1 && t.contains("a")) { answer = -1; } else { answer = answer << s.length(); } answers.append(answer + "\n"); } System.out.println(answers); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(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 int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
0693a4ea5ad0ec3d3093256c321a01c3
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class C { 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(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int test = t.nextInt(); while (test-- > 0) { String s = t.next(); String r = t.next(); long max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; int n = s.length(); int m = r.length(); // int n = t.nextInt(); // long[] a = new long[n]; // for (int i = 0; i < n; ++i) { // a[i] = t.nextLong(); // } long ans = 0; if (m == 0) ans = 1; else if (r.equals("a")) ans = 1; else if (m>1 && r.indexOf('a')!=-1) ans = -1; else { ans = (long)(Math.pow(2, n)); } o.println(ans); } o.flush(); o.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
bcb9f15d067f5b3b63bde9872f5aaa6b
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class C { private static FastReader fr; private static OutputStream out; private static int mod = (int)(1e9+7); private void solve() { int t = fr.nextInt(); while(t-- > 0) { int len = fr.next().length(); char arr[] = fr.next().toCharArray(); boolean flag = false; for(int i=0;i<arr.length;i++){ if(arr[i] == 'a') flag = true; } if(flag){ if(arr.length == 1) println(1); else println(-1); }else{ println((long)Math.pow(2, len)); } } } public static void main(String args[]) throws IOException{ new C().run(); } private static int modInverse(int a) { int m0 = mod; int y = 0, x = 1; if (mod == 1) return 0; while (a > 1) { int q = (int)(a / mod); int t = mod; mod = a % mod; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } private ArrayList<Integer> factors(int n, boolean include){ ArrayList<Integer> factors = new ArrayList<>(); if(n < 0) return factors; if(include) { factors.add(1); if(n > 1) factors.add(n); } int i = 2; for(;i*i<n;i++) { if(n % i == 0) { factors.add(i); factors.add(n / i); } } if(i * i == n) { factors.add(i); } return factors; } private ArrayList<Integer> PrimeFactors(int n) { ArrayList<Integer> primes = new ArrayList<>(); int i = 2; while (i * i <= n) { if (n % i == 0) { primes.add(i); n /= i; while (n % i == 0) { primes.add(i); n /= i; } } i++; } if(n > 1) primes.add(i); return primes; } private boolean isPrime(int n) { if(n == 0 || n == 1) { return false; } if(n % 2 == 0) { return false; } for(int i=3;i*i<=n;i+=2) { if(n % i == 0) { return false; } } return true; } private ArrayList<Integer> Sieve(int n){ boolean bool[] = new boolean[n+1]; Arrays.fill(bool, true); bool[0] = bool[1] = false; for(int i=2;i*i<=n;i++) { if(bool[i]) { int j = 2; while(i*j <= n) { bool[i*j] = false; j++; } } } ArrayList<Integer> primes = new ArrayList<>(); for(int i=2;i<=n;i++) { if(bool[i]) primes.add(i); } return primes; } private HashMap<Integer, Integer> addToHashMap(HashMap<Integer, Integer> map, int arr[]){ for(int val: arr) { if(!map.containsKey(val)) { map.put(val, 1); }else { map.put(val, map.get(val) + 1); } } return map; } private int factorial(int n) { long fac = 1; for(int i=2;i<=n;i++) { fac *= i; fac %= mod; } return (int)(fac % mod); } // private static int pow(int base,int exp){ // if(exp == 0){ // return 1; // }else if(exp == 1){ // return base; // } // int a = pow(base,exp/2); // a = ((a % mod) * (a % mod)) % mod; // if(exp % 2 == 1) { // a = ((a % mod) * (base % mod)); // } // return a; // } private static int gcd(int a,int b){ if(a == 0){ return b; } return gcd(b%a,a); } private static int lcm(int a,int b){ return (a * b)/gcd(a,b); } private void run() throws IOException{ fr = new FastReader(); out = new BufferedOutputStream(System.out); solve(); out.flush(); out.close(); } private static class FastReader{ private static BufferedReader br; private static StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedOutputStream(System.out); } 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()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public char[] nextCharArray() { return next().toCharArray(); } public int[] nextIntArray(int n) { int arr[] = new int[n]; for(int i=0;i<n;i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long arr[] = new long[n]; for(int i=0;i<n;i++) { arr[i] = nextLong(); } return arr; } public String[] nextStringArray(int n) { String arr[] = new String[n]; for(int i=0;i<n;i++) { arr[i] = next(); } return arr; } } public static void print(Object str) { try { out.write(str.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } public static void println(Object str) { try { out.write((str.toString() + "\n").getBytes()); } catch (IOException e) { e.printStackTrace(); } } public static void println() { println(""); } public static void printArray(Object str[]){ for(Object s : str) { try { out.write(str.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
757642d34a3397d70882e270064a90ca
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String args[]) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { String s = input.next(); String t = input.next(); int countt=0,Tanother=0; int counts=0; for (int i = 0; i <s.length(); i++) { if(s.charAt(i)=='a') { counts++; } } for (int i = 0; i <t.length(); i++) { if(t.charAt(i)=='a') { countt++; } else { Tanother++; } } if(counts==0) { System.out.println("1"); } else if(countt>0&&Tanother>0) { System.out.println("-1"); } else if(countt==t.length()&&countt==1) { System.out.println("1"); } else if(countt==t.length()&&countt>1) { System.out.println("-1"); } else { long ans = (long) Math.pow(2L, counts); System.out.println(ans); } } } 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
48ee089bf10b7793017801305bb04ae9
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f; public static int mod = 1000000007; public static int mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = true; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o){ try { String s = fReader.nextString(), t = fReader.nextString(); int slen = s.length(), tlen = t.length(); boolean containA = false; for(char c: t.toCharArray()){ if(c == 'a') { containA = true; break; } } if(slen == 1){ if(tlen == 1){ if(containA){ o.println(1); return; } else{ o.println(2); return; } } else { if (containA) { o.println(-1); return; } else { o.println(2); return; } } } else{ if(tlen == 1){ if(containA){ o.println(1); return; } else{ o.println(qpow(2, slen)); return; } } else{ if(containA){ o.println(-1); return; } else{ o.println(qpow(2, slen)); return; } } } } catch (Exception e){e.printStackTrace();} } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static long qpow(long a, long n){ long ret = 1l; while(n > 0){ if((n & 1) == 1) ret = ret * a; n >>= 1; a = a * a; } return ret; } public static class unionFind { int[] parent; int[] size; int n; public unionFind(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=1;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(p != parent[p]){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getCount(){ return n; } public int[] getSize(){ return size; } } public static class BIT { int[] tree; int n; public BIT(int n){ this.n = n; tree = new int[n+1]; } public void add(int x, int val){ while(x <= n){ tree[x] += val; x += lowBit(x); } } public int query(int x){ int ret = 0; while(x > 0){ ret += tree[x]; x -= lowBit(x); } return ret; } public int lowBit(int x) { return x&-x; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } 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().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
f0e24477c260ab5650a6ca6a896352ca
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; public class MyClass{ public static void main(String[] args){ Scanner sc= new Scanner(System.in); int n = sc.nextInt(); while(n > 0){ n--; String s = sc.next(); String t = sc.next(); int s_a = s.length(), t_len = t.length(); int t_a = 0; for(int i = 0; i < t.length(); i++){ if(t.charAt(i) == 'a'){ t_a++; } } if(t_len == 1 && t_a == 1){ System.out.println(1); continue; }else if(t_len > 1 && t_a > 0){ System.out.println(-1); continue; }else{ long num = (long) Math.pow(2, s_a); System.out.println(num); continue; } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
c8dac4a1edd483cdbdfb97b5316aead9
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; public class Solution{ public static double getCombination(int m,int n){//C(n,m)=m!/(m-n)!n! double c=1; if(n==0||n==m){ return c; } int p=n; for(int i=m-n+1;i<=m;i++){ c*=i; while(c>=p&&p>0){ c/=p; p--; } } while(p>0){ c/=p; p--; } return c; } public static int getACount(String str){ int c=0,n=str.length(); for(int i=0;i<n;i++){ if(str.charAt(i)=='a'){ c++; } } return c; } public static void main(String[] args){ Scanner sc=new Scanner(System.in); int rows=Integer.parseInt(sc.nextLine()); while(rows>0){ String s=sc.nextLine(),t=sc.nextLine(); int ta=getACount(t); if(ta==1&&t.length()==1){ System.out.println(1); }else if(ta>=1&&t.length()>1){ System.out.println(-1); }else if(ta==0){ double c=0; int sa=getACount(s); for(int i=0;i<=sa;i++){ c+=getCombination(sa,i); } System.out.println((long)c); } rows--; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
fa7fcdbaa3ee6645ea9ba956c759571e
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; import java.lang.Math; public class proj { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String s, t; for(int i = 0; i <n;i++){ s = sc.nextLine(); t = sc.nextLine(); if(t.equals("a")) System.out.println(1); else if(t.contains("a") && t.length()>=2) System.out.println(-1); else{ System.out.println((long)Math.pow(2,s.length())); } } sc.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
3d01d59347ef1bce1f469dd5bdc11407
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; // import java.util.Map.Entry; public class Main{ static long mod=(long)1e9+7; 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 boolean isArrayPalin(long arr[]) { int i=0;int j=arr.length-1; while(i<j) { if(arr[i]!=arr[j]) return false; i++;j--; } return true; } static boolean inStringPalin(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; } static boolean isPrime(int n) { boolean bool[]=new boolean[n+1]; for(int i=0;i<bool.length;i++) bool[i]=true; for(int i=2;i<=Math.sqrt(n);i++) { for(int j=i*i;j<=n;j+=i) bool[j]=false; } for(int i=2;i<bool.length;i++) { if(bool[i]==true) { if(i==n) return true; } } return false; } static long gcd(long a,long b) { if(a==0) return b; if(b==0) return a; return gcd(b%a,a); } static boolean isPowerOf2(int n) { if((n&(n-1))==0) return true; return false; } static int countSetBits(int n) //Brian Kernighan Algo O(log n) =>cnt num of 1's { int cnt=0; while(n>0) { n&=(n-1); cnt++; } return cnt; } static int maxSubArraySum(int a[],int size) //kadane's algorithm { int max_so_far = a[0], max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_ending_here < 0) max_ending_here = 0; else if (max_so_far < max_ending_here) max_so_far = max_ending_here; } return max_so_far; } static boolean isStringUpperCase(String s){ for(int i=0;i<s.length();i++){ if(!Character.isUpperCase(s.charAt(i))) return false; } return true; } static int UpperBound(long a[], long x) {// x is the key or target value int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int LowerBound(long a[], long x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static long factorial(int n) { long res = 1, i; for (i=2; i<=n; i++) res*=i; return res; } static class Pair{ int x;int y; Pair(int x,int y){ this.x=this.y; } } public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } public static void main(String[] args) { try { FastReader sc=new FastReader(); FastWriter out = new FastWriter(); int testCases=sc.nextInt(); while(testCases-- > 0){ String s=sc.next(); String t=sc.next(); int cnt=0; for(int i=0;i<t.length();i++){ if(t.charAt(i)=='a') cnt++; } if(t.length()>1&&cnt!=0) out.println(-1); else if(t.length()==1 && t.equals("a")) out.println(1); else out.println((long)Math.pow(2,s.length())); } out.close(); } catch (Exception e) { return; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
4b111500f3339cfdb874c4916bace1fa
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main{ static int mod = (int)1e9+7; static PrintWriter out; static String[] memo; static int n; static String line; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Scanner sc=new Scanner(System.in); int t = sc.nextInt(); w:while(t--!=0) { String line = sc.next(); String line2 = sc.next(); for(int i=0;i!=line2.length();i++) if(line2.charAt(i)=='a') { if(line2.length()>1) out.println(-1); else out.println(1); continue w; } out.println(1L<<line.length()); } out.flush(); } 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> { int x; int y; public pair(int x, int 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; } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
f4f118ec32078ec2a02d9108118fcd18
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.awt.*; import java.util.*; import java.io.*; // You have that in you, you can do it........ public class Codeforces { static FScanner sc = new FScanner(); static PrintWriter out = new PrintWriter(System.out); static final Random random = new Random(); static long mod = 1000000007L; static HashMap<String, Integer> map = new HashMap<>(); static boolean[] sieve = new boolean[1000000]; static double[] fib = new double[1000000]; public static void main(String args[]) throws IOException { int T = sc.nextInt(); while (T-- > 0) { String s=sc.next(); String t=sc.next(); if(t.contains("a")) { if (t.length() == 1) out.println("1"); else out.println("-1"); } else { out.println((long)Math.pow(2,s.length())); } } out.close(); } // TemplateCode static int maxi(int[] a) { int maxi = 0; int max = Integer.MIN_VALUE; for (int i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; maxi = i; } } return maxi; } static void fib() { fib[0] = 0; fib[1] = 1; for (int i = 2; i < fib.length; i++) fib[i] = fib[i - 1] + fib[i - 2]; } static void primeSieve() { for (int i = 0; i < sieve.length; i++) sieve[i] = true; for (int i = 2; i * i <= sieve.length; i++) { if (sieve[i]) { for (int j = i * i; j < sieve.length; j += i) { sieve[j] = false; } } } } static int max(int a, int b) { if (a < b) return b; return a; } static int min(int a, int b) { if (a < b) return a; return b; } 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 <E> void print(E res) { System.out.println(res); } static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if (a < 0) return -1 * a; return a; } static class Pair { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } } static class FScanner { BufferedReader br; StringTokenizer st; public FScanner() { 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[] readintarray(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readlongarray(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
888bc41e2b8f2544e67163d3a54e7810
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; public class InfiniteReplacement { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int tt=sc.nextInt(); while (tt-->0){ String s=sc.next(); String t=sc.next(); int count=0; for (int i = 0; i < t.length(); i++) { if(t.charAt(i)=='a'){ ++count; } } if(count==t.length() && t.length()==1){ System.out.println(1); } else if(count>0){ System.out.println(-1); } else if(count==t.length()){ System.out.println(1); } else { long ans = 0; ans = (long) Math.pow(2, s.length()); System.out.println(ans); } } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
4f009bd93cb08c85140cb8d218b03e41
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.Scanner; public class CodeForces { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { String str = s.next(); String T = s.next(); if (T.length() == 1 && T.charAt(0) == 'a') System.out.println("1"); else if (T.indexOf('a') != -1) System.out.println("-1"); else System.out.println((long) (Math.pow(2, str.length()))); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
cad6833b15e69e9c64095cdaadb22369
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
/* package codechef; // don't place package name! */ 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 { // your code goes here try { Scanner sc=new Scanner(System.in); int test=sc.nextInt(); while(test-->0) { String s=sc.next(); String t=sc.next(); if(t.equals("a")) { System.out.println(1); continue; } boolean flag=true; for(int i=0;i<t.length();i++) { if(t.charAt(i)=='a') { flag=false; break; } } long res=0; if(flag==false) { System.out.println(-1); continue; } res=(long)Math.pow(2,s.length()); System.out.println(res); } } catch(Exception e) { System.out.println(e); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
3bc771a5736c6184d3c404406da6d4be
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.io.*; public class C { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer st = new StringTokenizer(""); static String next() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nexti() throws IOException { return Integer.parseInt(next()); } public static void solve() throws IOException { String s = next(), t = next(); boolean bs = false, bt = false; for (int i = 0; i < s.length(); i++) if (s.charAt(i) == 'a') bs = true; for (int i = 0; i < t.length(); i++) if (t.charAt(i) == 'a') bt = true; if (bs && bt) out.println(t.length() != 1 ? -1 : 1); else if (bs) { int cnt = 0; for (int i = 0; i < s.length(); i++) if (s.charAt(i) == 'a') ++cnt; out.println((long) 1 << cnt); } else out.println(1); } public static void main(String[] args) throws IOException { // int T = 1; int T = nexti(); while(T-->0) solve(); br.close(); out.close(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
80522ca1f50e9cfecf790d6c268155cc
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; public class Main implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new Main(), "", 256 * (1L << 20)).start(); } public void run() { try { long t1 = System.currentTimeMillis(); if (System.getProperty("ONLINE_JUDGE") != null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } Locale.setDefault(Locale.US); solve(); in.close(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } int[] readIntArray(int n) throws IOException { int [] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = readInt(); } return a; } long[] readLongArray(int n) throws IOException { long [] a = new long[n]; for(int i = 0; i < n; i++) { a[i] = readLong(); } return a; } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort 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;//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); } void solveTest() throws IOException { String s = readString(); int len = s.length(); String t = readString(); if(t.length() > 1) { boolean hasA = false; for(int i=0;i<t.length();i++) { if(t.charAt(i) == 'a') { hasA = true; break; } } if (hasA) { out.println("-1"); } else { long ans = 1l<<len; out.println(ans); } return; } char c = t.charAt(0); if(c=='a') { out.println("1"); return; } long ans = 1l<<len; out.println(ans); } void solve() throws IOException { int numTests = readInt(); while(numTests-->0) { solveTest(); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
069fb755ec5abbb05848e8bf38f9e159
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.util.*; import java.util.concurrent.TimeUnit; import java.io.*; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; public class C_Infinite_Replacement{ public static void solve(){ } public static void main(String args[])throws IOException, ParseException{ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ String s=sc.next(); String ts=sc.next(); int count[]=new int[26]; for(int i=0;i<ts.length();i++){ count[ts.charAt(i)-'a']++; } if(count[0]==1&&ts.length()==1){ System.out.println(1); continue; } if(count[0]==0){ int len=s.length(); System.out.println((long)Math.pow(2,len)); continue; } System.out.println(-1); } } public static <K,V> Map<K,V> getLimitedSizedCache(int size){ /*Returns an unlimited sized map*/ if(size==0){ return (LinkedHashMap<K, V>) Collections.synchronizedMap(new LinkedHashMap<K, V>()); } /*Returns the map with the limited size*/ Map<K, V> linkedHashMap = Collections.synchronizedMap(new LinkedHashMap<K, V>() { protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > size; } }); return linkedHashMap; } } class BinarySearch<T extends Comparable<T>> { T ele[]; int n; public BinarySearch(T ele[],int n){ this.ele=(T[]) ele; Arrays.sort(this.ele); this.n=n; } public int lower_bound(T x){ //Return next smallest element greater than ewqual to the current element int left=0; int right=n-1; while(left<=right){ int mid=left+(right-left)/2; if(x.compareTo(ele[mid])==0)return mid; if(x.compareTo(ele[mid])>0)left=mid+1; else right=mid-1; } if(left ==n)return -1; return left; } public int upper_bound(T x){ //Returns the highest element lss than equal to the current element int left=0; int right=n-1; while(left<=right){ int mid=left+(right-left)/2; if(x.compareTo(ele[mid])==0)return mid; if(x.compareTo(ele[mid])>0)left=mid+1; else right=mid-1; } return right; } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[1000000]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } class Data implements Comparable<Data>{ int num; public Data(int num){ this.num=num; } public int compareTo(Data o){ return -o.num+num; } public String toString(){ return num+" "; } } class Binary{ public String convertToBinaryString(long ele){ StringBuffer res=new StringBuffer(); while(ele>0){ if(ele%2==0)res.append(0+""); else res.append(1+""); ele=ele/2; } return res.reverse().toString(); } } class FenwickTree{ int bit[]; int size; FenwickTree(int n){ this.size=n; bit=new int[size]; } public void modify(int index,int value){ while(index<size){ bit[index]+=value; index=(index|(index+1)); } } public int get(int index){ int ans=0; while(index>=0){ ans+=bit[index]; index=(index&(index+1))-1; } return ans; } } class PAndC{ long c[][]; long mod; public PAndC(int n,long mod){ c=new long[n+1][n+1]; this.mod=mod; build(n); } public void build(int n){ for(int i=0;i<=n;i++){ c[i][0]=1; c[i][i]=1; for(int j=1;j<i;j++){ c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod; } } } } class Trie{ int trie[][]; int revind[]; int root[]; int tind,n; int sz[]; int drev[]; public Trie(){ trie=new int[1000000][2]; root=new int[600000]; sz=new int[1000000]; tind=0; n=0; revind=new int[1000000]; drev=new int[20]; } public void add(int ele){ // System.out.println(root[n]+" "); n++; tind++; revind[tind]=n; root[n]=tind; addimpl(root[n-1],root[n],ele); } public void addimpl(int prev_root,int cur_root,int ele){ for(int i=18;i>=0;i--){ int edge=(ele&(1<<i))>0?1:0; trie[cur_root][1-edge]=trie[prev_root][1-edge]; sz[cur_root]=sz[trie[cur_root][1-edge]]; tind++; drev[i]=cur_root; revind[tind]=n; trie[cur_root][edge]=tind; cur_root=tind; prev_root=trie[prev_root][edge]; } sz[cur_root]+=sz[prev_root]+1; for(int i=0;i<=18;i++){ sz[drev[i]]=sz[trie[drev[i]][0]]+sz[trie[drev[i]][1]]; } } public void findmaxxor(int l,int r,int x){ int ans=0; int cur_root=root[r]; for(int i=18;i>=0;i--){ int edge=(x&(1<<i))>0?1:0; if(revind[trie[cur_root][1-edge]]>=l){ cur_root=trie[cur_root][1-edge]; ans+=(1-edge)*(1<<i); }else{ cur_root=trie[cur_root][edge]; ans+=(edge)*(1<<i); } } System.out.println(ans); } public void findKthStatistic(int l,int r,int k){ //System.out.println("In 3"); int curr=root[r]; int curl=root[l-1]; int ans=0; for(int i=18;i>=0;i--){ for(int j=0;j<2;j++){ if(sz[trie[curr][j]]-sz[trie[curl][j]]<k) k-=sz[trie[curr][j]]-sz[trie[curl][j]]; else{ curr=trie[curr][j]; curl=trie[curl][j]; ans+=(j)*(1<<i); break; } } } System.out.println(ans); } public void findSmallest(int l,int r,int x){ //System.out.println("In 4"); int curr=root[r]; int curl=root[l-1]; int countl=0,countr=0; // System.out.println(curl+" "+curr); for(int i=18;i>=0;i--){ int edge=(x&(1<<i))>0?1:0; // System.out.println(trie[curl][edge]+" "+trie[curr][edge]+" "+sz[curl]+" "+sz[curr]); if(edge==1){ countr+=sz[trie[curr][0]]; countl+=sz[trie[curl][0]]; } curr=trie[curr][edge]; curl=trie[curl][edge]; } countl+=sz[curl]; countr+=sz[curr]; System.out.println(countr-countl); } } class Printer{ public <T> T printArray(T obj[] ,String details){ System.out.println(details); for(int i=0;i<obj.length;i++) System.out.print(obj[i]+" "); System.out.println(); return obj[0]; } public <T> void print(T obj,String details){ System.out.println(details+" "+obj); } } class Node{ long weight; int vertex; public Node(int vertex,long weight){ this.vertex=vertex; this.weight=weight; } public String toString(){ return vertex+" "+weight; } } class Graph{ int nv; //0 indexing i.e vertices starts from 0 input as 1 indexed for add Edge List<List<Node>> adj; boolean visited[]; public Graph(int n){ adj=new ArrayList<>(); this.nv=n; // visited=new boolean[nv]; for(int i=0;i<n;i++) adj.add(new ArrayList<Node>()); } public void addEdge(int u,int v,long weight){ u--;v--; Node first=new Node(v,weight); Node second=new Node(u,weight); adj.get(v).add(second); adj.get(u).add(first); } public void dfscheck(int u,long curweight){ visited[u]=true; for(Node i:adj.get(u)){ if(visited[i.vertex]==false&&(i.weight|curweight)==curweight) dfscheck(i.vertex,curweight); } } long maxweight; public void clear(){ this.adj=null; this.nv=0; } public void solve() { maxweight=(1l<<32)-1; dfsutil(31); System.out.println(maxweight); } public void dfsutil(int msb){ if(msb<0)return; maxweight-=(1l<<msb); visited=new boolean[nv]; dfscheck(0,maxweight); for(int i=0;i<nv;i++) { if(visited[i]==false) {maxweight+=(1<<msb); break;} } dfsutil(msb-1); } // public boolean TopologicalSort() { // top=new int[nv]; // int cnt=0; // int indegree[]=new int[nv]; // for(int i=0;i<nv;i++){ // for(int j:adj.get(i)){ // indegree[j]++; // } // } // Deque<Integer> q=new LinkedList<Integer>(); // for(int i=0;i<nv;i++){ // if(indegree[i]==0){ // q.addLast(i); // } // } // while(q.size()>0){ // int tele=q.pop(); // top[tele]=cnt++; // for(int j:adj.get(tele)){ // indegree[j]--; // if(indegree[j]==0) // q.addLast(j); // } // } // return cnt==nv; // } // public boolean isBipartiteGraph(){ // col=new Integer[nv]; // visited=new boolean[nv]; // for(int i=0;i<nv;i++){ // if(visited[i]==false){ // col[i]=0; // dfs(i); // } // } } class DSU{ int []parent; int rank[]; int n; public DSU(int n){ this.n=n; parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++) { parent[i]=i; rank[i]=1; } } public int find(int i){ if(parent[i]==i)return i; return parent[i]=find(parent[i]); } public boolean union(int a,int b){ int pa=find(a); int pb=find(b); if(pa==pb)return false; if(rank[pa]>rank[pb]){ parent[pb]=pa; rank[pa]+=rank[pb]; } else{ parent[pa]=pb; rank[pb]+=rank[pa]; } return true; } } class SegmentTree{ long tree[]; long data[]; int tsize; int dsize; public SegmentTree(long data[],int n){ this.tsize=4*n+1; this.dsize=n; this.data=data; this.tree=new long[tsize]; build(0,n-1,0); } public SegmentTree(int n,long def){ this.tsize=4*n+1; this.dsize=n; this.data=new long[n]; for(int i=0;i<n;i++){ data[i]=def; } this.tree=new long[tsize]; build(0,n-1,0); } public void build(int l,int r,int treeindex){ if(l==r){ tree[treeindex]=data[l]; return ; } int mid=(l+r)/2; build(l,mid,2*treeindex+1); build(mid+1,r,2*treeindex+2); tree[treeindex]=Math.max(tree[2*treeindex+1],tree[2*treeindex+2]); } public void updateOneImpl(int l,int r,int tind,int dind){ if(l==r&&l==dind){ tree[tind]=data[dind]; return ; } if(dind>=l&&dind<=r){ int mid=(l+r)/2; updateOneImpl(l,mid,2*tind+1,dind); updateOneImpl(mid+1,r,2*tind+2,dind); tree[tind]=Math.max(tree[2*tind+1],tree[2*tind+2]); } // System.out.println("In range "+l+" "+r+" "+tree[tind]); } public void updateOne(int index,long value){ data[index]=value; updateOneImpl(0,dsize-1,0,index); } public long getRangeValueImpl(int ql,int qr,int tl,int tr,int tind){ if(tr<ql||qr<tl){ return Long.MIN_VALUE; } if(qr>=tr&&ql<=tl)return tree[tind]; int mid=(tl+tr)/2; long l=getRangeValueImpl(ql, qr, tl, mid, 2*tind+1); long r=getRangeValueImpl(ql, qr, mid+1, tr, 2*tind+2); //System.out.println("In range "+tl+" "+tr+" "+Math.max(l,r)); return Math.max(l,r); } public long getRangeValue(int l,int r){ long temp= getRangeValueImpl(l,r, 0, dsize-1, 0); // System.out.println(" Max Value in between "+l+" "+r+" = "+temp); return temp; } public void printData(){ System.out.println("The Data After updation"); for(int i=0;i<dsize;i++){ System.out.print(data[i]+" "); } System.out.println(); // System.out.println(" The Tree After Updation"); // for(int i=0;i<tsize;i++){ // System.out.print(tree[i]+" "); // } // System.out.println(); } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
37afc2cd78566b5ea67b0a9aa6033148
train_107.jsonl
1651502100
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
256 megabytes
import java.io.*; import java.util.*; import javafx.scene.control.Label; public class C_Infinite_Replacement{ static Scanner sc = new Scanner(System.in); // @Harshit Maurya public static void reverse(int[] arr, int l, int r) { int d = (r - l + 1) / 2; for (int i = 0; i < d; i++) { int t = arr[l + i]; arr[l + i] = arr[r - i]; arr[r - i] = t; } } // QUICK MATHS private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static void main(String[] args) { solve(); } // BIT MAGIC public int getMSB(int b) { return (int)(Math.log(b) / Math.log(2)); } //HELPER FUNCTIONS private static int[] nextIntArray(int n){ int arr[]=new int[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextInt(); } return arr; } private static int[][] nextInt2DArray(int m,int n){ int arr[][]=new int[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ arr[i][j]=sc.nextInt(); } } return arr; } private static long[] nextLongArray(int n){ long arr[]=new long[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextLong(); } return arr; } private static double[] nextDoubleArray(int n){ double arr[]=new double[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextDouble(); } return arr; } 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 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 print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static void solve() { int t = sc.nextInt(); Label: while (t-- > 0) { String str=sc.next(); String rep=sc.next(); if(rep.length()==1 && rep.charAt(0)=='a'){ System.out.println(1); continue Label; } if(rep.contains("a")){ System.out.println(-1); continue Label; } System.out.println((long)Math.pow(2, str.length())); // if(rep.length()!=1){ // System.out.println(-1); // continue Label; // } // if(rep.charAt(0)=='a'){ // System.out.println(1); // continue Label; // } // System.out.println(str.length()+1); } } }
Java
["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"]
2 seconds
["1\n-1\n2"]
NoteIn the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
Java 8
standard input
[ "combinatorics", "implementation", "strings" ]
d6ac9ca9cc5dfd9f43f5f65ce226349e
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.
1,000
For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
standard output
PASSED
b34719c4f429235c71a4004891eec4b5
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class ABCSort { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); for (int i = 0; i < n; i++) { int test = s.nextInt(); int[] a = new int[test]; for (int j = 0; j < test; j++) { a[j] = s.nextInt(); } System.out.println(equal(a, test)); } } public static String equal(int[] arr, int n) { int[] b = arr.clone(); Arrays.sort(b); for (int i = n - 1; i > 0; i -= 2) { if (arr[i] < arr[i - 1]) { int temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; } } return Arrays.equals(arr, b) ? "YES" : "NO"; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output
PASSED
d3b3acf4e1ecfa5bed900cde34e07931
train_107.jsonl
1651502100
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class ABCSort { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); for (int i = 0; i < n; i++) { int test = s.nextInt(); int[] a = new int[test]; for (int j = 0; j < test; j++) { a[j] = s.nextInt(); } System.out.println(equal(a, test)); } } public static String equal(int[] arr, int n) { int[] b = arr.clone(); Arrays.sort(b); for (int i = n - 1; i > 0; i -= 2) { if (arr[i] < arr[i - 1]) { int tmp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = tmp; } } return Arrays.equals(arr, b) ? "YES" : "NO"; } }
Java
["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"]
2 seconds
["YES\nNO\nYES"]
NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\Rightarrow$$$$$$[3, 1, 5]$$$$$$\Rightarrow$$$$$$[3, 1]$$$$$$\Rightarrow$$$$$$[3]$$$$$$\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\underline{3}]$$$$$$[3, \underline{5}]$$$$$$[3, \underline{1}, 5]$$$$$$[3, \underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \underline{1}, 5]$$$$$$\Rightarrow$$$$$$[3, \underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{3}, 5]$$$$$$\Rightarrow$$$$$$[\underline{5}]$$$$$$\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted.
Java 17
standard input
[ "constructive algorithms", "implementation", "sortings" ]
95b35c53028ed0565684713a93910860
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.
1,200
For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
standard output