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
69fccfd48cfea061bbc4ef5acd4b73a6
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 a111 { public static void main(String []args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); while(n-->0) { String t=s.next(); String s1=s.next(); if(s1.length()==1&&s1.charAt(0)=='a') { System.out.println(1); } else { int m=t.length(); int res=0; for(int i=0;i<s1.length();i++) { if(s1.charAt(i)=='a') res++; } if(res>0) { System.out.println(-1); } else { long ans =(long)Math.pow(2, m); 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 11
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
a3b4d2f002fb2237623a1fe9632eef8c
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.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.math.BigInteger; public final class Main{ static 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[64]; // 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(); } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } static Kattio sc = new Kattio(); static long mod = 998244353l; static PrintWriter out =new PrintWriter(System.out); //Heapify function to maintain heap property. public static void swap(int i,int j,int arr[]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void swap(int i,int j,long arr[]) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void swap(int i,int j,char arr[]) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static String endl = "\n" , gap = " "; public static void main(String[] args)throws IOException { int t =ri(); // List<Integer> list = new ArrayList<>(); // int MAX = (int)4e4; // for(int i =1;i<=MAX;i++) { // if(isPalindrome(i + "")) list.add(i); // } // // System.out.println(list); // long dp[] = new long[MAX +1]; // dp[0] = 1; // long mod = (long)(1e9+7); // for(int x : list) { // for(int i =1;i<=MAX;i++) { // if(i >= x) { // dp[i] += dp[i-x]; // dp[i] %= mod; // } // } // } // int MAK = (int)1e6; // boolean seive[] = new boolean[MAK]; // Arrays.fill(seive , true); // seive[1] = false; // seive[0] = false; // for (int i = 1; i < MAK; i++) { // if(seive[i]) { // for (int j = i+i; j < MAK; j+=i) { // seive[j] = false; // } // } // } // for (int i = 1; i <= MAK; i++) { // seive[i] += seive[i - 1]; // } int test_case = 1; while(t-->0) { // out.write("Case #"+(test_case++)+": "); solve(); } out.close(); } static HashMap<Integer , Boolean>dp; public static void solve()throws IOException { char s[] = rac(); String t = rs(); if(t.equals("a")) { out.write(1 + endl); return; } for(int i =0;i<t.length();i++) { if(t.charAt(i) == 'a') { out.write(-1 + endl); return; } } long ans = 1l<<(long)s.length; out.write(ans + endl); } public static long callfun(String s , String t,HashMap<String,Long>map) { int n = s.length(); long ans = 1; if(map.containsKey(s)) return 0; for(int i =0;i<n;i++) { if(s.charAt(i) == 'a') { String ns = s.substring(0,i) + s.substring(i+1); ans += callfun(ns , t , map); } } map.put(s , ans); return ans; } public static double getSlope(int a , int b,int x,int y) { if(a-x == 0) return Double.MAX_VALUE; if(b-y == 0) return 0.0; return ((double)b-(double)y)/((double)a-(double)x); } public static int callfun(int a[], int []b) { HashMap<Integer , Integer> map = new HashMap<>(); int n = a.length; for(int i =0;i<b.length;i++) { map.put(b[i] , i); } HashMap<Integer , Integer> cnt = new HashMap<>(); for(int i =0;i<n;i++) { int move = (map.get(a[i])-i+n)%n; cnt.put(move , cnt.getOrDefault(move,0) + 1); } int max =0; for(int x : cnt.keySet()) max = Math.max(max , cnt.get(x)); return max; } public static boolean collinearr(long a[] , long b[] , long c[]) { return (b[1]-a[1])*(b[0]-c[0]) == (b[0]-a[0])*(b[1]-c[1]); } public static boolean isSquare(int sum) { int root = (int)Math.sqrt(sum); return root*root == sum; } public static boolean canPlcae(int prev,int len , int sum,int arr[],int idx) { // System.out.println(sum + " is sum prev " + prev); if(len == 0) { if(isSquare(sum)) return true; return false; } for(int i = prev;i<=9;i++) { arr[idx] = i; if(canPlcae(i, len-1,sum + i*i,arr,idx + 1)) { return true; } } return false; } public static boolean isPalindrome(String s) { int i =0 , j = s.length() -1; while(i <= j && s.charAt(i) == s.charAt(j)) { i++; j--; } return i>j; } // digit dp hint; public static long callfun(String num , int N, int last ,int secondLast ,int bound ,long dp[][][][]) { if(N == 1) { if(last == -1 || secondLast == -1) return 0; long answer = 0; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i = 0;i<=max;i++) { if(last > secondLast && last > i) { answer++; } if(last < secondLast && last < i) { answer++; } } return answer; } if(secondLast == -1 || last == -1 ){ long answer = 0l; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i =0;i<=max;i++) { int nl , nsl , newbound = bound==0?0:i==max?1:0; if(last == - 1&& secondLast == -1 && i == 0) { nl = -1 ; nsl = -1; } else { nl = i;nsl = last; } long temp = callfun(num , N-1 , nl , nsl ,newbound, dp); answer += temp; if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++; } return answer; } if(dp[N][last][secondLast][bound] != -1) return dp[N][last][secondLast][bound]; long answer = 0l; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i =0;i<=max;i++) { int nl , nsl , newbound = bound==0?0:i==max?1:0; if(last == - 1&& secondLast == -1 && i == 0) { nl = -1 ; nsl = -1; } else { nl = i;nsl = last; } long temp = callfun(num , N-1 , nl , nsl ,newbound,dp); answer += temp; if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++; } return dp[N][last][secondLast][bound] = answer; } public static Long callfun(int index ,int pair,int arr[],Long dp[][]) { long mod = (long)998244353l; if(index >= arr.length) return 1l; if(dp[index][pair] != null) return dp[index][pair]; Long sum = 0l , ans = 0l; if(arr[index]%2 == pair) { return dp[index][pair] = callfun(index + 1,pair^1 , arr,dp)%mod + callfun(index + 1 ,pair , arr , dp)%mod; } else { return dp[index][pair] = callfun(index + 1,pair , arr,dp)%mod; } // for(int i =index;i<arr.length;i++) { // sum += arr[i]; // if(sum%2 == pair) { // ans = ans + callfun(i + 1,pair^1,arr , dp)%mod; // ans%=mod; // } // } // return dp[index][pair] = ans; } public static boolean callfun(int index , int n,int neg , int pos , String s) { if(neg < 0 || pos < 0) return false; if(index >= n) return true; if(s.charAt(0) == 'P') { if(neg <= 0) return false; return callfun(index + 1,n , neg-1 , pos , s.charAt(1) + "N"); } else { if(pos <= 0) return false; return callfun(index + 1 , n , neg , pos-1 , s.charAt(1) + "P"); } } public static void getPerm(int n , char arr[] , String s ,List<String>list) { if(n == 0) { list.add(s); return; } for(char ch : arr) { getPerm(n-1 , arr , s+ ch,list); } } public static int getLen(int i ,int j , char s[]) { while(i >= 0 && j < s.length && s[i] == s[j]) { i--; j++; } i++; j--; if(i>j) return 0; return j-i + 1; } public static int getMaxCount(String x) { char s[] = x.toCharArray(); int max = 0; int n = s.length; for(int i =0;i<n;i++) { max = Math.max(max,Math.max(getLen(i , i,s) , getLen(i ,i+1,s))); } return max; } public static double getDis(int arr[][] , int x, int y) { double ans = 0.0; for(int a[] : arr) { int x1 = a[0] , y1 = a[1]; ans += Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1)); } return ans; } public static boolean valid(String x ) { if(x.length() == 0) return true; if(x.length() == 1) return false; char s[] = x.toCharArray(); if(x.length() == 2) { if(s[0] == s[1]) { return false; } return true; } int r = 0 , b = 0; for(char ch : x.toCharArray()) { if(ch == 'R') r++; else b++; } return (r >0 && b >0); } public static long callfun(int day , int k, int limit,int n) { if(k > limit) return 0; if(day > n) return 1; long ans = callfun(day + 1 , k , limit, n)*k + callfun(day + 1,k+1,limit,n)*(k+1); return ans; } public static void primeDivisor(HashMap<Long , Long >cnt , long num) { for(long i = 2;i*i<=num;i++) { while(num%i == 0) { cnt.put(i ,(cnt.getOrDefault(i,0l) + 1)); num /= i; } } if(num > 2) { cnt.put(num ,(cnt.getOrDefault(num,0l) + 1)); } } public static boolean isSubsequene(char a[], char b[] ) { int i =0 , j = 0; while(i < a.length && j <b.length) { if(a[i] == b[j]) { j++; } i++; } return j >= b.length; } public static long fib(int n ,long M) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { long[][] mat = {{1, 1}, {1, 0}}; mat = pow(mat, n-1 , M); return mat[0][0]; } } public static long[][] pow(long[][] mat, int n ,long M) { if (n == 1) return mat; else if (n % 2 == 0) return pow(mul(mat, mat , M), n/2 , M); else return mul(pow(mul(mat, mat,M), n/2,M), mat , M); } static long[][] mul(long[][] p, long[][] q,long M) { long a = (p[0][0]*q[0][0] + p[0][1]*q[1][0])%M; long b = (p[0][0]*q[0][1] + p[0][1]*q[1][1])%M; long c = (p[1][0]*q[0][0] + p[1][1]*q[1][0])%M; long d = (p[1][0]*q[0][1] + p[1][1]*q[1][1])%M; return new long[][] {{a, b}, {c, d}}; } public static long[] kdane(long arr[]) { int n = arr.length; long dp[] = new long[n]; dp[0] = arr[0]; long ans = dp[0]; for(int i = 1;i<n;i++) { dp[i] = Math.max(dp[i-1] + arr[i] , arr[i]); ans = Math.max(ans , dp[i]); } return dp; } public static void update(int low , int high , int l , int r, int val , int treeIndex ,int tree[]) { if(low > r || high < l || high < low) return; if(l <= low && high <= r) { System.out.println("At " +low + " and " + high + " ans ttreeIndex " + treeIndex); tree[treeIndex] += val; return; } int mid = low + (high - low)/2; update(low , mid , l , r , val , treeIndex*2 + 1, tree); update(mid + 1 , high , l , r , val , treeIndex*2 + 2 , tree); } static int colx[] = {1 ,-1, 0,0 , 1,1,-1,-1}; static int coly[] = {0 ,0, 1,-1,1,-1,1,-1}; public static void reverse(char arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static long[] reverse(long arr[]) { long newans[] = arr.clone(); int i =0 , j = arr.length-1; while(i < j) { swap(i , j , newans); i++; j--; } return newans; } public static long inverse(long x , long mod) { return pow(x , mod -2 , mod); } public static int maxArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static long maxArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static int minArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static long minArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static int sumArray(int arr[]) { int ans = 0; for(int x : arr) { ans += x; } return ans; } public static long sumArray(long arr[]) { long ans = 0; for(long x : arr) { ans += x; } return ans; } public static long rl() { return sc.nextLong(); } public static char[] rac() { return sc.next().toCharArray(); } public static String rs() { return sc.next(); } public static char rc() { return sc.next().charAt(0); } public static int [] rai(int n) { int ans[] = new int[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextInt(); } return ans; } public static long [] ral(int n) { long ans[] = new long[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextLong(); } return ans; } public static int ri() { return sc.nextInt(); } public static int getValue(int num ) { int ans = 0; while(num > 0) { ans++; num = num&(num-1); } return ans; } public static boolean isValid(int x ,int y , int n,char arr[][],boolean visited[][][][]) { return x>=0 && x<n && y>=0 && y <n && !(arr[x][y] == '#'); } // public static Pair join(Pair a , Pair b) { // Pair res = new Pair(Math.min(a.min , b.min) , Math.max(a.max , b.max) , a.count + b.count); // return res; // } // segment tree query over range // public static int query(int node,int l , int r,int a,int b ,Pair tree[] ) { // if(tree[node].max < a || tree[node].min > b) return 0; // if(l > r) return 0; // if(tree[node].min >= a && tree[node].max <= b) { // return tree[node].count; // } // int mid = l + (r-l)/2; // int ans = query(node*2 ,l , mid ,a , b , tree) + query(node*2 +1,mid + 1, r , a , b, tree); // return ans; // } // // segment tree update over range // public static void update(int node, int i , int j ,int l , int r,long value, long arr[] ) { // if(l >= i && j >= r) { // arr[node] += value; // return; // } // if(j < l|| r < i) return; // int mid = l + (r-l)/2; // update(node*2 ,i ,j ,l,mid,value, arr); // update(node*2 +1,i ,j ,mid + 1,r, value , arr); // } public static long pow(long a , long b , long mod) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2 , mod)%mod; if(b%2 == 0) { return (ans*ans)%mod; } else { return ((ans*ans)%mod*a)%mod; } } public static long pow(long a , long b ) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2); if(b%2 == 0) { return (ans*ans); } else { return ((ans*ans)*a); } } public static boolean isVowel(char ch) { if(ch == 'a' || ch == 'e'||ch == 'i' || ch == 'o' || ch == 'u') return true; if((ch == 'A' || ch == 'E'||ch == 'I' || ch == 'O' || ch == 'U')) return true; return false; } public static int getFactor(int num) { if(num==1) return 1; int ans = 2; int k = num/2; for(int i = 2;i<=k;i++) { if(num%i==0) ans++; } return Math.abs(ans); } public static int[] readarr()throws IOException { int n = sc.nextInt(); int arr[] = new int[n]; for(int i =0;i<n;i++) { arr[i] = sc.nextInt(); } return arr; } public static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); } public static boolean isPrime(long num) { if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(long i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } public static boolean isPrime(int num) { // System.out.println("At pr " + num); if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(int i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } // public static boolean isPrime(long num) { // if(num==1) return false; // if(num<=3) return true; // if(num%2==0||num%3==0) return false; // for(int i =5;i*i<=num;i+=6) { // if(num%i==0) return false; // } // return true; // } public static long gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } public static int gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static int get_gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static long get_gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } // public static long fac(long num) { // long ans = 1; // int mod = (int)1e9+7; // for(long i = 2;i<=num;i++) { // ans = (ans*i)%mod; // } // return 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 11
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
995a0f25da267c90c8e30734fec269f9
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.*; /** * * @author eslam */ public class IceCave { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader input = new FastReader(); static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); static long mod = (long) (Math.pow(10, 9) + 7); static int dp[]; public static void main(String[] args) throws IOException { int t = input.nextInt(); while (t-- > 0) { String w = input.next(); String s = input.next(); if (s.equals("a")) { log.write("1\n"); } else if (s.contains("a")) { log.write("-1\n"); } else { log.write((long)Math.pow(2, w.length()) + "\n"); } } log.flush(); } public static long factorial(long n) { if (n <= 1) { return 1; } long t = n - 1; while (t > 1) { n *= t; t--; } return n; } public static boolean isPalindrome(int n) { int t = n; int ans = 0; while (t > 0) { ans = ans * 10 + t % 10; t /= 10; } return ans == n; } public static void graphRepresintionWithCycle(ArrayList<Integer>[] a, int q) { for (int i = 0; i < a.length; i++) { a[i] = new ArrayList<>(); } while (q-- > 0) { int x = input.nextInt() - 1; int y = input.nextInt() - 1; a[x].add(y); a[y].add(x); } } static class tri { int x, y, z; public tri(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } private static boolean isPrime(long num) { if (num == 1) { return false; } if (num == 2) { return true; } if (num % 2 == 0) { return false; } if (num == 3) { return true; } for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) { if (num % i == 0) { return false; } } return true; } public static void prefixSum(int[] a) { for (int i = 1; i < a.length; i++) { a[i] = a[i] + a[i - 1]; } } static long mod(long a, long b) { long r = a % b; return r < 0 ? r + b : r; } public static long binaryToDecimal(String w) { long r = 0; long l = 0; for (int i = w.length() - 1; i > -1; i--) { long x = (w.charAt(i) - '0') * (long) Math.pow(2, l); r = r + x; l++; } return r; } public static String decimalToBinary(long n) { String w = ""; while (n > 0) { w = n % 2 + w; n /= 2; } return w; } public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } public static void print(int a[]) throws IOException { for (int i = 0; i < a.length; i++) { log.write(a[i] + " "); } log.write("\n"); } public static void read(long[] a) { for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); } } static class pair { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } public static long LCM(long x, long y) { return x / GCD(x, y) * y; } public static long GCD(long x, long y) { if (y == 0) { return x; } return GCD(y, x % y); } }
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 11
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
2152c66b64e848b1afc60d9f86b8ac9c
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 practise { 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(); if(t.length()==1){ if(t.charAt(0)=='a'){ System.out.println(1); continue; } } boolean temp=false; for(int i=0;i<t.length();i++){ if(t.charAt(i)=='a'){ System.out.println(-1); temp=true; break; } } if(temp){ continue; } int count=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='a')count++; } if(count==1){ System.out.println(2); continue; } 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 11
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
9de59c8429cda8dfe8652517f1dfb719
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) throws Exception{ Scanner scn=new Scanner(System.in); int k=scn.nextInt(); while(k-->0){ String s=scn.next(),t=scn.next(); boolean afound=false; boolean others=false; boolean multiplea=false; int acount=0; for(char ch:t.toCharArray()){ if(ch=='a'){ afound=true; acount++; } else others=true; } if(acount>1) multiplea=true; if(afound==true && others==false){ if(multiplea==false) System.out.println("1"); else System.out.println("-1"); } else if(afound==true && others==true) System.out.println("-1"); else if(afound==false && others==true) 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 11
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
8441d2aec4198a95883772d9383ef65d
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) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0){ String s=sc.next(),p=sc.next(); Map<Character,Integer>mp=new HashMap<>(); for(int i=0;i<p.length();i++){ if(!mp.containsKey(p.charAt(i))){ mp.put(p.charAt(i),1); }else{ mp.put(p.charAt(i),mp.get(p.charAt(i))+1); } } if(p.equals("a")){ System.out.println("1"); }else if(!mp.containsKey('a')){ System.out.println((long)1<<s.length()); }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 11
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
eb1f3d76603b08bcf97ffb5802c8e3e8
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 in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); for (int i = 0; i < n; i++) { String s = in.nextLine(); String t = in.nextLine(); if(t.length() > 1 && t.contains("a")) { System.out.println("-1"); } else if (t.equals("a")) { System.out.println("1"); } else { System.out.println((long) Math.pow(2, s.length())); } } in.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 11
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
05d4ccbe966849a240ce7389bf94712d
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; public class Main { public static void main(String[] args) throws IOException { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = in.nextInt(); while(t-->0) solver.solve(in, out); out.close(); } static class PROBLEM { static class Pair{ int a; int b; Pair(int a,int b){ this.a = a; this.b = b; } } public void solve(FastReader sc, PrintWriter out ){ String s = sc.nextLine(); String t = sc.nextLine(); if(t.length()==1){ if(t.charAt(0)=='a'){ System.out.println(1); return ; } } if(t.contains("a")){ System.out.println(-1); return; } else{ long val = s.length(); long ans = (long)Math.pow(2,val); System.out.println(ans); } } } public static boolean isValid(int i,int j){ if(i<0||j<0||i>=3||j>=3){ return false; } return true; } public boolean isPerfectSquare(int x) { if (x >= 0) { int sr = (int)Math.sqrt(x); return ((sr * sr) == x); } return false; } public long sumPairs(long arr[],int n) { // final result long sum = 0; for (int i=n-1; i>=0; i--) sum += i*arr[i] - (n-1-i)*arr[i]; return sum; } static boolean isPalindrome(char[] s) { int flag = 1; for (int i = 0; i < s.length; i++) { if (s[i] != s[s.length - 1 - i]) { flag = 0; break; } } if (flag == 1) return true; else return false; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } 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()); } char nextChar() { return next().charAt(0); } boolean nextBoolean() { return !(nextInt() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } }
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 11
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
2a02e3703e4c455f51d2a21c3205ada9
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 Main { static long mod = (int)1e9+7; static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; O : while(t-->0) { String s=sc.nextLine(); String s1=sc.nextLine(); int n=s.length(); if(s1.length()==1){ if(s1.charAt(0)=='a') System.out.println(1); else System.out.println((long)Math.pow(2,n)); continue; } if(s1.indexOf('a')!=-1){ System.out.println("-1"); } else{ System.out.println((long)Math.pow(2,n)); } } out.flush(); } static int fact(int n){ if(n==1){ return 1; } else{ return n*fact(n-1); } } static void printN() { System.out.println("NO"); } static void printY() { System.out.println("YES"); } static int findfrequencies(int a[],int n) { int count=0; for(int i=0;i<a.length;i++) { if(a[i]==n) { count++; } } return count; } 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()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int[] EvenOddArragement(int nums[]) { int i1=0,i2=nums.length-1; while(i1<i2){ while(nums[i1]%2==0 && i1<i2){ i1++; } while(nums[i2]%2!=0 && i2>i1){ i2--; } int temp=nums[i1]; nums[i1]=nums[i2]; nums[i2]=temp; } return nums; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static String minLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for(int i=0;i<n;i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[0]; } static String maxLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[arr.length-1]; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i=i; this.j=j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } static class pair{ int i,j; pair(int x,int y){ i=x; j=y; } } static int binary_search(int a[],int value) { int start=0; int end=a.length-1; int mid=start+(end-start)/2; while(start<=end) { if(a[mid]==value) { return mid; } if(a[mid]>value) { end=mid-1; } else { start=mid+1; } mid=start+(end-start)/2; } return -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 11
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
199bbc238ff0503cea024f31b47a6879
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.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Math.max; import static java.lang.System.exit; import static java.util.Arrays.fill; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class cf_test { static long mod = (long) (1e9 + 7); static void solve() throws Exception { int tests = scanInt(); for (int test = 0; test < tests; test++) { // int n = scanInt(), k = scanInt(); // String l = scanString(); // out.println(); // continue test; // int n = scanInt(); String s = scanString(); String t = scanString(); if(t.length() == 1){ if(t.charAt(0) == 'a'){ out.println(1); } else{ out.println((long)Math.pow(2, s.length())); } } else{ int a = 0; for(int i = 0; i < t.length(); i++){ if(t.charAt(i) == 'a'){ a++; } } if(a > 0){ out.println(-1); } else{ out.println((long)Math.pow(2, s.length())); } } } } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } static class pairChar { Character key; int val; pairChar(Character key, int val) { this.key = key; this.val = val; } } static int[] arrI(int N) throws Exception { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = scanInt(); } return A; } static long[] arrL(int N) throws Exception { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = scanLong(); return A; } 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 sortReverse(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); int n = a.length; for (int i = 0; i < n; i++) a[n - i - 1] = 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 void sortReverse(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); int n = a.length; for (int i = 0; i < n; i++) a[n - i - 1] = l.get(i); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } static long pow(long a, long b) { long mod = 1000000007; long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) pow = (pow * x) % mod; x = (x * x) % mod; b /= 2; } return pow; } static long toggleBits(long x)// one's complement || Toggle bits { int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1; return ((1 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static 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 long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } static HashMap<Integer, Integer> getHashMap(int A[]) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } public static Map<Character, Integer> mapSortByValue(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getValue() - o2.getValue(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static Map<Character, Integer> mapSortByKey(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getKey() - o2.getKey(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(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 11
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
27abe12506b8a74a32e58941218c1e02
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.lang.Math; import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.Math; public class st49 { public static void main(String[] args) throws java.lang.Exception{ InputStreamReader r=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(r); int a=Integer.parseInt(br.readLine()); while(a--!=0) { String m=br.readLine(); String n=br.readLine(); int y=m.length(); String q=n.replaceAll("a", ""); if(n.contains("a")) { if(n.length()==1) { System.out.println(1); } else { System.out.println(-1); } } else { System.out.println((long)Math.pow(2,y)); } } }}
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 11
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
6b81ac75c06ec939114074832b538c02
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.HashSet; import java.util.Scanner; import java.util.Arrays; public class C_infinite_replacement { static long calc_str(String str1, String str2) { char[] ch = str2.toCharArray(); Arrays.sort(ch); if (ch[0] == 'a' && str2.length() > 1) return -1; else if (ch[0] == 'a') return 1; else { long ans = 1; for (long i = 1; i <= str1.length(); i++) { ans *= 2; } return ans; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testcase = sc.nextInt(); while (testcase-- > 0) { String str1 = sc.next(); String str2 = sc.next(); System.out.println(calc_str(str1, str2)); } 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 11
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
7e8ebfbca458886d45367b418d860c48
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 MyPackage; import java.util.*; import java.io.*; public class A{ static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); StringBuilder sb = new StringBuilder(); int testCases=in.nextInt(); while(testCases-- > 0){ // write code here String s = in.next(); String t = in.next(); Set<Character> set = new HashSet<>(); for(int i = 0; i < t.length(); i++) set.add(t.charAt(i)); boolean ok = true; long cnt = 0; for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(set.contains(c)) { if(t.length() > 1) { ok = false; } else cnt = 1; break; } else { cnt += (long) Math.pow(2, s.length()); break; } } if(ok) sb.append(cnt + "\n"); else sb.append(-1 + "\n"); } out.println(sb); 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 11
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
13a4245bbda8d446e11942466bce2c71
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 MyPackage; import java.util.*; import java.io.*; public class A{ static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); StringBuilder sb = new StringBuilder(); int testCases=in.nextInt(); while(testCases-- > 0){ // write code here String s = in.next(); String t = in.next(); Set<Character> set = new HashSet<>(); for(int i = 0; i < t.length(); i++) { set.add(t.charAt(i)); } long cnt = 1; if(set.contains('a')) { if(t.length() > 1) { cnt = -1; } } else { int v = s.length(); cnt = (long) (Math.pow(2, v)); } sb.append(cnt + "\n"); } out.println(sb); 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 11
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
615ee89eca557df76cccc98f8f3df12f
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 Test{ static boolean palindrome(String s,int i,int j) { if(j==s.length())return false; while(i<j) { if(s.charAt(i)!=s.charAt(j))return false; i++;j--; } return true; } static int valid(String s,int i) { Stack<Character>st=new Stack<>(); int n=s.length(); return -1; } static int msb(long num){ int c=0; while(num!=0) { num>>=1; c++; } return c-1; } static boolean isPrime(long n, int k) { // Corner cases if (n <= 1 || n == 4) return false; if (n <= 3) return true; // Try k times while (k > 0) { // Pick a random number in [2..n-2] // Above corner cases make sure that n > 4 int a = 2 + (int)(Math.random() % (n - 4)); // Fermat's little theorem if (Math.pow(a,n - 1) != 1) return false; k--; } return true; } static void swap(int a[],int i,int j){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } static boolean collinear(int x1, int y1, int x2, int y2, int x3, int y3) { int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); if (a == 0)return true; else return false; } public static void main(String[] args) throws ArithmeticException{ FastScanner fs=new FastScanner(); int t=fs.nextInt(); while(t-->0){ String s=fs.next(); String b=fs.next(); boolean ans=false; for(char c:b.toCharArray())ans=c=='a'?true:ans; if(b.length()==1 && b.charAt(0)=='a')System.out.println(1); else if(!ans) System.out.println((long)Math.pow(2,s.length())); else System.out.println(-1); } } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next(){ while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().charAt(0); } }
Java
["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 11
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
74eb0cf7566db4398058966e6f5ee10f
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 InfiniteReplacement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int test=sc.nextInt(); for(int i=0;i<test;i++) { String s=sc.next(); String t=sc.next(); if(t.length()==1 && t.charAt(0)=='a') { System.out.println(1); } else if(t.contains("a")) { System.out.println(-1); } else { double f=Math.pow(2,s.length()); System.out.println(Math.round(f)); } } } }
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 11
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
9f21f5fb4d9952c20d1fcc0fe51c4893
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 problem3 { public static void main(String[] args) { Scanner sh = new Scanner(System.in); int t = sh.nextInt(); sh.nextLine(); while(t>0){ String s = sh.nextLine(); String a = sh.nextLine(); int numa = 0; for(int i = 0; i<a.length(); i++){ if(a.charAt(i) == 'a'){ numa++; } } if(a.length() == 1 && a.charAt(0) == 'a'){ System.out.println(1); }else if(numa >0 && a.length() != 1){ System.out.println(-1); }else{ System.out.println((long)Math.pow(2,s.length())); } t--;} } }
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 11
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
7d4400fb208a87a73803385e156f7d2c
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 A{ public static void main(String[] args) throws IOException,NumberFormatException{ try { FastScanner sc=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { char s[]=sc.next().toCharArray(); char to[]=sc.next().toCharArray(); boolean contains=false; for(char c:to) { if(c=='a') { contains=true; break; } } if(contains) { if(to.length==1) { out.println(1); } else { out.println(-1); } } else { long ans=(long)Math.pow(2, s.length); out.println(ans); } } out.close(); } catch(Exception e) { return ; } } public static int GCD(int a, int b) { if(b==0) { return a; } else { return GCD(b,a%b); } } public static boolean isPrime(int n) { if(n==0||n==1) { return false; } for(int i=2;i*i<=n;i++) { if(n%i==0) { return false; } } return true; } public static class Pair<L,R> { private L l; private R r; public Pair(L l, R r){ this.l = l; this.r = r; } public L getL(){ return l; } public R getR(){ return r; } public void setL(L l){ this.l = l; } public void setR(R r){ this.r = r; } } // public static void djikstra(int a[],ArrayList<Node> adj[] ,int src,int n) { // // Arrays.fill(a, Integer.MAX_VALUE); // a[src]=0; // PriorityQueue<Node> pq=new PriorityQueue<Node>(n, new Node()); // pq.add(new Node(src,0)); // // while(!pq.isEmpty()) { // Node node=pq.remove(); // int v=node.getV(); // int weight=node.getWeight(); // for(Node it: adj[v]) { // if(it.weight+weight<a[it.getV()]) { // a[it.getV()]=it.weight+weight; // pq.add(new Node(it.getV(),a[it.getV()])); // } // } // } // } static final Random random=new Random(); 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); } public static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } 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; } } }
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 11
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
1c69f79c916011d930f8ad1bb6e6075b
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.HashSet; import java.util.Hashtable; public class C_Infinite_Replacement { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { char[] line1 = br.readLine().trim().toCharArray(); char[] line2 = br.readLine().trim().toCharArray(); long count = 1; count =(long) 1 << line1.length; // System.out.println(count); for (int i = 0; i < line2.length; i++) { if (line2[i] == 'a') { if (line2.length > 1) { count = -1; break; } else { // System.out.println(set.get(line2[i])); count = 1; break; } } } 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 11
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
3cfd33e06922b4d4d66b05078dd7cb41
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 class P { char chara; int c; public P(char chara, int c) { this.chara = chara; this.c = c; } } 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 int solve(String s1,String s2) { int c=0; for(int i=0;i<s1.length();i++) { int c1=s1.charAt(i)-'0'; int c2=s2.charAt(i)-'0'; c=c+Math.abs(c1-c2); } return c; } static Map<Long,Long> map=new HashMap<>(); static long r,res,j,n; 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 11
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
364a02120d6239f60ea25c263f1082df
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[] argrs){ Scanner scan = new Scanner(System.in); int T = scan.nextInt(); while (T -- > 0){ String s = scan.next(); String t = scan.next(); if (t.equals("a")) { System.out.println(1); continue; } int v = t.indexOf("a"); if (v != -1) System.out.println(-1); else { int len = s.length(); System.out.println((long)Math.pow(2,len)); } } } }
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 11
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
8814cf017222b1fc7d3c710ecc0af2b5
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 E { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int q = scanner.nextInt(); scanner.nextLine(); for (int l = 0; l < q; l++) { String s = scanner.nextLine(); String t = scanner.nextLine(); Long count = 0L; if (t.contains("a")) { if (t.length() > 1) { count = -1L; } else { count = 1L; } } else { count = (long)Math.pow(2, s.length()); } 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 11
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
2c985b6e6fdcedbf7d569260263149fb
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.Scanner; import java.util.Arrays; public class trial { public static void main(String[] args) { // TODO code application logic here Scanner read = new Scanner(System.in); int t = read.nextInt(); for (int i = 0; i < t; i++) { testCase(read); } } public static void testCase(Scanner read) { String s = read.next(), t = read.next(); int l = s.length() - s.replaceAll("a", "").length(), fact = 1; if (t.length() == 1 && t.charAt(0) == 'a'){ System.out.println(1); return; } else if (t.length() > 1 && t.contains("a") ){ System.out.println(-1); return; } else // while(l > 0 ) // fact *= l--; // System.out.println(++fact); 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 11
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
c314f5f4c6a6c7b17deff968a17efb6c
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 testing{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); for(int w = 0; w < t; w++){ char[] ch = in.nextLine().toCharArray(); char[] x = in.nextLine().toCharArray(); boolean flag = false; for (char c : x) { if (c == 'a') { flag = true; break; } } if(x.length==1 && flag) System.out.println(1); else if (x.length>1 && flag) System.out.println(-1); else System.out.println((long)1<<ch.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 11
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
53c49d565d5d39edd34acd2793d8d529
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.Map.Entry; import java.io.*; public class c2{ static class Pair{ int first; int last; public Pair(int first , int last){ this.first = first; this.last = last; } public int getFirst(){ return first; } public int getLast(){ return last; } } static final int M = 1000000007; // Fast input static class Hrittik_FastReader{ StringTokenizer st; BufferedReader br; public int n; public Hrittik_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(); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } double nextDouble(){ return Double.parseDouble(next()); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } } // Fast output static class Hrittik_FastWriter { private final BufferedWriter bw; public Hrittik_FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void print(Object object) throws IOException { bw.append("" + object); } public void close() throws IOException { bw.close(); } } // GCD of two integers private static int gcd(int a , int b){ if(b == 0) return a; return gcd(b, a%b); } // GCD of two long integers private static long gcd(long a , long b){ if(b == 0) return a; return gcd(b, a%b); } // LCM of two integers private static int lcm(int a, int b) { return a / gcd(a, b) * b; } // LCM of two long integers private static long lcm(long a, long b) { return a / gcd(a, b) * b; } // swap two elements of an array private static void swap(int[] arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // calculating x ^ y private static long power(long x, long y){ long res = 1; x = x % M; while(y > 0){ if ((y & 1) == 1){ res = (res * x) % M; } y = y >> 1; x = (x * x) % M; } return res; } static void sortPairByFirst(Pair arr[]){ Arrays.sort(arr , new Comparator<Pair>(){ @Override public int compare(Pair p1 , Pair p2){ return p1.first - p2.first; } }); } static void sortPairByLast(Pair arr[]){ Arrays.sort(arr , new Comparator<Pair>(){ @Override public int compare(Pair p1 , Pair p2){ return p1.last - p2.last; } }); } // A utility for creating palindrome static int createPalindrome(int input, int b, int isOdd) { int n = input; int palin = input; // checks if number of digits is odd or even // if odd then neglect the last digit of input in // finding reverse as in case of odd number of // digits middle element occur once if (isOdd == 1) n /= b; // Creates palindrome by just appending reverse // of number to itself while (n > 0) { palin = palin * b + (n % b); n /= b; } return palin; } // Function to print decimal // palindromic number static ArrayList<Integer> generatePalindromes(int n) { int number; ArrayList<Integer> ans = new ArrayList<>(); // Run two times for odd and even // length palindromes for (int j = 0; j < 2; j++) { // Creates palindrome numbers with first // half as i. Value of j decided whether // we need an odd length of even length // palindrome. int i = 1; while ((number = createPalindrome(i, 10, j % 2)) < n) { ans.add(number); i++; } } return ans; } static int subsetSum(int a[], int n, int sum) { // Storing the value -1 to the matrix int tab[][] = new int[n + 1][sum + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= sum; j++) { tab[i][j] = -1; } } // If the sum is zero it means // we got our expected sum if (sum == 0) return 1; if (n <= 0) return 0; // If the value is not -1 it means it // already call the function // with the same value. // it will save our from the repetition. if (tab[n - 1][sum] != -1) return tab[n - 1][sum]; // if the value of a[n-1] is // greater than the sum. // we call for the next value if (a[n - 1] > sum) return tab[n - 1][sum] = subsetSum(a, n - 1, sum); else { // Here we do two calls because we // don't know which value is // full-fill our criteria // that's why we doing two calls if (subsetSum(a, n - 1, sum) != 0 || subsetSum(a, n - 1, sum - a[n - 1]) != 0) { return tab[n - 1][sum] = 1; } else return tab[n - 1][sum] = 0; } } static long findWays(int[] num, int k){ int n = num.length; int[][] dp=new int[n][k+1]; for(int i=0; i<n; i++){ dp[i][0] = 1; } if(num[0]<=k) dp[0][num[0]] = 1; for(int ind = 1; ind<n; ind++){ for(int target= 1; target<=k; target++){ int notTaken = dp[ind-1][target]; int taken = 0; if(num[ind]<=target) taken = dp[ind][target-num[ind]]; dp[ind][target]= (notTaken%M + taken%M)%M; } } return dp[n-1][k]; } static int countWays(int N , int arr[]) { int count[] = new int[N + 1]; // base case count[0] = 1; // count ways for all values up // to 'N' and store the result for (int i = 1; i <= N; i++) for (int j = 0; j < arr.length; j++) // if i >= arr[j] then // accumulate count for value 'i' as // ways to form value 'i-arr[j]' if (i >= arr[j]) count[i] += count[i - arr[j]]; // required number of ways return count[N]; } static int reverseDigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } /* Function to check if n is Palindrome*/ static int isPalindrome(int n) { // get the reverse of n int rev_n = reverseDigits(n); // Check if rev_n and n are same or not. if (rev_n == n) return 1; else return 0; } private static int f(int x , int y){ if(y == 0) return 1; if(y == 1) return x; int ans = f(x,y/2); if(y%2==0){ return ans*ans; } return ans*ans*x; } public static void main(String[] args) { try { Hrittik_FastReader Sc =new Hrittik_FastReader(); Hrittik_FastWriter out = new Hrittik_FastWriter(); int t = Sc.nextInt(); outer : while(t-- > 0){ String s1 = Sc.next(); String s2 = Sc.next(); char c1[] = s1.toCharArray(); char c2[] = s2.toCharArray(); int a = 0; int n = s1.length() , m = s2.length(); for(int i = 0; i < s2.length(); i++){ if(c2[i] == 'a') a++; } if(a== 0){ System.out.println((long)(Math.pow(2, n))); } else if(a == 1 && m == 1){ System.out.println(1); } else{ System.out.println(-1); } // int a = 0; // int n = c1.length; // for(int i = 0; i < c2.length; i++){ // if(c2[i] == 'a')a++; // } // if(a == 0){ // System.out.println(f(2,n)); // } // else if(a == 1 && c2.length == 1){ // System.out.println(1); // } // else{ // System.out.println(-1); // } } 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 11
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
c3572e67ce028a3f83bbdfb44df3d069
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.StringTokenizer; public class Main{ static void solve() { String a=in.next(); String t=in.next(); if(t.length()==1&&t.equals("a")){ out.println(1); return; } if(t.indexOf('a')!=-1){ out.println(-1); return; } out.println((long)1<<a.length()); } public static void main(String[] args) throws Exception { int cnt=in.nextInt(); while (cnt-->0){ solve(); } out.flush(); } static class FastReader{ StringTokenizer st; BufferedReader br; 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 PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); }
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 11
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
ccbdd4be79903d7cd75698d8d5f71276
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.HashSet; import java.util.InputMismatchException; /* @author : sanskarXrawat @date : 4/21/2022 @time : 9:12 PM */ @SuppressWarnings("ALL") public class A { public static void main(String[] args) throws Throwable { Thread thread = new Thread (null, new TaskAdapter (), "", 1 << 29); thread.start (); thread.join (); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput (inputStream); FastOutput out = new FastOutput (outputStream); Solution solver = new Solution (); solver.solve (1, in, out); in.close (); out.close (); } } @SuppressWarnings("unused") static class Solution { static final Debug debug = new Debug (true); public void solve(int testNumber, FastInput in, FastOutput out) { int test = in.ri (); while (test-- > 0) { String s=in.readString (); String t=in.readString (); if(t.contains ("a") && t.length ()>1){ out.prtl (-1); } else if(t.contains ("a") && t.length ()==1){ out.prtl (1); } else{ out.prtl ((long) Math.pow (2,s.length ())); } } } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private final StringBuilder cache = new StringBuilder (THRESHOLD * 2); private static final int THRESHOLD = 32 << 10; private final Writer os; public FastOutput append(CharSequence csq) { cache.append (csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append (csq, start, end); return this; } private void afterWrite() { if (cache.length () < THRESHOLD) { return; } flush (); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this (new OutputStreamWriter (os)); } public FastOutput append(char c) { cache.append (c); afterWrite (); return this; } public FastOutput append(String c) { cache.append (c); afterWrite (); return this; } public FastOutput println(String c) { return append (c).println (); } public FastOutput println() { return append ('\n'); } final <T> void prt(T a) { append (a + " "); } final <T> void prtl(T a) { append (a + "\n"); } public FastOutput flush() { try { os.append (cache); os.flush (); cache.setLength (0); } catch (IOException e) { throw new UncheckedIOException (e); } return this; } public void close() { flush (); try { os.close (); } catch (IOException e) { throw new UncheckedIOException (e); } } public String toString() { return cache.toString (); } public FastOutput printf(String format, Object... args) { return append (String.format (format, args)); } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } } static class FastInput { private final StringBuilder defaultStringBuf = new StringBuilder (1 << 13); private final ByteBuffer tokenBuf = new ByteBuffer (); private final byte[] buf = new byte[1 << 13]; private SpaceCharFilter filter; private final InputStream is; private int bufOffset; private int bufLen; private int next; private int ptr; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read (buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read (); } } public String next() { return readString (); } public int ri() { return readInt (); } public int readInt() { boolean rev = false; skipBlank (); if (next == '+' || next == '-') { rev = next == '-'; next = read (); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read (); } return rev ? val : -val; } public long readLong() { boolean rev = false; skipBlank (); if (next == '+' || next == '-') { rev = next == '-'; next = read (); } long val = 0L; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read (); } return rev ? val : -val; } public long rl() { return readLong (); } public String readString(StringBuilder builder) { skipBlank (); while (next > 32) { builder.append ((char) next); next = read (); } return builder.toString (); } public String readString() { defaultStringBuf.setLength (0); return readString (defaultStringBuf); } public int rs(char[] data, int offset) { return readString (data, offset); } public char[] rsc() { return readString ().toCharArray (); } public int rs(char[] data) { return rs (data, 0); } public int readString(char[] data, int offset) { skipBlank (); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read (); } return offset - originalOffset; } public char rc() { return readChar (); } public char readChar() { skipBlank (); char c = (char) next; next = read (); return c; } public double rd() { return nextDouble (); } public double nextDouble() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } double res = 0; while (!isSpaceChar (c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow (10, readInt ()); if (c < '0' || c > '9') throw new InputMismatchException (); res *= 10; res += c - '0'; c = read (); } if (c == '.') { c = read (); double m = 1; while (!isSpaceChar (c)) { if (c == 'e' || c == 'E') return res * Math.pow (10, readInt ()); if (c < '0' || c > '9') throw new InputMismatchException (); m /= 10; res += (c - '0') * m; c = read (); } } return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar (c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public final int readByteUnsafe() { if (ptr < bufLen) return buf[ptr++]; ptr = 0; try { bufLen = is.read (buf); if (bufLen > 0) { return buf[ptr++]; } else { return -1; } } catch (IOException e) { throw new UncheckedIOException (e); } } public final int readByte() { if (ptr < bufLen) return buf[ptr++]; ptr = 0; try { bufLen = is.read (buf); if (bufLen > 0) { return buf[ptr++]; } else { throw new java.io.EOFException (); } } catch (IOException e) { throw new UncheckedIOException (e); } } public final String nextLine() { tokenBuf.clear (); for (int b = readByte (); b != '\n'; b = readByteUnsafe ()) { if (b == -1) break; tokenBuf.append (b); } return new String (tokenBuf.getRawBuf (), 0, tokenBuf.size ()); } public final String nl() { return nextLine (); } public final boolean hasNext() { for (int b = readByteUnsafe (); b <= 32 || b >= 127; b = readByteUnsafe ()) { if (b == -1) return false; } --ptr; return true; } public void readArray(Object T) { if (T instanceof int[]) { int[] arr = (int[]) T; for (int i = 0; i < arr.length; i++) { arr[i] = ri (); } } if (T instanceof long[]) { long[] arr = (long[]) T; for (int i = 0; i < arr.length; i++) { arr[i] = rl (); } } if (T instanceof double[]) { double[] arr = (double[]) T; for (int i = 0; i < arr.length; i++) { arr[i] = rd (); } } if (T instanceof char[]) { char[] arr = (char[]) T; for (int i = 0; i < arr.length; i++) { arr[i] = readChar (); } } if (T instanceof String[]) { String[] arr = (String[]) T; for (int i = 0; i < arr.length; i++) { arr[i] = next (); } } if (T instanceof int[][]) { int[][] arr = (int[][]) T; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { arr[i][j] = ri (); } } } if (T instanceof char[][]) { char[][] arr = (char[][]) T; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { arr[i][j] = readChar (); } } } if (T instanceof long[][]) { long[][] arr = (long[][]) T; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { arr[i][j] = rl (); } } } } public final void close() { try { is.close (); } catch (IOException e) { throw new UncheckedIOException (e); } } private static final class ByteBuffer { private static final int DEFAULT_BUF_SIZE = 1 << 12; private byte[] buf; private int ptr = 0; private ByteBuffer(int capacity) { this.buf = new byte[capacity]; } private ByteBuffer() { this (DEFAULT_BUF_SIZE); } private ByteBuffer append(int b) { if (ptr == buf.length) { int newLength = buf.length << 1; byte[] newBuf = new byte[newLength]; System.arraycopy (buf, 0, newBuf, 0, buf.length); buf = newBuf; } buf[ptr++] = (byte) b; return this; } private char[] toCharArray() { char[] chs = new char[ptr]; for (int i = 0; i < ptr; i++) { chs[i] = (char) buf[i]; } return chs; } private byte[] getRawBuf() { return buf; } private int size() { return ptr; } private void clear() { ptr = 0; } } } static class Debug { private final boolean offline; private final PrintStream out = System.err; static int[] empty = new int[0]; public Debug(boolean enable) { offline = enable && System.getSecurityManager () == null; } public Debug debug(String name, Object x) { return debug (name, x, empty); } public Debug debug(String name, long x) { if (offline) { debug (name, "" + x); } return this; } public Debug debug(String name, String x) { if (offline) { out.printf ("%s=%s", name, x); out.println (); } return this; } public Debug debug(String name, Object x, int... indexes) { if (offline) { if (x == null || !x.getClass ().isArray ()) { out.append (name); for (int i : indexes) { out.printf ("[%d]", i); } out.append ("=").append ("" + x); out.println (); } else { indexes = Arrays.copyOf (indexes, indexes.length + 1); if (x instanceof byte[]) { byte[] arr = (byte[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof short[]) { short[] arr = (short[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof boolean[]) { boolean[] arr = (boolean[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof char[]) { char[] arr = (char[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof int[]) { int[] arr = (int[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof float[]) { float[] arr = (float[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof double[]) { double[] arr = (double[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof long[]) { long[] arr = (long[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else { Object[] arr = (Object[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } } } return this; } } }
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 11
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
f48d8e6d3f7ffe1156c7c325dcf7bffc
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 Problem10 { static long binpow(long a,long b) { if (b == 0) return 1; long res = binpow(a, b / 2); if (b % 2 != 0) return res * res * a; else return res * res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); for(int i = 0;i<n;i++){ String a = sc.nextLine(); String b = sc.nextLine(); int count = 0; for(int j = 0;j<b.length();j++){ if(b.charAt(j) == 'a'){ count++; } } if(count == 0){ System.out.println(binpow(2,a.length())); } else if(b.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 11
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
4b19c6dbb05466e3e8482da60705917e
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.*; public class diffstrings { public static void main (String[] args) throws IOException { PrintWriter pw = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { String s = br.readLine(); String s2 = br.readLine(); int ans = 0; int as = 0; for (int j = 0; j < s2.length(); j++) { char s22 = s2.charAt(j); if (s22 == 'a') { as++; } } if (as > 1) { pw.println(-1); continue; } else if (as == 1 && s2.length() != 1) { pw.println(-1); continue; } else if (as == 1) { pw.println(1); continue; } pw.println((long)Math.pow(2,s.length()-as)); } 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 11
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
ccd8f1e808c60126d6b6508f22cb7b60
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
//some updates in import stuff import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; //key points learned //max space ever that could be alloted in a program to pass in cf //int[][] prefixSum = new int[201][200_005]; -> not a single array more!!! //never allocate memory again again to such bigg array, it will give memory exceeded for sure //believe in your fucking solution and keep improving it!!! (sometimes) //few things to figure around //getting better and faster at taking input/output with normal method (buffered reader and printwriter) //memorise all the key algos! a public class C{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List<Integer> primeNumbers = new ArrayList<>(); public static void main(String[] args) { MyScanner sc = new MyScanner(); //pretty important for sure - out = new PrintWriter(new BufferedOutputStream(System.out)); //dope shit output for sure //code here int test = sc.nextInt(); int n= 100; long[] fact = new long[2 * n + 1]; long[] ifact = new long[2 * n + 1]; fact[0] = 1; ifact[0] = 1; for (long i = 1; i <= 2 * n; i++) { fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod); ifact[(int)i] = mminvprime(fact[(int)i], mod); } while(test --> 0){ char[] f = sc.nextLine().toCharArray(); char[] s = sc.nextLine().toCharArray(); //after this think about what to do next //if s contains a and any other letter boolean apre = false; for(char ch : s){ if(ch == 'a') apre = true; } if(apre && s.length > 1){ out.println(-1); continue; } if(apre && s.length == 1){ out.println(1); continue; } //otherwise simple count how many a's are tehre long ans = power(2, f.length); out.println(ans); } out.close(); } //new stuff to learn (whenever this is need for them, then only) //Lazy Segment Trees //Persistent Segment Trees //Square Root Decomposition //Geometry & Convex Hull //High Level DP -- yk yk //String Matching Algorithms //Heavy light Decomposition //Updation Required //Fenwick Tree - both are done (sum) //Segment Tree - both are done (min, max, sum) //-----CURRENTLY PRESENT-------// //Graph //DSU //powerMODe //power //Segment Tree (work on this one) //Prime Sieve //Count Divisors //Next Permutation //Get NCR //isVowel //Sort (int) //Sort (long) //Binomial Coefficient //Pair //Triplet //lcm (int & long) //gcd (int & long) //gcd (for binomial coefficient) //swap (int & char) //reverse //primeExponentCounts //Fast input and output //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //GRAPH (basic structure) public static class Graph{ public int V; public ArrayList<ArrayList<Integer>> edges; //2 -> [0,1,2] (current) Graph(int V){ this.V = V; edges = new ArrayList<>(V+1); for(int i= 0; i <= V; i++){ edges.add(new ArrayList<>()); } } public void addEdge(int from , int to){ edges.get(from).add(to); edges.get(to).add(from); } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ //union by size if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod public static long powerMOD(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ x %= mod; res %= mod; res = (res * x)%mod; } // y must be even now y = y >> 1; // y = y/2 x%= mod; x = (x * x)%mod; // Change x to x^2 } return res%mod; } //without mod public static long power(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ res = (res * x); } // y must be even now y = y >> 1; // y = y/ x = (x * x); } return res; } public static class segmentTree{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query -> O(logn) //update -> O(logn) //update-range -> O(n) (worst case) //simple iteration and stuff for sure public segmentTree(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long query(int s, int e, int qs , int qe, int index){ //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //no overlap if(qe < s || qs > e){ return Long.MAX_VALUE; } //partial overlap int mid = (s + e)/2; long left = query( s, mid , qs, qe, 2*index); long right = query( mid + 1, e, qs, qe, 2*index + 1); return min(left, right); } //gonna do range updates for sure now!! //let's do this bois!!! (solve this problem for sure) public void updateRange(int s, int e, int l, int r, long increment, int index){ //out of bounds if(l > e || r < s){ return; } //leaf node if(s == e){ tree[index] += increment; return; //behnchoda return tera baap krvayege? } //recursive case int mid = (s + e)/2; updateRange(s, mid, l, r, increment, 2 * index); updateRange(mid + 1, e, l, r, increment, 2 * index + 1); tree[index] = min(tree[2 * index], tree[2 * index + 1]); } } public static class segmentTreeLazy{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; public long[] lazy; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query-range -> O(logn) //lazy update-range -> O(logn) (imp) //simple iteration and stuff for sure public segmentTreeLazy(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; lazy = new long[1000000]; //pretty big for no inconvenience (no?) NONONONOONON! NO fucker NO! } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long queryLazy(int s, int e, int qs, int qe, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > qe || e < qs){ return Long.MAX_VALUE; } //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //partial overlap int mid = (s + e)/2; long left = queryLazy(s, mid, qs, qe, 2 * index); long right = queryLazy(mid + 1, e, qs, qe, 2 * index + 1); return Math.min(left, right); } //update range in O(logn) -- using lazy array public void updateRangeLazy(int s, int e, int l, int r, int inc, int index){ //before going down resolve if it exist if(lazy[index] != 0){ tree[index] += lazy[index]; //non leaf node if(s != e){ lazy[2*index] += lazy[index]; lazy[2*index + 1] += lazy[index]; } lazy[index] = 0; //clear the lazy value at current node for sure } //no overlap if(s > r || l > e){ return; } //another case if(l <= s && e <= r){ tree[index] += inc; //create a new lazy value for children node if(s != e){ lazy[2*index] += inc; lazy[2*index + 1] += inc; } return; } //recursive case int mid = (s + e)/2; updateRangeLazy(s, mid, l, r, inc, 2*index); updateRangeLazy(mid + 1, e, l, r, inc, 2*index + 1); //update the tree index tree[index] = Math.min(tree[2*index], tree[2*index + 1]); return; } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //primeExponentCounts public static int primeExponentsCount(int n) { if (n <= 1) return 0; int sqrt = (int) Math.sqrt(n); int remainingNumber = n; int result = 0; for (int i = 2; i <= sqrt; i++) { while (remainingNumber % i == 0) { result++; remainingNumber /= i; } } //in case of prime numbers this would happen if (remainingNumber > 1) { result++; } return result; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } //to sort the array with better method 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); } //sort long 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); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; public int hashCode; Pair(int a , int b){ this.a = a; this.b = b; this.hashCode = Objects.hash(a, b); } @Override public String toString(){ return a + " -> " + b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair that = (Pair) o; return a == that.a && b == that.b; } @Override public int hashCode() { return this.hashCode; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } public static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second a = (a * a) % mod; b = b >> 1; } return res; } //SOME EXTRA DOPE FUNCTIONS public static long mminvprime(long a, long b) { return expo(a, b - 2, b); } public static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } public static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } public static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } public static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } //O(n) every single time remember that public static long nCr(long N, long K , long mod){ long upper = 1L; long lower = 1L; long lowerr = 1L; for(long i = 1; i <= N; i++){ upper = mod_mul(upper, i, mod); } for(long i = 1; i <= K; i++){ lower = mod_mul(lower, i, mod); } for(long i = 1; i <= (N - K); i++){ lowerr = mod_mul(lowerr, i, mod); } // out.println(upper + " " + lower + " " + lowerr); long answer = mod_mul(lower, lowerr, mod); answer = mod_div(upper, answer, mod); return answer; } // long[] fact = new long[2 * n + 1]; // long[] ifact = new long[2 * n + 1]; // fact[0] = 1; // ifact[0] = 1; // for (long i = 1; i <= 2 * n; i++) // { // fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod); // ifact[(int)i] = mminvprime(fact[(int)i], mod); // } //ifact is basically inverse factorial in here!!!!!(imp) public static long combination(long n, long r, long m, long[] fact, long[] ifact) { long val1 = fact[(int)n]; long val2 = ifact[(int)(n - r)]; long val3 = ifact[(int)r]; return (((val1 * val2) % m) * val3) % m; } //-----------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 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 11
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
d5b33ba3fc0dd0914e38c252c2fdc2d5
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.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; public class CF1674C { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8)); int t = Integer.parseInt(reader.readLine()); for (int i = 0; i < t; i++) { String line1 = reader.readLine(); String line2 = reader.readLine(); writer.write(solution(line1, line2).concat(System.lineSeparator())); } writer.close(); reader.close(); } private static String solution(String line1, String line2) { // t 等于 "a" 换了等于没换 if (line2.equals("a")) { return "1"; } // 如果 t 含 'a' 且不为 "a",则无限大 if (line2.contains("a")) { return "-1"; } int n = line1.length(); return String.valueOf((long) Math.pow(2, 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 11
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
866f6b4203bd4415dbef0cfe07f7f2a4
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.InputStreamReader; import java.util.HashSet; public class InfiniteReplacement { // private static HashSet<String> hs = new HashSet<>(); public static void main(String[] args) throws Exception { var br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); var sb = new StringBuilder(); // generatePairs("",0 , 2, "ba"); // System.out.println(hs); // System.out.println(hs.size()); while (t-- > 0) { String s1 = br.readLine().trim(); String s2 = br.readLine().trim(); sb.append(solve(s1,s2)).append("\n"); } br.close(); System.out.println(sb); } private static long solve(String s1, String s2) { if(s2.contains("a")) { if(s2.length() == 1) return 1; return -1; } return 1l<<s1.length(); } // private static void generatePairs(String prev, int cur, int end, String rep) { // if(cur == end) { // hs.add(prev); // return; // } // // generatePairs(prev+rep, cur+1, end, rep); // generatePairs(prev+'a', cur+1, end, rep); // } }
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 11
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
cb37a99ac59ea00c1c58df554e6cc431
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.lang.*; public class Codechef { static 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[200001]; // 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(); }} static void cout(Object line) {System.out.println(line);} static void iop() { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); }} static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b);} static boolean isPerfectSquare(long n){ if (Math.ceil((double)Math.sqrt(n)) ==Math.floor((double)Math.sqrt(n))) return true; else return false;} static boolean isPrime(int n){ if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true;} static int fact(int n){ return (n == 1 || n == 0) ? 1 : n * fact(n - 1);} static boolean isPowerOfTwo (int x) { /* First x in the below expression is for the case when x is 0 */ return x!=0 && ((x&(x-1)) == 0);} static void printArray(int arr[]) { for(int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); }} public static void main(String[] args) throws IOException{ iop(); Reader sr = new Reader(); Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0) { String str = s.next(); String st = s.next(); char ch1[] = str.toCharArray(); char ch[] = st.toCharArray(); int cnt = 0,count = 0; for(int i = 0; i < ch.length; i++) { if(ch[i] == 'a') cnt++; } for(int i = 0; i < ch1.length; i++) { if(ch1[i] == 'a') count++; } if(cnt == 0) cout((long)Math.pow(2,count)); else if(cnt == 1 && ch.length == 1) cout(1); else cout(-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 11
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
b8bc8377d6e86716595f88c2639afe47
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) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int T = sc.nextInt(); while (T-- > 0) { String s, t; s = sc.next(); t = sc.next(); int count = 0; for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == 'a') count++; } if (count > 1 || count >= 1 && t.length() > 1) out.println(-1); else if (count == 1 && t.length() == 1) out.println(1); else if (count == 0) out.println(1L << s.length()*1L); } out.close(); } static final Random random = new Random(); static final int mod = 1_000_000_007; 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); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; } public static long power(long a, long p) { long res = 1; while (p > 0) { if ((p & 1) == 1) res = (res * a) % mod; a = (a * a) % mod; p >>= 1; } return res; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } 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 nextLong() { return Long.parseLong(next()); } } static void reverseSort(int arr[]) { ArrayList<Integer> a = new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a, Comparator.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = a.get(i); } } } /* * boolean[] sieve = new boolean[1_000_001]; * Arrays.fill(sieve, true); * sieve[0] = false; * sieve[1] = false; * * for (int x = 2; x * x < sieve.Lengthgth; x++) { * if (sieve[x]) { * for (int j = x; j * x < sieve.Lengthgth; j++) { * sieve[j * x] = 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 11
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
e763b1b6a9f138454db4b3c139fba656
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.Scanner; public class C { public static void main(String[] args) { Scanner go = new Scanner(System.in); int q = go.nextInt(); while (q-->0){ String s = go.next(); String t = go.next(); int co = 0; for (int i =0; i<t.length(); i++){ if (t.charAt(i) == 'a'){ co++; } } if (co > 0){ if (co == 1 && t.length() == 1){ System.out.println(1); }else { System.out.println("-1"); } }else { BigInteger ans = BigInteger.valueOf(0); for (int i = 1; i<s.length(); i++){ BigInteger fi = BigInteger.valueOf(1); BigInteger se =BigInteger.valueOf(1); for (int j = 1; j <= i; j++){ se = se.multiply(BigInteger.valueOf(j)); } for (int j = s.length()-i+1; j <= s.length(); j++){ fi = fi.multiply(BigInteger.valueOf(j)); } ans = ans.add(fi.divide(se)); } ans = ans.add(BigInteger.valueOf(2)); System.out.println(ans); } } } public static BigInteger Fac(int i){ if (i == 0){ return BigInteger.valueOf(1); } BigInteger re = BigInteger.valueOf(1); for (int j=1; j<=i; j++){ re = re.multiply(BigInteger.valueOf(j)); } return re; } } /* aaaaaa bfe 000 1 1 1 11 11 1 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 11
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
1d202ecc4a510493e96d24d794c57a9e
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 infinite_rep { public static void main(String args[]) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { String first_word = in.next(); String second_word = in.next(); if (second_word.length() == 1 && ((second_word.charAt(0)) == 'a' )) { System.out.println(1); continue; } if (second_word.indexOf( 'a' ) >-1) { System.out.println(-1); continue; } /* else if(b && s1.length()>1) System.out.println(-1);*/ System.out.println((long) Math.pow(2, first_word.length())); } in.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 11
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
d4e7f4a35155a7650c1314093389257b
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 test { public static void main(String argss[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++){ String s1 = sc.next(); String s2 = sc.next(); int x=s2.indexOf('a'); if (s2.length() == 1 && s2.charAt(0) == 'a') { System.out.println(1); continue; } if(x>-1){ System.out.println(-1); continue; } System.out.println((long)Math.pow(2,s1.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 11
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
55d6b80baeb5a678910c91e2d8af3827
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 whatever; // 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 Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner( System.in ); int y = in.nextInt(); for(int i = 0;i<y;i++){ String a = in.next(); String t = in.next(); boolean b = false; int p = 0; long k = (long)Math.pow(2,a.length()); int x = t.length(); for(int j = 0;j<t.length();j++){ if(t.charAt(j)=='a'){ p++; } } if(p==0){ b = true; } if(b){ System.out.println(k); }else{ if(t.charAt(0)=='a'&&x==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 11
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
c3a3d60a748734da41526c8b61f08ed4
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); int x=sc.nextInt(); while(x-->0) { String s1=sc.next(); String s2=sc.next();int c=0; int a=s1.length();//System.out.println(a+" lllll"); char ch[]=s2.toCharArray(); for(int i=0;i<s2.length();i++) { if(ch[i]=='a') { c++; break; } } if(s2.length()==1&&ch[0]=='a') { System.out.println("1"); continue; } if(c==1) { System.out.println("-1"); continue; } System.out.println((long)(Math.pow(2,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 11
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
4a073bc371f4abacb2e09dc073a15a56
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 cin = new Scanner(System.in); int t = cin.nextInt(); while (t != 0) { String a = cin.next(); String b = cin.next(); int countA = 0; for (int i = 0; i < b.length(); i++) { if (b.charAt(i) == 'a') ++countA; } if (b.length() == 1 && b.charAt(0) == 'a') System.out.println(1); else { if (countA == 0) System.out.println((long) Math.pow(2, a.length())); else System.out.println(-1); } t--; } } }
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 11
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
741fd6e871a58af959a6a8d6486124d3
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 Lol { //Hi my name is Vijay, My age is 27, My birthday is 17th Oct 1995. public static void main(String[] args) { Scanner sc = new Scanner(System.in); //System.out.println('c'-'a'); int t = sc.nextInt(); while (t-- > 0) { String s=sc.next(); String x=sc.next(); if(x.equals("a")) System.out.println(1); else if(x.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 11
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
0b5642df2e79b1e5d382d22dad95244f
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); int t = sc.nextInt(); while (t-- > 0) { String str = sc.next(); String s =sc.next(); long count = 0; for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == 'a') count++; } if(Objects.equals(s, "a")) System.out.println(1); else if(count !=0) 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 11
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
fcfabd8ab731126251f0f163e0bdb0ad
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 Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }} public static void main(String[] args) { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { String s=sc.next(); String x=sc.next(); if(x.equals("a")) System.out.println(1); else if(x.length()>1 && x.contains("a")) System.out.println(-1); else { long count=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='a') count++; } long ans=0; for(long i=1;i<=count;i++) ans+=bino(count,i); System.out.println(ans+1); } } } public static long bino(long n,long r) { if(n<r || n==0) return 1; long num = 1, den = 1; for(long i=0; i<r; i++){ num*=n-i; num/=(i+1); } return num; } }
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 11
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
8864aca50faa67eb8dc001504415770e
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.OutputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.StringTokenizer; public class cf1674C { // https://codeforces.com/problemset/problem/1674/C public static void main(String[] args) { Kattio io = new Kattio(); int q = io.nextInt(), a; String s; char[] c; HashSet<Character> ca = new HashSet<>(); for (int i = 0; i < q; i++) { a = io.next().length(); s = io.next(); c = s.toCharArray(); for (int k = 0; k < c.length; k++) ca.add(c[k]); if (c.length > 1 && ca.contains('a')) { io.println(-1); } else if (c[0] == 'a'){ io.println(1); } else { io.println((long) Math.pow(2, a)); } ca.clear(); } // program io.close(); } // Kattio static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in,System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName+".out"); r = new BufferedReader(new FileReader(problemName+".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public String nextLine() { try { st = null; return r.readLine(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public 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 11
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
92b1110ba341323e0956f41bb05fbba8
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.Scanner; public class temp { public static void main(String [] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ String x = sc.next(); String y = sc.next(); int count =0; for(int i=0;i<y.length();i++)if(y.charAt(i)=='a')count++; if(y.equals("a")){ System.out.println(1); continue; } else if(count>=1 && y.length()>=2){ System.out.println(-1); } else System.out.println((long)Math.pow(2,x.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 11
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
6c3c32d64032a38188e2ef90bdf994aa
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; import java.math.BigInteger; import java.nio.charset.StandardCharsets; public class C { public static void main(String[] args){ Scanner s = new Scanner(System.in); int q = Integer.parseInt(s.nextLine()); while(q>0){ String string1,string2; string1 = s.nextLine(); string2 = s.nextLine(); //System.out.println(bytes[0] + " " + bytes[1] + " lenght:" + bytes.length); boolean bandera = true; for(int i=0;i<string2.length() && bandera;i++){ if(string2.charAt(i) == 'a'){ bandera = false; } } if(!bandera && string2.length() > 1){ System.out.println("-1"); } else{ if(string2.length() == 1 && string2.charAt(0) == 'a'){ System.out.println(1); } else{ System.out.println(Math.round(Math.pow(2,string1.length()))); } } q--; } } }
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 11
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
df4cfbb1e9b25786bb532115ae3b61b3
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 Practice { static boolean multipleTC = true; final static int mod2 = 1000000007; final static int mod = 998244353; final double E = 2.7182818284590452354; final double PI = 3.14159265358979323846; int MAX = 1005; boolean sieve[]; int pf[]; ArrayList<Integer> primes; void setup() { sieve = new boolean[MAX + 1]; pf = new int[MAX + 1]; for (int i = 1; i <= MAX; i++) { pf[i] = i; } sieve[0] = sieve[1] = true; for (int i = 2; i * i <= MAX; i++) { if (!sieve[i]) { for (int j = i * i; j <= MAX; j += i) { sieve[j] = true; pf[j] = i; } } } primes = new ArrayList<>(); for (int i = 2; i <= MAX; i++) { if (!sieve[i]) { primes.add(i); } } } void pre() throws Exception { setup(); } // All the best void solve(int TC) throws Exception { char s[] = nln().toCharArray(); char s2[] = nln().toCharArray(); int n = s.length, n2 = s2.length; int acount = 0, acount2 = 0; for (char c : s) { if (c == 'a') acount++; } for (char c : s2) { if (c == 'a') acount2++; } if (acount2 == 0) { pn(pow(2,acount)); } else if ((acount == n && acount2 == n2 && n2==1) || acount==0) { pn("1"); } else { pn("-1"); } } public long pow(long base, long exp) { long res = 1; while (exp > 0) { if ((exp & 1) == 1) res = res * base; base = base * base; exp >>= 1; } return res; } 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); } double dist(int x1, int y1, int x2, int y2) { double a = x1 - x2, b = y1 - y2; return Math.sqrt((a * a) + (b * b)); } int[] readArr(int n) throws Exception { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } void sort(int arr[], int left, int right) { ArrayList<Integer> list = new ArrayList<>(); for (int i = left; i <= right; i++) list.add(arr[i]); Collections.sort(list); for (int i = left; i <= right; i++) arr[i] = list.get(i - left); } void sort(int arr[]) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < arr.length; i++) arr[i] = list.get(i); } public long max(long... arr) { long max = arr[0]; for (long itr : arr) max = Math.max(max, itr); return max; } public int max(int... arr) { int max = arr[0]; for (int itr : arr) max = Math.max(max, itr); return max; } public long min(long... arr) { long min = arr[0]; for (long itr : arr) min = Math.min(min, itr); return min; } public int min(int... arr) { int min = arr[0]; for (int itr : arr) min = Math.min(min, itr); return min; } public long sum(long... arr) { long sum = 0; for (long itr : arr) sum += itr; return sum; } public long sum(int... arr) { long sum = 0; for (int itr : arr) sum += itr; return sum; } String bin(long n) { return Long.toBinaryString(n); } String bin(int n) { return Integer.toBinaryString(n); } static int bitCount(int x) { return x == 0 ? 0 : (1 + bitCount(x & (x - 1))); } static void dbg(Object... o) { System.err.println(Arrays.deepToString(o)); } int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } int abs(int a) { return (a < 0) ? -a : a; } long abs(long a) { return (a < 0) ? -a : a; } void p(Object o) { out.print(o); } void pn(Object o) { out.println(o); } void pni(Object o) { out.println(o); out.flush(); } void pn(int[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } void pn(long[] arr) { int n = arr.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(arr[i] + " "); } pn(sb); } String n() throws Exception { return in.next(); } String nln() throws Exception { return in.nextLine(); } int ni() throws Exception { return Integer.parseInt(in.next()); } long nl() throws Exception { return Long.parseLong(in.next()); } double nd() throws Exception { return Double.parseDouble(in.next()); } public static void main(String[] args) throws Exception { new Practice().run(); } FastReader in; PrintWriter out; void run() throws Exception { in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } 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 11
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
cd0a7ba3bc508068626fdbbb5dd21d37
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.math.BigDecimal; import java.math.BigInteger; import java.util.*; //import java.util.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Integer time = Integer.parseInt(br.readLine()); while (time-->0){ String s = br.readLine(); String t = br.readLine(); if(isInfinite(t)){ System.out.println(-1); }else if(t.equals("a")){ System.out.println(1); }else{ long ans = 1; for(int i=1;i<=s.length();i++){ ans*=2; } System.out.println(ans); } } } private static boolean isInfinite(String t) { if(t.length()==1) return false; for(int i=0;i<t.length();i++){ if(t.charAt(i)=='a') return true; } 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 11
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
507e87904f46e2d8bdf39134adaaefb7
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.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int q = Integer.parseInt(br.readLine()); while(q-->0) { String s = br.readLine(); String t = br.readLine(); int count=0; for(int i=0 ; i<t.length() ; i++) { if(t.charAt(i) == 'a') count++; } if(count == 0) { long x = 1; long res = (x<<(s.length())); bw.write(res+"\n"); }else if(count==1 && t.length()==1) { bw.write("1\n"); }else { bw.write("-1\n"); } } bw.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 11
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
e305545416105a85f4cb35b017dccacc
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 Infinite_Replacement { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int q = sc.nextInt(); sc.nextLine(); while(q-- > 0) { String s = sc.nextLine(); String t = sc.nextLine(); int length_of_t = t.length(); int length_of_s = s.length(); if(length_of_t == 1 && t.charAt(0) == 'a') { System.out.println(1); } else { boolean is_infinite = false; for(int i = 0 ; i < length_of_t ; i++) { if(t.charAt(i) == 'a') { is_infinite = true; break; } } if(is_infinite) { System.out.println(-1); } else { // int sum = 4*(length_of_s-1)//(length_of_s*length_of_s)+1; long sum = 2; sum <<= (length_of_s-1); System.out.println(sum); } } } 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 11
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
978edc2e659418f25db1f9b0fee4c602
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 HelloWorld{ public static void main(String []args){ Scanner sc=new Scanner(System.in); int m=sc.nextInt(); while(m-->0){ String s=sc.next(); String t=sc.next(); if(t.length()==1 && t.charAt(0)=='a'){ System.out.println("1"); }else if(t.contains("a")){ System.out.println("-1"); }else{ long 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 11
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
55d31ecf073a7305f1842d792f2538cf
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 Solution { static long cal(int n) { return (1L << n); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = sc.nextInt();//, x = 1; while (t -- > 0) { String first = sc.next(); String second = sc.next(); HashMap<Character, Integer> fir = new HashMap<>(); HashMap<Character, Integer> sec = new HashMap<>(); for (char c : first.toCharArray()) { fir.put(c, fir.getOrDefault(c, 0) + 1); } for (char c : second.toCharArray()) { sec.put(c, sec.getOrDefault(c, 0) + 1); } long ans = 0, occur = cal(fir.getOrDefault('a', 0)); if (second.equals("a")) ans = 1; else if (second.length() == 1) { ans = occur ; } else if (first.equals("a")) { if (second.length() >= 2 && sec.getOrDefault('a', 0) > 0) ans = -1; else ans = 2; } else if (fir.getOrDefault('a', 0) == 0) ans = 1; else { if (fir.getOrDefault('a', 0) > 0 && sec.getOrDefault('a', 0) > 0) ans = -1; else ans = occur; } out.write(ans + "\n"); } out.flush(); } static { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); err = new PrintStream(new FileOutputStream("error.txt")); } catch (Exception e) {} } static PrintStream err; }
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 11
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
069ab5bbfd7c04034e252c8683809fcc
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 NewProgramJava { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T>0){ T--; StringBuilder str1 = new StringBuilder(sc.next()); StringBuilder str2 = new StringBuilder(sc.next()); HashSet<String> hs = new HashSet<>(); StringBuilder strrep = str1; if (str2.toString().equals("a")){ System.out.println(1); } else if (str2.toString().contains("a")){ System.out.println(-1); } else{ int a = 0; for (int i = 0; i<str1.length(); i++){ if (str1.charAt(i) == 'a'){ a++; } } long ans = 1; for (int i = 0; i<a; i++){ ans = ans*2; } 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 11
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
dbaee5de059b011828b80af29fac58c6
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 Main { static long mod = (int)1e9+7; // static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { char arr[] = sc.next().toCharArray(); char brr[] = sc.next().toCharArray(); Arrays.sort(brr); Arrays.sort(arr); if(brr.length == 1) { if(brr[0] == 'a') { System.out.println(1); } else { int count = 0; long ans = 0; for(char x:arr) { if(x != 'a') { break; } else { count++; } } System.out.println((long)Math.pow(2 , count)); } } else { if(brr[0] == 'a') { System.out.println(-1); } else { int count = 0; long ans = 0; for(char x:arr) { if(x != 'a') { break; } else { count++; } } System.out.println((long)Math.pow(2 , count)); } } } } 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()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static void my_sort(long[] arr) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static int LowerBound(int a[], int 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 int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } } class Pair { int first; int second; Pair(int first , int second) { this.first = first; this.second = second; } }
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 11
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
1d94fe5be74cf9e0484659dcabf8876b
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.StringTokenizer; import java.util.*; public class Main { 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 s = new FastReader(); int t = s.nextInt(); while (t-- > 0){ String a = s.next(); String b = s.next(); Set<Character> set = new HashSet<>(); for (int i = 0; i < b.length(); i++) { set.add(b.charAt(i)); } if (b.equals("a")) { System.out.println("1"); continue; } if (b.contains("a")) { System.out.println("-1"); } else { long ans = (long)Math.pow(2, (long)(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 11
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
2c0418802fe68c98a90d271470c4ab4e
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.lang.*; import java.io.*; import java.util.*; public class codeforces { static int max = Integer.MAX_VALUE, min = Integer.MIN_VALUE; long maxl = Long.MAX_VALUE, minl = Long.MIN_VALUE; static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static int parent[] = new int[100001]; static int mod = 1000000007; static boolean b1 = true; public static void main(String[] args) throws IOException { // if (System.getProperty("ONLINE_JUDGE") == null) { // PrintStream ps = new PrintStream(new File("output.txt")); // InputStream is = new FileInputStream("input.txt"); // System.setIn(is); // System.setOut(ps); FastScanner sc = new FastScanner(); int ttt = sc.nextInt(); for (int tt = 1; tt <= ttt; tt++) { String s = sc.next(); String t = sc.next(); if (t.equals("a")) System.out.println("1"); else if (t.length() == 1) System.out.println((long) Math.pow(2, s.length())); else { b1 = true; for (int i = 0; i < t.length(); i++) if (t.charAt(i) == 'a') { System.out.println("-1"); b1 = false; break; } if (b1) { int n = s.length(); System.out.println((long) Math.pow(2, n)); } } // } } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } public static boolean checkPalindrome(int n) { int v = 0; int n1 = n; while (n > 0) { v = v * 10 + n % 10; n = n / 10; } if (n1 == v) return true; else return false; } public static int lis(int arr[], int n) { int lis[] = new int[n]; int i, j, max = 0; /* Initialize LIS values for all indexes */ for (i = 0; i < n; i++) lis[i] = 1; /* * Compute optimized LIS values in * bottom up manner */ for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (arr[i] > arr[j] && lis[i] < lis[j] + 1) lis[i] = lis[j] + 1; /* Pick maximum of all LIS values */ for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } public static void union(int a, int b) { parent[a] += parent[b]; parent[b] = a; } public static int find(int a) { if (parent[a] < 0) return a; return (parent[a] = find(parent[a])); } public static Boolean[] primeNo() { Boolean[] is_prime = new Boolean[100001]; int max = 100000; for (int i = 2; i <= max; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2; (long) i * i <= max; i++) { if (is_prime[i] == true) { for (int j = (i * i); j <= max; j += i) is_prime[j] = false; } } return is_prime; } public static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; int result1 = -1; while (l <= r) { int mid = l + (r - l) / 2; if (arr[mid] == x) { return mid; } else if (arr[mid] > x) { r = mid - 1; } else { l = mid + 1; } } return result1; } public static class Pair { public int x; public int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["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 11
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
99cd94a4e0ac0eba939b7bc2757d8d42
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 three { public static int fac(int i) { if(i==0 || i==1) { return i; }else { return i*fac(i-1); } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner key=new Scanner(System.in); int q=key.nextInt(); key.nextLine(); for(int i=0;i<q;i++) { String s=key.nextLine(); String s1=key.nextLine(); if(s1.equals("a")) { System.out.println("1"); }else if(s1.contains("a")) { System.out.println("-1"); }else { int count=0; for(int y=0;y<s.length();y++) { if(s.charAt(y)=='a') { count++; } } System.out.println((long)Math.pow(2, 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 11
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
84aceda68504a88281a19d0e4d35c2d8
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 Answer { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int q = sc.nextInt(); outer: while(q-->0) { String s = sc.next(); String t = sc.next(); if(t.equals("a")) { pw.println(1); continue outer; } if(t.contains("a")) { pw.println(-1); continue outer; } pw.println((long)Math.pow(2, s.length())); } sc.close(); 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 11
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
c54f3bf4aaa292aec50d8509c62cbbbc
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.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.security.spec.EncodedKeySpec; import java.util.concurrent.ThreadLocalRandom; import javax.lang.model.util.ElementScanner6; import javax.swing.plaf.multi.MultiButtonUI; public class codeforces { 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; } } public static void toString(int[] j) { System.out.println(Arrays.toString(j)); } public static void main(String[] args) throws IOException { FastReader scan = new FastReader(); int j = scan.nextInt(); while(j>0) { String bruh = scan.nextLine(); String replace = scan.nextLine(); if(replace.equals("a")) { System.out.println(1); } else if(replace.length() - replace.replace("a", "").length()>0) { System.out.println(-1); } else{ int numOfoccur = bruh.length() - bruh.replace(replace, "").length(); int stringLength = bruh.length(); System.out.println((long)Math.pow((long)2, (long)stringLength-numOfoccur)); } j--; } } } /*int[] bruh = new int[j*2]; int i = 0; while(j>0) { line = br.readLine(); values = line.split(" "); joes.put(Integer.parseInt(values[0]),Integer.parseInt(values[2])); joes.put(Integer.parseInt(values[1]),-Integer.parseInt(values[2])); bruh[i] = Integer.parseInt(values[0]); i++; bruh[i] = Integer.parseInt(values[1]); i++; j--; } Arrays.sort(bruh); int sum = 0; int max = 0; for(int k = 0; k<bruh.length; k++) { sum+=joes.get(bruh[k]); max = Math.max(sum, max); } System.out.println(max); */ /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); Map<Integer, Integer> joes = new HashMap<>(); String[] values = line.split(" "); int j = Integer.parseInt(values[0]); int m = 3; int[] bruh = new int[j]; line = br.readLine(); values= line.split(" "); int[] finale = new int[j]; for(int i = 0; i<j; i++) { bruh[i] = Integer.parseInt(values[i]); } line = br.readLine(); values= line.split(" "); for(int k = 0; k<j; k++) { finale[k] = Integer.parseInt(values[k]); } while(m>0) { int[] man = new int[j]; for(int z = 0; z<bruh.length; z++) { man[z] = finale[bruh[z]-1]; } finale = man; m--; } for(int y = 0; y<finale.length; y++) { System.out.println(finale[y]); } */
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 11
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
0bd36f80d1bbab707ce39d76941d690c
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 Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count = scanner.nextInt(); while (count > 0) { String s = scanner.next(); String t = scanner.next(); if (t.equals("a")) { System.out.println(1); } else if (t.contains("a")) { System.out.println(-1); } else { long countA = s.chars().filter(ch -> ch == 'a').count(); System.out.println((long) (Math.pow(2, countA))); } 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 11
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
cfca4a79e8445adc5570e077431909b3
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 in = new Scanner(System.in); int z = in.nextInt(); for ( int p = 0; p < z; p++) { String s = in.next(); String t = in.next(); int test = 0; if (t.equals("a")) { System.out.println(1); } else { for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == 'a') { System.out.println(-1); test = -1; break; } } if (test == 0) { 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 11
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
6e45c86e1c75d72d13d5582e2ea2ece0
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.Scanner; import java.util.StringTokenizer; public class infiniteReplacement { public static void main(String[] args) throws IOException { Reader r = new Reader(); int t = r.nextInt(); String s, x; long[] answers = new long[t]; for (int i = 0; i < t; i++) { s = r.next(); x = r.next(); answers[i] = solver(s, x); } for (long i : answers) { System.out.println(i); } } public static long solver(String s, String t) { if (t.indexOf('a') != -1 && t.length() > 1) { return -1; } if (t.equals("a")) { return 1; } return (long) Math.pow(2, s.length()); } static class Reader { String filename; Scanner sc; StringTokenizer st; PrintWriter out; boolean fileOut = false; public Reader() { sc = new Scanner(System.in); // stdin to test before submitting } public Reader(String name) throws IOException { filename = name; sc = new Scanner(new File(filename + ".in")); // adding filename reads from file instead of keyboard out = new PrintWriter(new BufferedWriter(new FileWriter(filename + ".out"))); fileOut = true; } public String next() throws IOException { return sc.nextLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public void println(Object text) { if (fileOut) { out.println(text); } else System.out.println(text); } public void close() { if (fileOut) 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 11
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
2f2a2e11817b6f4a22f87c22ae821a3b
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.Arrays; import java.util.StringTokenizer; import static java.lang.Math.pow; public class haha { static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String nextString() throws IOException { return br.readLine(); } 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()); } float nextFloat() { return Float.parseFloat(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()); } } static int minde(int a, int b, int c) { if(a < b) { if (c < a) return b - c; else if ( c < b) return b - a; else return c - a; } else { if(c < b) return a - c; else if(c < a) return a - b; else return c - b; } } public static void main(String []args) throws IOException { PrintWriter out = new PrintWriter(System.out,true); FastScanner sc = new FastScanner(); int n = sc.nextInt(); for(int i = 0; i < n ; i++) { String s = sc.nextString(); String t = sc.nextString(); int ju1 = 0 , ju2 = 0; for (int j = 0 ; j < t.length() ; j++) { if(t.charAt(j) == 'a') { ju1 = 1; } else { ju2 ++; } } if(ju1 == 1) { if(t.length() > 1) out.println(-1); else out.println(1); } else { out.println((long)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 11
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
c859abbd617673e35d3d0b89e92f154f
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.*; public class Solution implements Runnable { public void solve() throws Exception { int testCase = sc.nextInt(); while (testCase-- > 0) { String a = in.readLine(); String b = in.readLine(); HashMap <Character, Integer> hash = new HashMap<>(); for (char c: b.toCharArray()) { if (c == 'a') { hash.put(c, hash.getOrDefault(c, 0) +1); } } if (b.length() == hash.size()) { out.println(1); } else { if (hash.size() >= 1) { out.println(-1); } else { Long ans = 1L; for (char c : a.toCharArray()) { ans *= 2; } out.println(ans); } } } } 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 11
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
14fc23cd8d97b8c21484e432f77dd148
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 javax.management.openmbean.OpenDataException; public class Codeforces { final static int mod = 1000000007; final static String yes = "YES"; final static String no = "NO"; public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); outer: while (t-- > 0) { String s = sc.next(); String x = sc.next(); if (x.equals("a")) { System.out.println(1); continue outer; } if (x.contains("a")) { System.out.println(-1); continue outer; } System.out.println((long) Math.pow(2, s.length())); } } static void sortLong(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 sortInt(int[] a) // check for int { 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 boolean isSorted(int[] nums, int n) { for (int i = 1; i < n; i++) { if (nums[i] < nums[i - 1]) return false; } return true; } public static boolean isPalindrome(String s) { StringBuilder sb = new StringBuilder(s); return s.equals(sb.reverse().toString()); } static long kadane(long A[]) { long lsum = A[0], gsum = 0; gsum = Math.max(gsum, lsum); for (int i = 1; i < A.length; i++) { lsum = Math.max(lsum + A[i], A[i]); gsum = Math.max(gsum, lsum); } return gsum; } public static void sortByColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); } public static void backtrack(String[] letters, int index, String digits, StringBuilder build, List<String> result) { if (build.length() >= digits.length()) { result.add(build.toString()); return; } char[] key = letters[digits.charAt(index) - '2'].toCharArray(); for (int j = 0; j < key.length; j++) { build.append(key[j]); backtrack(letters, index + 1, digits, build, result); build.deleteCharAt(build.length() - 1); } } public static String get(String s, int k) { int n = s.length(); int rep = k % n == 0 ? k / n : k / n + 1; s = s.repeat(rep); return s.substring(0, k); } public static int diglen(Long y) { int a = 0; while (y != 0L) { y /= 10; a++; } return a; } static class FastReader { BufferedReader br; StringTokenizer st; // StringTokenizer() is used to read long strings 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 class Pair implements Comparable<Pair> { public final int index; public final int value; public Pair(int index, int value) { this.index = index; this.value = value; } @Override public int compareTo(Pair other) { // multiplied to -1 as the author need descending sort order return -1 * Integer.valueOf(this.value).compareTo(other.value); } } static String reverseString(String str) { StringBuilder input = new StringBuilder(); return input.append(str).reverse().toString(); } static void printArray(int[] nums) { for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + "->"); } System.out.println(); } static void printLongArray(long[] nums) { for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + "->"); } System.out.println(); } static long factorial(int n, int b) { if (n == b) return 1; return n * factorial(n - 1, b); } static int lcm(int ch, int b) { return ch * b / gcd(ch, b); } static int gcd(int ch, int b) { return b == 0 ? ch : gcd(b, ch % b); } static double ceil(double n, double k) { return Math.ceil(n / k); } static int sqrt(double n) { return (int) Math.sqrt(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 11
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
2e04eb70cc87050bb2873be18425f739
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 Solution { public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { char[] s = sc.next().toCharArray(); char[] word = sc.next().toCharArray(); int count = 0; for (char ch : word) { if (ch == 'a') count++; } if (word.length == 1 && word[0] == 'a') { pw.println(1); continue; } if (word.length > 1 && count > 0) { pw.println(-1); continue; } // long ans = 1; // for (int i = 0; i < s.length; i++) { // ans += combination(s.length, i); // } // pw.println(ans); pw.println((1l*1<<s.length)); } pw.close(); } static long getDiff(pair start, pair end) { return Math.abs(start.x - end.x) + Math.abs(start.y - end.y); } static HashMap Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static HashMap Hash(char[] arr) { HashMap<Character, Integer> map = new HashMap<>(); for (char i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return n * factorial(n - 1); } static boolean isPalindrome(char[] str, int i, int j) { // While there are characters to compare while (i < j) { // If there is a mismatch if (str[i] != str[j]) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static double log2(int N) { double result = (Math.log(N) / Math.log(2)); return result; } public static double log4(int N) { double result = (Math.log(N) / Math.log(4)); return result; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static int clearBit(int mask, int idx) { return mask ^ (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } 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 static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(long[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(Integer[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(String[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } 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 11
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
dbe5b5d5b2e1dc7df5ed1e6bcc8fd411
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 Exception { int q = sc.nextInt(); while (q-- > 0) { String s = sc.nextLine(); String t = sc.nextLine(); if (t.equals("a")) { pw.println(1); continue; } if (t.contains("a")) { pw.println(-1); continue; } pw.println(1l * 1 << s.length()); } pw.close(); } public static int fac(int n) { if (n < 0) return 1; if (n == 1 || n == 0) return 1; return n * fac(n - 1); } static class pairr { String s; int len; public pairr(String s, int len) { this.s = s; this.len = len; } } public static void swap(pairr[] arr, int start, int end) { int tmp = arr[start].len; arr[start].len = arr[end].len; arr[end].len = tmp; String tmpp = arr[start].s; arr[start].s = arr[end].s; arr[end].s = tmpp; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["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 11
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
8317f3811919a88398fe5d2f557a051a
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.Arrays; import java.util.Scanner; public class InfiniteReplacement { public static void main(String[] args) { Scanner input =new Scanner (System.in); int t=input.nextInt(); for(int i=0;i<t;i++) { String s1=input.next(); String s2=input.next(); if (s2.length()==1) { if (s2.charAt(0)=='a') { System.out.println(1); } else System.out.println((long)Math.pow(2, s1.length())); } else { char[]array= s2.toCharArray(); Arrays.sort(array); if (array[0]=='a') { System.out.println(-1); } else System.out.println((long)Math.pow(2, s1.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 11
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
45b723caf25d14ab279d5e9af5412b88
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 d3 { //static boolean[] visited; //static long min; public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while(t-->0){ st = new StringTokenizer(br.readLine()); String s1 = st.nextToken(); st = new StringTokenizer(br.readLine()); String s2 = st.nextToken(); if(s2.equals("a")){ bw.write("1\n"); } else if(s2.contains("a") && !s2.equals("a")){ bw.write("-1\n"); } else{ long ans = (long)Math.pow(2,s1.length()); bw.write(ans+"\n"); } } bw.flush(); } public static boolean palin1(String s){ int n = s.length()-1; int mid = (n+1)/2; for(int i = 0;i<mid;i++){ if(s.charAt(i) != s.charAt(n-i)){ return false; } } return true; } public static long recur(int n,long[] ans){ return ans[n-1] + ans[n/2] +ans[n/3]; } static class pair{ int a; int b; pair(int a,int b){ this.a = a; this.b = b; } } public static boolean check1(ArrayList<ArrayList<Integer>> graph,boolean[] visited){ for(int i = 0;i<visited.length;i++){ if(!visited[i] && graph.get(i).size() != 0){ if(dfs(i, graph, visited)){ return true; } } print1(visited); } return false; } public static void print1(boolean[] visited){ for(int i = 0;i<visited.length;i++){ System.out.print(visited[i]+" "); } System.out.println(); } public static boolean dfs(int i ,ArrayList<ArrayList<Integer>> g,boolean[] visited){ visited[i] = true; for(int e : g.get(i)){ if(!visited[e]){ // if(dfs(e,g,visited) == true){ // System.out.println("ji"); // return true; // } // dfs(i, g, visited); } else{ return true; } } return false; } public static boolean checksum(int n){ int temp = 0; while(n > 0){ temp += n%10; n /= 10; } return (temp == 10)?true:false; } public static class Node{ int val; int freq; Node(int val,int freq){ this.val = val; this.freq = freq; } } /* public static void dijk(ArrayList<ArrayList<pair>> graph,int i,boolean[] visited){ int[] dist = new int[visited.length]; for(int j = 0;j<dist.length;i++){ dist[j] = Integer.MAX_VALUE; } dist[i] = 0; PriorityQueue<pair> queue = new PriorityQueue<>((pair a1,pair a2)->(a1.dist - a2.dist)); queue.add(new pair(i, 0)); while(!queue.isEmpty()){ pair p1 = queue.poll(); int current = p1.b; if(visited[current]){ continue; } visited[current] = true; for(pair p : graph.get(current)){ int childnode = p.b; int childdist = p.dist; if(!visited[childnode] && dist[childnode] > dist[current]+childdist){ dist[childnode] = dist[current]+childdist; queue.add(p); } } } for(int j = 0;i<visited.length;j++){ System.out.println(dist[j]+" "); } }*/ public static int ceil(ArrayList<Long> al,long val){ int low = 0; int high = al.size()-1; int pos = 0; while(low <= high){ int mid = (low+high)/2; if(al.get(mid) == val){ pos = Math.max(pos,mid); low = mid+1; } else if(al.get(mid) < val){ low = mid+1; } else{ high = mid-1; } } return pos; } public static int floor(ArrayList<Long> al,long val){ int low = 0; int high = al.size()-1; int pos = high; while(low <= high){ int mid = (low+high)/2; if(al.get(mid) == val){ pos = Math.min(pos,mid); high = mid-1; } else if(al.get(mid) < val){ low = mid+1; } else{ high = mid-1; } } return pos; } public static boolean palin(String s){ for(int i = 0;i<s.length()/2;i++){ if(s.charAt(i) == s.charAt(s.length()-i-1)){ continue; } else{ return false; } } return true; } public static long gcd(long a,long b){ if(b == 0L){ return a; } return gcd(b,a%b); } public static boolean check(ArrayList<Long> al,long mid,long m){ long cur = 0L; for(int i = 0;i<al.size();i++){ if(al.get(i) <= mid){ cur++; } else{ cur += (al.get(i)/mid); cur += (al.get(i)%mid != 0)?1L:0L; } } return (cur <= m)?true:false; } // public static int bs(int sum,int low,int high){ // } public static long fin(long n){ return ((long)n)*(n+1)/2; } public static int bceil(ArrayList<Integer> al,int val,int low,int high){ int index = 0; while(low <= high){ int mid = low + (high-low)/2; if(al.get(mid) <= val){ index = Math.max(mid,index); low = mid+1; } else{ high = mid-1; } } return index; } public static int bfloor(ArrayList<Integer> al,int val){ int low = 0; int high = al.size()-1; int index = -1; while(low <= high){ int mid = low + (high-low)/2; int mval = al.get(mid); if(mval >= val){ high = mid-1; index = Math.max(index,mid); } else{ low = mid+1; } } return index; } public static String rev(String s){ StringBuffer sb = new StringBuffer(s); sb.reverse(); return s+sb.toString(); } public static String rev1(String s){ StringBuffer sb = new StringBuffer(s); sb.reverse(); return sb.toString()+s; } public static long fact12(long a){ long l = 1L; for(long i = 1;i<=a;i++){ l *= i; } // System.out.println(l); return l; } public static boolean binary(ArrayList<Integer> al,int a1){ int low = 0; int high = al.size(); while(low <= high){ int mid = (low+high)/2; if(a1 == al.get(mid)){ return true; } else if(a1 > al.get(mid)){ low = mid+1; } else{ high = mid-1; } } return false; } public static void primefact(int n){ for(int i = 2;i*i<=n;i++){ if(n%i == 0){ while(n%i == 0){ n /= i; System.out.println(i); } } } System.out.println(n); } public static boolean prime(long n){ int count = 0; for(long i = 1;i*i <= n;i++){ if(n%i == 0){ if(i*i == n){ count++; } else{ count += 2; } } } if(count == 2){ return true; } return false; } public static boolean bsearch(ArrayList<Integer> al,int low,int high,int req){ if(low > high || low < 0 || high < 0 || low >= al.size() || high >= al.size()){ return false; } int mid = (low+high)/2; if(al.get(mid) == req){ return true; } else if(al.get(mid) > req){ high = mid-1; bsearch(al, low, high, req); } else{ low = mid+1; bsearch(al, low, high, req); } return false; } public static long fact1(int n){ long ans = 1L; for(int i = 2;i<=n;i++){ ans *= i; } return ans; } public static String reverse(String s){ StringBuilder sb = new StringBuilder(s); return sb.reverse().toString(); } public static int find(int n){ return n*(n+1)/2; } public static double calc(int x1,int x2,int y1,int y2){ return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); } }
Java
["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 11
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
a5e870b87e8567ff6dd1aa241670fc6a
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[] strgs) { Scanner sc=new Scanner(System.in); long[] power=new long[51]; power[1]=2; for(int i=2;i<=50;i++) { power[i]=power[i-1]*2; } int t=sc.nextInt(); while(t-- >0) { String sr=sc.next(); String tr=sc.next(); if(tr.contains("a") && tr.length()==1) { System.out.println(1); } else if(tr.contains("a")) { System.out.println(-1); } else { System.out.println(power[sr.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 11
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
b2e176f17e206d20fbd396db9460aea8
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.*;import java.util.*; //* --> number of prime numbers less then or equal to x are --> x/ln(x) //* --> String concatenation using the + operator within a loop should be avoided. Since the String object is immutable, each call for concatenation will // result in a new String object being created. // THE SIEVE USED HERE WILL RETURN A LIST CONTAINING ALL THE PRIME NUMBERS TILL N public class codechef {static FastScanner sc;static PrintWriter pw;static class FastScanner {InputStreamReader is;BufferedReader br;StringTokenizer st; public FastScanner() {is = new InputStreamReader(System.in);br = new BufferedReader(is);} String next() throws Exception {while (st == null || !st.hasMoreElements())st = new StringTokenizer(br.readLine()); return st.nextToken();}int nextInt() throws Exception {return Integer.parseInt(next());}long nextLong() throws Exception { return Long.parseLong(next());}int[] readArray(int num) throws Exception {int arr[]=new int[num]; for(int i=0;i<num;i++)arr[i]=nextInt();return arr;}String nextLine() throws Exception {return br.readLine(); }} public static boolean power_of_two(int a){if((a&(a-1))==0){ return true;}return false;} static boolean PS(double x){if (x >= 0) {double i= Math.sqrt(x);if(i%1!=0){ return false;}return ((i * i) == x);}return false;}public static int[] ia(int n){int ar[]=new int[n]; return ar;}public static long[] la(int n){long ar[]=new long[n];return ar;} public static void print(int ans,int t){System.out.println("Case"+" "+"#"+t+":"+" "+ans);} static long mod=1000000007;static int max=Integer.MIN_VALUE;static int min=Integer.MAX_VALUE; public static void sort(long[] arr){//because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Long> ls = new ArrayList<Long>();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 long fciel(long a, long b) {if (a == 0) return 0;return (a - 1) / b + 1;} static boolean[] is_prime = new boolean[1000001];static ArrayList<Integer> list = new ArrayList<>(); static long n = 1000000;public static void sieve() {Arrays.fill(is_prime, true); is_prime[0] = is_prime[1] = false;for (int i = 2; i * i <= n; i++) { if (is_prime[i]) {for (int j = i * i; j <= n; j += i)is_prime[j] = false;}}for (int i = 2; i <= n; i++) { if (is_prime[i]) {list.add(i);}}} // ---------- NCR ---------- \ static int NC=100005; static long inv[]=new long[NC]; static long fac_inv[]=new long[NC]; static long fac[]=new long[NC];public static void initialize() { long MOD=mod; int i; inv[1]=1; for(i=2;i<=NC-2;i++) inv[i]=(MOD-MOD/i)*inv[(int)MOD%i]%MOD; fac[0]=fac[1]=1; for(i=2;i<=NC-2;i++) fac[i]=i*fac[i-1]%MOD; fac_inv[0]=fac_inv[1]=1; for(i=2;i<=NC-2;i++) fac_inv[i]=inv[i]*fac_inv[i-1]%MOD; } public static long ncr(int n,int r) { long MOD=mod; if(n<r) return 0; return (fac[n]*fac_inv[r]%MOD)*fac_inv[n-r]%MOD; } // ---------- NCR ---------- \ // ---------- FACTORS -------- \ static int div[][] = new int[1000001][]; public static void factors() { int divCnt[] = new int[1000001]; for(int i = 1000000; i >= 1; --i) { for(int j = i; j <= 1000000; j += i) divCnt[j]++; } for(int i = 1; i <= 1000000; ++i) div[i] = new int[divCnt[i]]; int ptr[] = new int[1000001]; for(int i = 1000000; i >= 1; --i) { for(int j = i; j <= 1000000; j += i) div[j][ptr[j]++] = i; } } // ---------- FACTORS -------- \ // ------------- DSU ---------------\ static int par[]=new int[1000001];static int size[]=new int[1000001]; public static void make(int v){par[v]=v;size[v]++;} public static void union(int a,int b){a=find(a);b=find(b); if(a!=b){if(size[a]<size[b]){int temp=a;a=b;b=temp;}par[b]=a; size[a]++;}}public static int find(int v) {if(v==par[v]){return v;}return par[v]=find(par[v]);} // ------------- DSU ---------------\ public static void main(String args[]) throws java.lang.Exception { sc = new FastScanner();pw = new PrintWriter(System.out);StringBuilder s = new StringBuilder(); int t=sc.nextInt(); while(t-->0) { String s1=sc.next(); String s2=sc.next(); if(s2.length()==1&&s2.charAt(0)=='a') { s.append(1); } else{ int f=0; for(int i=0;i<s2.length();i++) { if(s2.charAt(i)=='a') { f=1; break; } } int x=s1.length(); long ans=(long)Math.pow(2,x); s.append(f==0?(ans):-1); } if(t>0) { s.append("\n"); }} pw.print(s);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 11
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
1b8076b083261e9965541ed1e1e0ad53
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.math.*; public class D { static boolean check(int i, int j, int n, int m,int arr[][]) { if(i<0 || i>=n || j<0 || j>=m) return false; if(arr[i][j]!=1) return false; return true; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { String x=sc.next(); String y=sc.next(); int a=0; for(int i=0;i<y.length();i++) { if(y.charAt(i)=='a') a++; } if(y.length()==1 && a>0) System.out.println(1); else if(y.length()>1 && a>0) System.out.println(-1); else if(a==0) System.out.println((long)Math.pow(2, x.length())); else System.out.println(-1); } 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 11
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
52394345730e5ca24301a686e92e45e1
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.*; import static java.lang.Math.*; import static java.util.Arrays.sort; public class Round12 { public static void main(String[] args) { FastReader fastReader = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fastReader.nextInt(); while (t-- > 0) { char c[] = fastReader.nextLine().toCharArray(); int n = c.length; String rep = fastReader.nextLine(); if (rep.equals("a")) { out.println(1); } else { int len = rep.length(); if (len >= 2 && rep.contains("a")) { out.println(-1); } else { out.println(1L<<n); } } } out.close(); } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } 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()); } int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() { return Long.parseLong(next()); } long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } 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 11
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
1d37368731aec015f1d203bbac93b5f8
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; public class Main { InputStream is; PrintWriter out = new PrintWriter(System.out); ; String INPUT = ""; void run() throws Exception { is = System.in; solve(); out.flush(); out.close(); } public static void main(String[] args) throws Exception { new Main().run(); } public byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; public int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() { // continue; String; charAt; println(); ArrayList; Integer; Long; // long; Queue; Deque; LinkedList; Pair; double; binarySearch; // s.toCharArray; length(); length; getOrDefault; break; // Map.Entry<Integer, Integer> e; HashMap; TreeMap; int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } public double nd() { return Double.parseDouble(ns()); } public char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-') { minus = true; b = readByte(); } while(true) { if(b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } class Pair { int first; int second; Pair(int a, int b) { first = a; second = b; } } // KMP ALGORITHM void KMPSearch(String pat, String txt) { int M = pat.length(); int N = txt.length(); int lps[] = new int[M]; int j = 0; computeLPSArray(pat, M, lps); int i = 0; while (i < N) { if (pat.charAt(j) == txt.charAt(i)) { j++; i++; } if (j == M) { j = lps[j - 1]; } else if (i < N && pat.charAt(j) != txt.charAt(i)) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } } void computeLPSArray(String pat, int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; i++; } } } } int FirstAndLastOccurrenceOfAnElement(int[] arr, int target, boolean findStartIndex) { int ans = -1; int n = arr.length; int low = 0; int high = n-1; while(low <= high) { int mid = low + (high - low)/2; if(arr[mid] > target) high = mid-1; else if(arr[mid] < target) low = mid+1; else { ans = mid; if(findStartIndex) high = mid-1; else low = mid+1; } } return ans; } // Print All Subsequence. static ArrayList<Integer> qwerty = new ArrayList<>(); void printAllSubsequence(int[] arr, int index, int n) { if(index >= n) { for(int i=0; i<qwerty.size(); i++) out.print(qwerty.get(i) + " "); if(qwerty.size() == 0) out.print(" "); out.println(); return; } qwerty.add(arr[index]); printAllSubsequence(arr, index+1, n); qwerty.remove(qwerty.size()-1); printAllSubsequence(arr, index+1, n); } // Print All Subsequence. // Print All Subsequence. with sum k. static ArrayList<Integer> qwerty2 = new ArrayList<>(); void printSubsequenceWithSumK(int[] arr, int index, int K, int sum, int n) { if(index >= n) { if(sum == K) { for(int i=0; i<qwerty2.size(); i++) out.print(qwerty2.get(i) + " "); out.println(); } return; } qwerty2.add(arr[index]); sum += arr[index]; printSubsequenceWithSumK(arr, index+1, K, sum, n); sum -= arr[index]; qwerty2.remove(qwerty2.size()-1); printSubsequenceWithSumK(arr, index+1, K, sum, n); } // Print All Subsequence. with sum k. int[] na(int n) { int[] arr = new int[n]; for(int i=0; i<n; i++) arr[i]=ni(); return arr; } long[] nal(int n) { long[] arr = new long[n]; for(int i=0; i<n; i++) arr[i]=nl(); return arr; } void solve() { int test_case = ni(); while(test_case-- > 0) { String s1 = ns(); String s2 = ns(); if(s2.length() == 1 && s2.charAt(0) == 'a'){ out.println("1"); continue; } boolean f = false; for(int i = 0; i<s2.length(); i++){ if(s2.charAt(i) == 'a') { f = true; break; } } if(f) { out.println(-1); } else { out.println((long) Math.pow(2, s1.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 11
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
cf9ac78bc332d9b25341ede6ee959b23
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 = (int)1e9+7; static boolean[] prime = new boolean[10]; static int[][] dir1 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; static int[][] dir2 = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; static { for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i < prime.length; i++) { if (prime[i]) { for (int k = 2; i * k < prime.length; k++) { prime[i * k] = false; } } } } static class JoinSet { int[] fa; JoinSet(int n) { fa = new int[n]; for (int i = 0; i < n; i++) fa[i] = i; } int find(int t) { if (t != fa[t]) fa[t] = find(fa[t]); return fa[t]; } void join(int x, int y) { x = find(x); y = find(y); fa[x] = y; } } static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static int get() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Integer.parseInt(ss); } static long getx() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Long.parseLong(ss); } static int[] getint() throws Exception { String[] s = bf.readLine().split(" "); int[] a = new int[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(s[i]); } return a; } static long[] getlong() throws Exception { String[] s = bf.readLine().split(" "); long[] a = new long[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(s[i]); } return a; } static String getstr() throws Exception { return bf.readLine(); } static void println() throws Exception { bw.write("\n"); } static void print(int a) throws Exception { bw.write(a + "\n"); } static void print(long a) throws Exception { bw.write(a + "\n"); } static void print(String a) throws Exception { bw.write(a + "\n"); } static void print(int[] a) throws Exception { for (int i : a) { bw.write(i + " "); } println(); } static void print(long[] a) throws Exception { for (long i : a) { bw.write(i + " "); } println(); } static void print(int[][] a) throws Exception { for (int i[] : a) print(i); } static void print(long[][] a) throws Exception { for (long[] i : a) print(i); } static void print(char[] a) throws Exception { for (char i : a) { bw.write(i + ""); } println(); } static long pow(long a, long b) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans *= a; } a *= a; b >>= 1; } return ans; } static int powmod(long a, long b, int mod) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return (int)ans; } static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void resort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n - 1 - i]; } static void resort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n - 1 - i]; } static int max(int a, int b) { return Math.max(a, b); } static int min(int a, int b) { return Math.min(a, b); } static long max(long a, long b) { return Math.max(a, b); } static long min(long a, long b) { return Math.min(a, b); } static int max(int[] a) { int max = a[0]; for (int i : a) max = max(max, i); return max; } static int min(int[] a) { int min = a[0]; for (int i : a) min = min(min, i); return min; } static long max(long[] a) { long max = a[0]; for (long i : a) max = max(max, i); return max; } static long min(long[] a) { long min = a[0]; for (long i : a) min = min(min, i); return min; } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } static void yes() throws Exception { print("Yes"); } static void no() throws Exception { print("No"); } static int[] getarr(List<Integer> list) { int n = list.size(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = list.get(i); return a; } public static void main(String[] args) throws Exception { int t = get(); while (t-- > 0){ String s = getstr(); String p = getstr(); int m = s.length(), n = p.length(); Set<Integer> set = new HashSet<>(); for(char c : p.toCharArray()) set.add(c-'a'); if(set.contains(0)) print(n==1 ? 1 : -1); else print(pow(2,m)); } bw.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 11
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
94b34e5a1fb52dea1b365314f5e9192f
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.*; /* 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 Scanner sc = new Scanner(System.in); int ti = sc.nextInt(); while(ti-->0){ String s = sc.next(); String t = sc.next(); int flag = 0; for(int i = 0; i< t.length();i++){ if(t.charAt(i)=='a'){ flag=1; break; } } if(flag==1){ if(t.length()==1){ System.out.println(1); } else{ 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 11
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
8e5dccdb0b0b142cf43c1b2113136bd0
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.text.DecimalFormat; import java.util.*; public class Main { static class Pair { long a,b,c; public Pair(long a,long b,long c) { this.a=a; this.b=b; this.c=c; } // @Override // public int compareTo(Pair p) { // return Long.compare(l, p.l); // } } static final int INF = 1 << 30; static final long INFL = 1L << 60; static final long NINF = INFL * -1; static final long mod = (long) 1e9 + 7; static final long mod2 = 998244353; static DecimalFormat df = new DecimalFormat("0.00000000000000"); public static final double PI = 3.141592653589793d, eps = 1e-9; public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public static void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.ni(); //int t=1; while (t-->0) { String s=in.ns(); String tt=in.ns(); int n=s.length(); if(tt.equals("a")) { out.printLine(1); } else if(tt.indexOf('a')!=-1) { out.printLine(-1); } else { long ans=(1L<<n); out.printLine(ans); } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private char nc() { return (char)skip(); } public int[] na(int arraySize) { int[] array = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = ni(); } return array; } private int skip() { int b; while ((b = read()) != -1 && isSpaceChar(b)) ; return b; } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = read(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } int[][] nim(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = na(w); } return a; } long[][] nlm(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nla(w); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isNewLine(int c) { return c == '\n'; } public String nextLine() { int c = read(); StringBuilder result = new StringBuilder(); do { result.appendCodePoint(c); c = read(); } while (!isNewLine(c)); return result.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sign = 1; if (c == '-') { sign = -1; c = read(); } long result = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } result *= 10; result += c & 15; c = read(); } while (!isSpaceChar(c)); return result * sign; } public long[] nla(int arraySize) { long array[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextLong(); } return array; } public double nextDouble() { double ret = 0, div = 1; byte c = (byte) read(); while (c <= ' ') { c = (byte) read(); } boolean neg = (c == '-'); if (neg) { c = (byte) read(); } do { ret = ret * 10 + c - '0'; } while ((c = (byte) read()) >= '0' && c <= '9'); if (c == '.') { while ((c = (byte) read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } boolean oj = System.getProperty("ONLINE_JUDGE") != null; void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } } public static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; (long) i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } public static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } public static long pow(long a, long n, long mod) { // a %= mod; long ret = 1; int x = 63 - Long.numberOfLeadingZeros(n); for (; x >= 0; x--) { ret = ret * ret % mod; if (n << 63 - x < 0) ret = ret * a % mod; } return ret; } public static boolean isComposite(long n) { if (n < 2) return true; if (n == 2 || n == 3) return false; if (n % 2 == 0 || n % 3 == 0) return true; for (long i = 6L; i * i <= n; i += 6) if (n % (i - 1) == 0 || n % (i + 1) == 0) return true; return false; } static int ifnotPrime(int[] prime, int x) { return (prime[x / 64] & (1 << ((x >> 1) & 31))); } static int log2(long n) { return (int) (Math.log10(n) / Math.log10(2)); } static void makeComposite(int[] prime, int x) { prime[x / 64] |= (1 << ((x >> 1) & 31)); } public static String swap(String a, int i, int j) { char temp; char[] charArray = a.toCharArray(); temp = charArray[i]; charArray[i] = charArray[j]; charArray[j] = temp; return String.valueOf(charArray); } static void reverse(long arr[]){ int l = 0, r = arr.length-1; while(l<r){ long temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } static void reverse(int arr[]){ int l = 0, r = arr.length-1; while(l<r){ int temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } public static int[] sieveEratosthenes(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } static long digit(long s) { long brute = 0; while (s > 0) { brute+=s%10; s /= 10; } return brute; } public static int[] primefacts(int n, int[] primes) { int[] ret = new int[15]; int rp = 0; for (int p : primes) { if (p * p > n) break; int i; for (i = 0; n % p == 0; n /= p, i++) ; if (i > 0) ret[rp++] = p; } if (n != 1) ret[rp++] = n; return Arrays.copyOf(ret, rp); } static ArrayList<Integer> bitWiseSieve(int n) { ArrayList<Integer> al = new ArrayList<>(); int prime[] = new int[n / 64 + 1]; for (int i = 3; i * i <= n; i += 2) { if (ifnotPrime(prime, i) == 0) for (int j = i * i, k = i << 1; j < n; j += k) makeComposite(prime, j); } al.add(2); for (int i = 3; i <= n; i += 2) if (ifnotPrime(prime, i) == 0) al.add(i); return al; } public static long[] sort(long arr[]) { List<Long> list = new ArrayList<>(); for (long n : arr) { list.add(n); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static long[] revsort(long[] arr) { List<Long> list = new ArrayList<>(); for (long n : arr) { list.add(n); } Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static int[] revsort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int n : arr) { list.add(n); } Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static ArrayList<Integer> reverse( ArrayList<Integer> data, int left, int right) { // Reverse the sub-array while (left < right) { int temp = data.get(left); data.set(left++, data.get(right)); data.set(right--, temp); } // Return the updated array return data; } static ArrayList<Integer> sieve(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return pr; } static boolean[] sieve1(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } //for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return prime; } static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) { ArrayList<Integer> al = new ArrayList<>(); if (l == 1) ++l; int max = r - l + 1; int arr[] = new int[max]; for (int p : primes) { if (p * p <= r) { int i = (l / p) * p; if (i < l) i += p; for (; i <= r; i += p) { if (i != p) { arr[i - l] = 1; } } } } for (int i = 0; i < max; ++i) { if (arr[i] == 0) { al.add(l + i); } } return al; } static boolean isfPrime(long n, int iteration) { if (n == 0 || n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; Random rand = new Random(); for (int i = 0; i < iteration; i++) { long r = Math.abs(rand.nextLong()); long a = r % (n - 1) + 1; if (modPow(a, n - 1, n) != 1) return false; } return true; } static long modPow(long a, long b, long c) { long res = 1; for (int i = 0; i < b; i++) { res *= a; res %= c; } return res % c; } private static long binPower(long a, long l, long mod) { long res = 0; while (l > 0) { if ((l & 1) == 1) { res = mulmod(res, a, mod); l >>= 1; } a = mulmod(a, a, mod); } return res; } private static long mulmod(long a, long b, long c) { long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2L) % c; b /= 2; } return x % c; } static long binary_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static void swap(int a, int b) { int tp = b; b = a; a = tp; } static long Modular_Expo(long a, long b, long mod) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % mod; --b; } a = (a * a) % mod; b /= 2; } return res % mod; } static int i_gcd(int a, int b) { while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long ceil_div(long a, long b) { return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } static long getIthBitFromLong(long bits, int i) { return (bits >> (i - 1)) & 1; } // static boolean isPerfectSquare(long a, long b) // { // long sq=Math.sqrt() // } private static TreeMap<Long, Long> primeFactorize(long n) { TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder()); long cnt = 0; long total = 1; for (long i = 2; (long) i * i <= n; ++i) { if (n % i == 0) { cnt = 0; while (n % i == 0) { ++cnt; n /= i; } pf.put(cnt, i); //total*=(cnt+1); } } if (n > 1) { pf.put(1L, n); //total*=2; } return pf; } //less than or equal private static int lower_bound(ArrayList<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid]>=val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== LIS & LNDS ================================ private static int[] LIS(long arr[], int n) { List<Long> list = new ArrayList<>(); int[] cnt=new int[n]; for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); cnt[i]=list.size(); } return cnt; } private static int find1(List<Long> list, long val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid)>=val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } private static long nCr(long n, long r,long mod) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans = (ans%mod*i%mod)%mod; for (long i = 2; i <= n - r; i++) ans /= i; return ans%mod; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } static ArrayList<StringBuilder> permutations(StringBuilder s) { int n=s.length(); ArrayList<StringBuilder> al=new ArrayList<>(); getpermute(s,al,0,n); return al; } // static String longestPalindrome(String s) // { // int st=0,ans=0,len=0; // // // } static void getpermute(StringBuilder s,ArrayList<StringBuilder> al,int l, int r) { if(l==r) { al.add(s); return; } for(int i=l+1;i<r;++i) { char c=s.charAt(i); s.setCharAt(i,s.charAt(l)); s.setCharAt(l,c); getpermute(s,al,l+1,r); c=s.charAt(i); s.setCharAt(i,s.charAt(l)); s.setCharAt(l,c); } } static void initStringHash(String s,long[] dp,long[] inv,long p) { long pow=1; inv[0]=1; int n=s.length(); dp[0]=((s.charAt(0)-'a')+1); for(int i=1;i<n;++i) { pow=(pow*p)%mod; dp[i]=(dp[i-1]+((s.charAt(i)-'a')+1)*pow)%mod; inv[i]=CP.modinv(pow,mod)%mod; } } static long getStringHash(long[] dp,long[] inv,int l,int r) { long ans=dp[r]; if(l-1>=0) { ans-=dp[l-1]; } ans=(ans*inv[l])%mod; return ans; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } static boolean isSquarefactor(int x, int factor, int target) { int s = (int) Math.round(Math.sqrt(x)); return factor * s * s == target; } static boolean isSquare(long x) { long s = (long) Math.round(Math.sqrt(x)); return x * x == s; } static int bs(ArrayList<Integer> al, int val) { int l = 0, h = al.size() - 1, mid = 0, ans = -1; while (l <= h) { mid = (l + h) >> 1; if (al.get(mid) == val) { return mid; } else if (al.get(mid) > val) { h = mid - 1; } else { l = mid + 1; } } return ans; } static void sort(int a[]) // heap sort { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); fast_swap(in, idx, i); } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for (int v : a) { c0[(v & 0xff) + 1]++; c1[(v >>> 8 & 0xff) + 1]++; c2[(v >>> 16 & 0xff) + 1]++; c3[(v >>> 24 ^ 0x80) + 1]++; } for (int i = 0; i < 0xff; i++) { c0[i + 1] += c0[i]; c1[i + 1] += c1[i]; c2[i + 1] += c2[i]; c3[i + 1] += c3[i]; } int[] t = new int[n]; for (int v : a) t[c0[v & 0xff]++] = v; for (int v : t) a[c1[v >>> 8 & 0xff]++] = v; for (int v : a) t[c2[v >>> 16 & 0xff]++] = v; for (int v : t) a[c3[v >>> 24 ^ 0x80]++] = v; return a; } static int[] computeLps(String pat) { int len = 0, i = 1, m = pat.length(); int lps[] = new int[m]; 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; } } } return lps; } static ArrayList<Integer> kmp(String s, String pat) { ArrayList<Integer> al = new ArrayList<>(); int n = s.length(), m = pat.length(); int lps[] = computeLps(pat); int i = 0, j = 0; while (i < n) { if (s.charAt(i) == pat.charAt(j)) { i++; j++; if (j == m) { al.add(i - j); j = lps[j - 1]; } } else { if (j != 0) { j = lps[j - 1]; } else { i++; } } } return al; } static void reverse_ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); for (int l = 0, r = a.length - 1; l < r; ++l, --r) fast_swap(a, l, r); } static void ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } static ArrayList<Long> primeFact(long n) { ArrayList<Long> al = new ArrayList<>(); al.add(1L); while (n % 2 == 0) { if (!al.contains(2L)) { al.add(2L); } n /= 2L; } for (long i = 3; (long) i * i <= n; i += 2) { while ((n % i == 0)) { if (!al.contains((long) i)) { al.add((long) i); } n /= i; } } if (n > 2) { if (!al.contains(n)) { al.add(n); } } return al; } public static long totFact(long n) { long cnt = 0, tot = 1; while (n % 2 == 0) { n /= 2; ++cnt; } tot *= (cnt + 1); for (int i = 3; i <= Math.sqrt(n); i += 2) { cnt = 0; while (n % i == 0) { n /= i; ++cnt; } tot *= (cnt + 1); } if (n > 2) { tot *= 2; } return tot; } static int[] z_function(String s) { int n = s.length(), z[] = new int[n]; for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = Math.min(z[i - l], r - i + 1); while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) ++z[i]; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } return z; } static void fast_swap(int[] a, int idx1, int idx2) { if (a[idx1] == a[idx2]) return; a[idx1] ^= a[idx2]; a[idx2] ^= a[idx1]; a[idx1] ^= a[idx2]; } public static void fast_sort(long[] array) { ArrayList<Long> copy = new ArrayList<>(); for (long i : array) copy.add(i); Collections.sort(copy); for (int i = 0; i < array.length; i++) array[i] = copy.get(i); } static int factorsCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } static long binomialCoeff(long n, long k) { long res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res = (res * (n - i)); res /= (i + 1); } return res; } static long nck(long fact[], long inv[], long n, long k) { if (k > n) return 0; long res = fact[(int) n]%mod; res = (int) ((res%mod* inv[(int) k]%mod))%mod; res = (int) ((res%mod*inv[(int) (n - k)]%mod))%mod; return res % mod; } public static long fact(long x) { long fact = 1; for (int i = 2; i <= x; ++i) { fact = fact * i; } return fact; } public static ArrayList<Long> getFact(long x) { ArrayList<Long> facts = new ArrayList<>(); facts.add(1L); for (long i = 2; i * i <= x; ++i) { if (x % i == 0) { facts.add(i); if (i != x / i) { facts.add(x / i); } } } return facts; } static void matrix_ex(long n, long[][] A, long[][] I) { while (n > 0) { if (n % 2 == 0) { Multiply(A, A); n /= 2; } else { Multiply(I, A); n--; } } } static void Multiply(long[][] A, long[][] B) { int n = A.length, m = A[0].length, p = B[0].length; long[][] C = new long[n][p]; for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { for (int k = 0; k < m; k++) { C[i][j] += ((A[i][k] % mod) * (B[k][j] % mod)) % mod; C[i][j] = C[i][j] % mod; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { A[i][j] = C[i][j]; } } } public static HashMap<Long, Integer> sortMapDesc(HashMap<Long,Integer> map) { List<Map.Entry<Long, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); HashMap<Long, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Long, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Character, Integer> sortMapDescch(HashMap<Character, Integer> map) { List<Map.Entry<Character, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); HashMap<Character, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Character, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Long,Long> sortMapAsclng(HashMap<Long,Long> map) { List<Map.Entry<Long,Long>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> (int)(o1.getValue() - o2.getValue())); HashMap<Long,Long> temp = new LinkedHashMap<>(); for (Map.Entry<Long,Long> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Integer, Integer> sortMapAsc(HashMap<Integer, Integer> map) { List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o1.getValue() - o2.getValue()); HashMap<Integer, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Integer, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static long lcm(long l, long l2) { long val = gcd(l, l2); return (l * l2) / val; } public static boolean isSubsequence(String s, String t) { int n = s.length(); int m = t.length(); if (m > n) { return false; } int i = 0, j = 0, skip = 0; while (i < n && j < m) { if (s.charAt(i) == t.charAt(j)) { --skip; ++j; } ++skip; ++i; } return j==m; } public static long[][] combination(int l, int r) { long[][] pascal = new long[l + 1][r + 1]; pascal[0][0] = 1; for (int i = 1; i <= l; ++i) { pascal[i][0] = 1; for (int j = 1; j <= r; ++j) { pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j]; } } return pascal; } public static long gcd(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]); return ret; } public static long lcm(int... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = lcm(ret, array[i]); return ret; } public static int min(int a, int b) { return a < b ? a : b; } public static int min(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]); return ret; } public static long min(long a, long b) { return a < b ? a : b; } public static long min(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]); return ret; } public static int max(int a, int b) { return a > b ? a : b; } public static int max(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]); return ret; } public static long max(long a, long b) { return a > b ? a : b; } public static long max(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]); return ret; } public static long sum(int... array) { long ret = 0; for (int i : array) ret += i; return ret; } public static long sum(long... array) { long ret = 0; for (long i : array) ret += i; return ret; } public static long[] facts(int n) { long[] fact=new long[1005]; fact[0]=1; fact[1]=1; for(int i=2;i<n;i++) { fact[i]=(long)(i*fact[i-1])%mod; } return fact; } public static long[] inv(long[] fact,int n) { long[] inv=new long[n]; inv[n-1]= CP.Modular_Expo(fact[n-1],mod-2,mod)%mod; for(int i=n-2;i>=0;--i) { inv[i]=(inv[i+1]*(i+1))%mod; } return inv; } public static int modinv(long x, long mod) { return (int) (CP.Modular_Expo(x, mod - 2, mod) % mod); } public static int lcs(String s, String t) { int n = s.length(), m = t.length(); int dp[][] = new int[n + 1][m + 1]; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (s.charAt(i - 1) == t.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[n][m]; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void print(long[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(long[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } public void printLine(int i) { writer.println(i); } public void printLine(char c) { writer.println(c); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void py() { print("YES"); writer.println(); } public void pn() { print("NO"); writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } void flush() { writer.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 11
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
b70ba0453798fda877762e4dd7f2713b
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.text.DecimalFormat; import java.util.*; public class Main { static class Pair { long a,b,c; public Pair(long a,long b,long c) { this.a=a; this.b=b; this.c=c; } // @Override // public int compareTo(Pair p) { // return Long.compare(l, p.l); // } } static final int INF = 1 << 30; static final long INFL = 1L << 60; static final long NINF = INFL * -1; static final long mod = (long) 1e9 + 7; static final long mod2 = 998244353; static DecimalFormat df = new DecimalFormat("0.00000000000000"); public static final double PI = 3.141592653589793d, eps = 1e-9; public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public static void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.ni(); //int t=1; while (t-->0) { String s=in.ns(); String tt=in.ns(); int f=0; int m=tt.length(),n=s.length(); for(int i=0;i<m;++i) { if(tt.charAt(i)!='a') { f=1; break; } } if(f==0) { if(m==1) { out.printLine(1); } else { out.printLine(-1); } } else if(tt.indexOf('a')!=-1) { out.printLine(-1); } else { long tp= CP.binary_Expo(2,n)-1; out.printLine(tp+1); } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private char nc() { return (char)skip(); } public int[] na(int arraySize) { int[] array = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = ni(); } return array; } private int skip() { int b; while ((b = read()) != -1 && isSpaceChar(b)) ; return b; } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = read(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } int[][] nim(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = na(w); } return a; } long[][] nlm(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nla(w); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isNewLine(int c) { return c == '\n'; } public String nextLine() { int c = read(); StringBuilder result = new StringBuilder(); do { result.appendCodePoint(c); c = read(); } while (!isNewLine(c)); return result.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sign = 1; if (c == '-') { sign = -1; c = read(); } long result = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } result *= 10; result += c & 15; c = read(); } while (!isSpaceChar(c)); return result * sign; } public long[] nla(int arraySize) { long array[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextLong(); } return array; } public double nextDouble() { double ret = 0, div = 1; byte c = (byte) read(); while (c <= ' ') { c = (byte) read(); } boolean neg = (c == '-'); if (neg) { c = (byte) read(); } do { ret = ret * 10 + c - '0'; } while ((c = (byte) read()) >= '0' && c <= '9'); if (c == '.') { while ((c = (byte) read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } boolean oj = System.getProperty("ONLINE_JUDGE") != null; void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } } public static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; (long) i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } public static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } public static long pow(long a, long n, long mod) { // a %= mod; long ret = 1; int x = 63 - Long.numberOfLeadingZeros(n); for (; x >= 0; x--) { ret = ret * ret % mod; if (n << 63 - x < 0) ret = ret * a % mod; } return ret; } public static boolean isComposite(long n) { if (n < 2) return true; if (n == 2 || n == 3) return false; if (n % 2 == 0 || n % 3 == 0) return true; for (long i = 6L; i * i <= n; i += 6) if (n % (i - 1) == 0 || n % (i + 1) == 0) return true; return false; } static int ifnotPrime(int[] prime, int x) { return (prime[x / 64] & (1 << ((x >> 1) & 31))); } static int log2(long n) { return (int) (Math.log10(n) / Math.log10(2)); } static void makeComposite(int[] prime, int x) { prime[x / 64] |= (1 << ((x >> 1) & 31)); } public static String swap(String a, int i, int j) { char temp; char[] charArray = a.toCharArray(); temp = charArray[i]; charArray[i] = charArray[j]; charArray[j] = temp; return String.valueOf(charArray); } static void reverse(long arr[]){ int l = 0, r = arr.length-1; while(l<r){ long temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } static void reverse(int arr[]){ int l = 0, r = arr.length-1; while(l<r){ int temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } public static int[] sieveEratosthenes(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } static long digit(long s) { long brute = 0; while (s > 0) { brute+=s%10; s /= 10; } return brute; } public static int[] primefacts(int n, int[] primes) { int[] ret = new int[15]; int rp = 0; for (int p : primes) { if (p * p > n) break; int i; for (i = 0; n % p == 0; n /= p, i++) ; if (i > 0) ret[rp++] = p; } if (n != 1) ret[rp++] = n; return Arrays.copyOf(ret, rp); } static ArrayList<Integer> bitWiseSieve(int n) { ArrayList<Integer> al = new ArrayList<>(); int prime[] = new int[n / 64 + 1]; for (int i = 3; i * i <= n; i += 2) { if (ifnotPrime(prime, i) == 0) for (int j = i * i, k = i << 1; j < n; j += k) makeComposite(prime, j); } al.add(2); for (int i = 3; i <= n; i += 2) if (ifnotPrime(prime, i) == 0) al.add(i); return al; } public static long[] sort(long arr[]) { List<Long> list = new ArrayList<>(); for (long n : arr) { list.add(n); } Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static long[] revsort(long[] arr) { List<Long> list = new ArrayList<>(); for (long n : arr) { list.add(n); } Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static int[] revsort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int n : arr) { list.add(n); } Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } return arr; } public static ArrayList<Integer> reverse( ArrayList<Integer> data, int left, int right) { // Reverse the sub-array while (left < right) { int temp = data.get(left); data.set(left++, data.get(right)); data.set(right--, temp); } // Return the updated array return data; } static ArrayList<Integer> sieve(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return pr; } static boolean[] sieve1(long size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[(int) size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } //for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return prime; } static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) { ArrayList<Integer> al = new ArrayList<>(); if (l == 1) ++l; int max = r - l + 1; int arr[] = new int[max]; for (int p : primes) { if (p * p <= r) { int i = (l / p) * p; if (i < l) i += p; for (; i <= r; i += p) { if (i != p) { arr[i - l] = 1; } } } } for (int i = 0; i < max; ++i) { if (arr[i] == 0) { al.add(l + i); } } return al; } static boolean isfPrime(long n, int iteration) { if (n == 0 || n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; Random rand = new Random(); for (int i = 0; i < iteration; i++) { long r = Math.abs(rand.nextLong()); long a = r % (n - 1) + 1; if (modPow(a, n - 1, n) != 1) return false; } return true; } static long modPow(long a, long b, long c) { long res = 1; for (int i = 0; i < b; i++) { res *= a; res %= c; } return res % c; } private static long binPower(long a, long l, long mod) { long res = 0; while (l > 0) { if ((l & 1) == 1) { res = mulmod(res, a, mod); l >>= 1; } a = mulmod(a, a, mod); } return res; } private static long mulmod(long a, long b, long c) { long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2L) % c; b /= 2; } return x % c; } static long binary_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static void swap(int a, int b) { int tp = b; b = a; a = tp; } static long Modular_Expo(long a, long b, long mod) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % mod; --b; } a = (a * a) % mod; b /= 2; } return res % mod; } static int i_gcd(int a, int b) { while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long ceil_div(long a, long b) { return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } static long getIthBitFromLong(long bits, int i) { return (bits >> (i - 1)) & 1; } // static boolean isPerfectSquare(long a, long b) // { // long sq=Math.sqrt() // } private static TreeMap<Long, Long> primeFactorize(long n) { TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder()); long cnt = 0; long total = 1; for (long i = 2; (long) i * i <= n; ++i) { if (n % i == 0) { cnt = 0; while (n % i == 0) { ++cnt; n /= i; } pf.put(cnt, i); //total*=(cnt+1); } } if (n > 1) { pf.put(1L, n); //total*=2; } return pf; } //less than or equal private static int lower_bound(ArrayList<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid]>=val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== LIS & LNDS ================================ private static int[] LIS(long arr[], int n) { List<Long> list = new ArrayList<>(); int[] cnt=new int[n]; for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); cnt[i]=list.size(); } return cnt; } private static int find1(List<Long> list, long val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid)>=val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } private static long nCr(long n, long r,long mod) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans = (ans%mod*i%mod)%mod; for (long i = 2; i <= n - r; i++) ans /= i; return ans%mod; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } static ArrayList<StringBuilder> permutations(StringBuilder s) { int n=s.length(); ArrayList<StringBuilder> al=new ArrayList<>(); getpermute(s,al,0,n); return al; } // static String longestPalindrome(String s) // { // int st=0,ans=0,len=0; // // // } static void getpermute(StringBuilder s,ArrayList<StringBuilder> al,int l, int r) { if(l==r) { al.add(s); return; } for(int i=l+1;i<r;++i) { char c=s.charAt(i); s.setCharAt(i,s.charAt(l)); s.setCharAt(l,c); getpermute(s,al,l+1,r); c=s.charAt(i); s.setCharAt(i,s.charAt(l)); s.setCharAt(l,c); } } static void initStringHash(String s,long[] dp,long[] inv,long p) { long pow=1; inv[0]=1; int n=s.length(); dp[0]=((s.charAt(0)-'a')+1); for(int i=1;i<n;++i) { pow=(pow*p)%mod; dp[i]=(dp[i-1]+((s.charAt(i)-'a')+1)*pow)%mod; inv[i]=CP.modinv(pow,mod)%mod; } } static long getStringHash(long[] dp,long[] inv,int l,int r) { long ans=dp[r]; if(l-1>=0) { ans-=dp[l-1]; } ans=(ans*inv[l])%mod; return ans; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } static boolean isSquarefactor(int x, int factor, int target) { int s = (int) Math.round(Math.sqrt(x)); return factor * s * s == target; } static boolean isSquare(long x) { long s = (long) Math.round(Math.sqrt(x)); return x * x == s; } static int bs(ArrayList<Integer> al, int val) { int l = 0, h = al.size() - 1, mid = 0, ans = -1; while (l <= h) { mid = (l + h) >> 1; if (al.get(mid) == val) { return mid; } else if (al.get(mid) > val) { h = mid - 1; } else { l = mid + 1; } } return ans; } static void sort(int a[]) // heap sort { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); fast_swap(in, idx, i); } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for (int v : a) { c0[(v & 0xff) + 1]++; c1[(v >>> 8 & 0xff) + 1]++; c2[(v >>> 16 & 0xff) + 1]++; c3[(v >>> 24 ^ 0x80) + 1]++; } for (int i = 0; i < 0xff; i++) { c0[i + 1] += c0[i]; c1[i + 1] += c1[i]; c2[i + 1] += c2[i]; c3[i + 1] += c3[i]; } int[] t = new int[n]; for (int v : a) t[c0[v & 0xff]++] = v; for (int v : t) a[c1[v >>> 8 & 0xff]++] = v; for (int v : a) t[c2[v >>> 16 & 0xff]++] = v; for (int v : t) a[c3[v >>> 24 ^ 0x80]++] = v; return a; } static int[] computeLps(String pat) { int len = 0, i = 1, m = pat.length(); int lps[] = new int[m]; 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; } } } return lps; } static ArrayList<Integer> kmp(String s, String pat) { ArrayList<Integer> al = new ArrayList<>(); int n = s.length(), m = pat.length(); int lps[] = computeLps(pat); int i = 0, j = 0; while (i < n) { if (s.charAt(i) == pat.charAt(j)) { i++; j++; if (j == m) { al.add(i - j); j = lps[j - 1]; } } else { if (j != 0) { j = lps[j - 1]; } else { i++; } } } return al; } static void reverse_ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); for (int l = 0, r = a.length - 1; l < r; ++l, --r) fast_swap(a, l, r); } static void ruffle_sort(int a[]) { shuffle(a); Arrays.sort(a); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } static ArrayList<Long> primeFact(long n) { ArrayList<Long> al = new ArrayList<>(); al.add(1L); while (n % 2 == 0) { if (!al.contains(2L)) { al.add(2L); } n /= 2L; } for (long i = 3; (long) i * i <= n; i += 2) { while ((n % i == 0)) { if (!al.contains((long) i)) { al.add((long) i); } n /= i; } } if (n > 2) { if (!al.contains(n)) { al.add(n); } } return al; } public static long totFact(long n) { long cnt = 0, tot = 1; while (n % 2 == 0) { n /= 2; ++cnt; } tot *= (cnt + 1); for (int i = 3; i <= Math.sqrt(n); i += 2) { cnt = 0; while (n % i == 0) { n /= i; ++cnt; } tot *= (cnt + 1); } if (n > 2) { tot *= 2; } return tot; } static int[] z_function(String s) { int n = s.length(), z[] = new int[n]; for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = Math.min(z[i - l], r - i + 1); while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) ++z[i]; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } return z; } static void fast_swap(int[] a, int idx1, int idx2) { if (a[idx1] == a[idx2]) return; a[idx1] ^= a[idx2]; a[idx2] ^= a[idx1]; a[idx1] ^= a[idx2]; } public static void fast_sort(long[] array) { ArrayList<Long> copy = new ArrayList<>(); for (long i : array) copy.add(i); Collections.sort(copy); for (int i = 0; i < array.length; i++) array[i] = copy.get(i); } static int factorsCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } static long binomialCoeff(long n, long k) { long res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res = (res * (n - i)); res /= (i + 1); } return res; } static long nck(long fact[], long inv[], long n, long k) { if (k > n) return 0; long res = fact[(int) n]%mod; res = (int) ((res%mod* inv[(int) k]%mod))%mod; res = (int) ((res%mod*inv[(int) (n - k)]%mod))%mod; return res % mod; } public static long fact(long x) { long fact = 1; for (int i = 2; i <= x; ++i) { fact = fact * i; } return fact; } public static ArrayList<Long> getFact(long x) { ArrayList<Long> facts = new ArrayList<>(); facts.add(1L); for (long i = 2; i * i <= x; ++i) { if (x % i == 0) { facts.add(i); if (i != x / i) { facts.add(x / i); } } } return facts; } static void matrix_ex(long n, long[][] A, long[][] I) { while (n > 0) { if (n % 2 == 0) { Multiply(A, A); n /= 2; } else { Multiply(I, A); n--; } } } static void Multiply(long[][] A, long[][] B) { int n = A.length, m = A[0].length, p = B[0].length; long[][] C = new long[n][p]; for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { for (int k = 0; k < m; k++) { C[i][j] += ((A[i][k] % mod) * (B[k][j] % mod)) % mod; C[i][j] = C[i][j] % mod; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { A[i][j] = C[i][j]; } } } public static HashMap<Long, Integer> sortMapDesc(HashMap<Long,Integer> map) { List<Map.Entry<Long, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); HashMap<Long, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Long, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Character, Integer> sortMapDescch(HashMap<Character, Integer> map) { List<Map.Entry<Character, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); HashMap<Character, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Character, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Long,Long> sortMapAsclng(HashMap<Long,Long> map) { List<Map.Entry<Long,Long>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> (int)(o1.getValue() - o2.getValue())); HashMap<Long,Long> temp = new LinkedHashMap<>(); for (Map.Entry<Long,Long> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static HashMap<Integer, Integer> sortMapAsc(HashMap<Integer, Integer> map) { List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o1.getValue() - o2.getValue()); HashMap<Integer, Integer> temp = new LinkedHashMap<>(); for (Map.Entry<Integer, Integer> i : list) { temp.put(i.getKey(), i.getValue()); } return temp; } public static long lcm(long l, long l2) { long val = gcd(l, l2); return (l * l2) / val; } public static boolean isSubsequence(String s, String t) { int n = s.length(); int m = t.length(); if (m > n) { return false; } int i = 0, j = 0, skip = 0; while (i < n && j < m) { if (s.charAt(i) == t.charAt(j)) { --skip; ++j; } ++skip; ++i; } return j==m; } public static long[][] combination(int l, int r) { long[][] pascal = new long[l + 1][r + 1]; pascal[0][0] = 1; for (int i = 1; i <= l; ++i) { pascal[i][0] = 1; for (int j = 1; j <= r; ++j) { pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j]; } } return pascal; } public static long gcd(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]); return ret; } public static long lcm(int... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = lcm(ret, array[i]); return ret; } public static int min(int a, int b) { return a < b ? a : b; } public static int min(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]); return ret; } public static long min(long a, long b) { return a < b ? a : b; } public static long min(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]); return ret; } public static int max(int a, int b) { return a > b ? a : b; } public static int max(int... array) { int ret = array[0]; for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]); return ret; } public static long max(long a, long b) { return a > b ? a : b; } public static long max(long... array) { long ret = array[0]; for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]); return ret; } public static long sum(int... array) { long ret = 0; for (int i : array) ret += i; return ret; } public static long sum(long... array) { long ret = 0; for (long i : array) ret += i; return ret; } public static long[] facts(int n) { long[] fact=new long[1005]; fact[0]=1; fact[1]=1; for(int i=2;i<n;i++) { fact[i]=(long)(i*fact[i-1])%mod; } return fact; } public static long[] inv(long[] fact,int n) { long[] inv=new long[n]; inv[n-1]= CP.Modular_Expo(fact[n-1],mod-2,mod)%mod; for(int i=n-2;i>=0;--i) { inv[i]=(inv[i+1]*(i+1))%mod; } return inv; } public static int modinv(long x, long mod) { return (int) (CP.Modular_Expo(x, mod - 2, mod) % mod); } public static int lcs(String s, String t) { int n = s.length(), m = t.length(); int dp[][] = new int[n + 1][m + 1]; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (s.charAt(i - 1) == t.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[n][m]; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void print(long[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(long[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } public void printLine(int i) { writer.println(i); } public void printLine(char c) { writer.println(c); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void py() { print("YES"); writer.println(); } public void pn() { print("NO"); writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } void flush() { writer.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 11
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
36f9d6bff3cf8ef3d4e90ffed6eb5628
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 codeforce { public static void main(String[] args){ Scanner read = new Scanner(System.in); int test = read.nextInt(); while(test-->0){ String s = read.next(); String t = read.next(); int num1 = s.length(); int num2 = 0; for(int i=0;i<t.length();i++){ if(t.charAt(i)=='a') num2++; } long ans=1; if(num2==1 && t.length()==1) ans = 1; else if(num2!=0) ans = -1; else if(num2==0) ans = (long)Math.pow(2, num1); 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 11
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
9982752ed43d54d95e0f01a4735548dc
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 Solution{ 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 int[] string_to_array(String[] arr){ int[] ans=new int[arr.length]; for(int i=0;i<arr.length;i++){ ans[i]=Integer.parseInt(arr[i]); } return ans; } static int count(String s){ int count=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='a'){ count++; } } return count; } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); int testCases=in.nextInt(); List<String>answer=new ArrayList<>(); while(testCases-- > 0){ String a=in.nextLine(); String b=in.nextLine(); int a_n=a.length(); int b_n=b.length(); if(b_n==1 && count(b)==1){ out.println("1"); }else{ if(count(b)>1 || (count(b)==1 && b_n>1)){ out.println("-1"); }else{ long n=(long)Math.pow(2, a_n); out.println(n); } } } for(String s:answer){ out.println(s); } 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 11
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
a10a28ea2a463fc986b786d136bb0f4a
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 javax.sound.sampled.SourceDataLine; public class infinite_replacement { public static void main(String[] args) { Scanner sc=new Scanner (System.in); int t= sc.nextInt(); sc.nextLine(); while(t>0){ String p= sc.nextLine(); String q=sc.nextLine(); if (q.contains("a")){ if (q.length()>1) System.out.println(-1); else System.out.println(1); } else System.out.println((long)Math.pow(2,p.length())); t--; } } }
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 11
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
f566dba759fe68793b77764f5833d843
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.math.*; public class C { static int MAX = 1000000007; static HashMap<Long, BigInteger> mem = new HashMap(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { String s = sc.next(); String t = sc.next(); System.out.println(solve(s, t)); } } static BigInteger nCr(int n, int r) { return factorial(n).divide((factorial(r).multiply(factorial(n - r)))); } private static BigInteger factorial(long n) { long x = n; BigInteger fact = BigInteger.ONE; if ((n == 0) || (n == 1)) { return BigInteger.ONE; } else { if (mem.containsKey(n)) return mem.get(n); BigInteger val = factorial(n-1).multiply(BigInteger.valueOf(n)); mem.put(n, val); return val; } } // static long fact(int n) { // if (n == 1) return 1; // if (n == 0) return 1; // if (mem.containsKey(n)) return mem.get(n); // long val = n * fact(n-1); // mem.put(n, val); // return val; // } static long calc(int n) { long ans = 0; for(int i=1; i<=n; i++) { ans += nCr(n,i).longValue(); } return ans; } static long solve(String s, String t) { for(int i=0; i<t.length(); i++) { if (t.charAt(i) == 'a') { if (t.length() == 1) return 1; return -1; } } //calculate fact return (calc(s.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 11
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
3dad8601477f663823f5821a46ac21c4
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 Main { static Scanner in = new Scanner(System.in); static String s, b; public static void main(String[] args) { int casesNumber = in.nextInt(); while (casesNumber-- != 0) { s = in.next(); b = in.next(); System.out.println(result()); } } private static long result() { if (b.contains("a")) if (b.length() > 1) return -1; else 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 11
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
1fc66aa5825f4e23dd1aad05a231b169
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 in=new Scanner(System.in); int t= in.nextInt(); for(int i=0;i<t;i++){ String s=in.next(); String tt=in.next(); long x=tt.length(); if(!tt.equals("a")) { tt = tt.replaceFirst("a", ""); if (tt.length() != x) { System.out.println("-1"); continue; } long c = (long) Math.pow(2, s.length()); System.out.println(c); } 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 11
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
5edad8e52c00cae199bfea730982f866
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.HashMap; import java.util.HashSet; import java.util.StringTokenizer; /* Name of the class has to be "Main" only if the class is public. */ public class C_Infinite_Replacement { public static long binPow(long a, long n) { long res = 1; while (n != 0) { if ((n & 1) == 1) { res *= a; } a *= a; n >>= 1; } return res; } public static long binPow(long a, long n, long mod) { long res = 1; while (n != 0) { if ((n & 1) == 1) { res = (res * a) % mod; } a = (a * a) % mod; n >>= 1; } return res; } public static void main(String[] args) throws java.lang.Exception{ // your code goes here FS fs = new FS(); PrintWriter pw = new PrintWriter(System.out); long t; t=fs.ni(); while(t-->0) { String s; String ts; s=fs.next(); ts=fs.next(); int len1=s.length(); int len2=ts.length(); HashMap<Character,Integer> map=new HashMap<>(); HashMap<Character,Integer> map2=new HashMap<>(); for(int i=0;i<s.length();i++){ char ch=s.charAt(i); map.put(ch,map.getOrDefault(ch,0)+1); } for(int i=0;i<ts.length();i++){ char ch=ts.charAt(i); map2.put(ch,map2.getOrDefault(ch,0)+1); } int sizer=0; if(map2.containsKey('a')){ sizer=map2.get('a'); } if(sizer==0){ pw.println(binPow(2,len1)); }else if(sizer==1 && len2==1){ pw.println(1); }else pw.println(-1); } pw.flush(); } } class FS { BufferedReader br; StringTokenizer st; public FS() { 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; } }
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 11
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
c53eedcde705d72e3ec9ec728f278066
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.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Solution { static ArrayList<String> list; public static void main(String[] args) { FastScanner fs = new FastScanner(); int ti = fs.nextInt(); outer: while (ti-- > 0) { String s = fs.next(); String t = fs.next(); if (t.equals("a")) { System.out.println(1); continue outer; } if (t.contains("a")) { System.out.println(-1); continue outer; } System.out.println((long) Math.pow(2, s.length())); } } static final int mod = 1_000_000_007; static void sort(long[] a) { Random random = new Random(); int n = a.length; for (int i = 0; i < n; i++) { int oi = random.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void sort(int[] a) { Random random = new Random(); 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 long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long binpow(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { if (b % 2 == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } static int divCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Pair { int a; int b; public Pair(int a, int b) { this.a = a; this.b = 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 11
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
2e418c3f52b50f8fb735de64e44854c8
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 : Multi-Thread */ public class C { //public class Main { // static int INF = 998244353; static int INF = (int) 1e9 + 7; static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; public static void main(String[] args) { int test = fs.nextInt(); // int test = 1; for (int cases = 0; cases < test; cases++) { // solve(); final_answer.append(solve() + "\n"); } out.print(final_answer.toString()); out.flush(); } static StringBuilder final_answer = new StringBuilder(); static String solve() { char ar[] = fs.next().toCharArray(); char br[] = fs.next().toCharArray(); boolean rs = true, isa = false; for (char x : br) { if (x == 'a') { isa = true; } if (x != 'a') { rs = false; } } // only a if (rs) { if (br.length == 1) return 1 + ""; return -1 + ""; } if (isa) return -1 + ""; long ans = (long) Math.pow(2, ar.length); return ans + ""; } static class Pair { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "[" + first + "," + second + "]"; } } static class LongPair { long first; long second; LongPair(long a, long b) { this.first = a; this.second = b; } public String toString() { return "[" + first + "," + second + "]"; } } 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 OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println() { writer.print("\n"); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } private static final FastReader fs = new FastReader(); private static final OutputWriter out = new OutputWriter(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 11
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
1586ffb446fad1e36b85e41ecfad60c1
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
/* IF I HAD CHOICE, I WOULD HAVE BEEN A PIRATE, LOL XD ; _____________############# _____________##___________## ______________#____________# _______________#____________#_## _______________#__############### _____##############______________# _____##____________#_____################ ______#______##############______________# ______##_____##___________#______________## _______#______#____PARTH___#______________# ______##_______#___________##____________# ______###########__________##___________## ___________#_#_##________###_____########_____### ___________#_#_###########_#######_______#####__######### ######_____#_#______##_#_______#_#########___######## ##___#########______##_#____#######________##### __##________##################____##______### ____#____________________________##_#____## _____#_____###_____##_____###_____###___## ______#___##_##___##_#____#_##__________# ______##____##_____###_____##__________## ________##___________________________## ___________######################### */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int tes = sc.nextInt(); while(tes-->0){ String a = sc.nextLine(); String b = sc.nextLine(); long ans = 1; if(b.length()>1 && b.contains("a")){ ans=-1; } else if (!b.contains("a")) { ans= (long) Math.pow(2,a.length()); } else if (b.length()==1 && b.contains("a")){ ans=1; } out.println(ans); } 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 11
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
12f9ac7e4bf1836d3284d542157542b8
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 { void go() { String s = Reader.next(); String t = Reader.next(); int m = s.length(); int n = t.length(); long ans = 0; long[][] comb = new long[m + 1][m + 1]; comb[0][0] = 1; for(int i = 1; i <= m; i++) { comb[i][0] = 1; for(int j = 1; j <= i; j++) { comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]; } } if(t.indexOf("a") < 0) { for(int i = 0; i <= m; i++) { //System.out.println(comb[m][i]); ans += comb[m][i]; } } else { ans = n == 1 ? 1 : -1; } Writer.println(ans); } void solve() { for(int T = Reader.nextInt(); T > 0; T--) go(); } void run() throws Exception { Reader.init(System.in); Writer.init(System.out); solve(); Writer.close(); } public static void main(String[] args) throws Exception { new C().run(); } public static class Reader { public static StringTokenizer st; public static BufferedReader br; public static void init(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } public static String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new InputMismatchException(); } } return st.nextToken(); } public static int nextInt() { return Integer.parseInt(next()); } public static long nextLong() { return Long.parseLong(next()); } public static double nextDouble() { return Double.parseDouble(next()); } } public static class Writer { public static PrintWriter pw; public static void init(OutputStream os) { pw = new PrintWriter(new BufferedOutputStream(os)); } public static void print(String s) { pw.print(s); } public static void print(char c) { pw.print(c); } public static void print(int x) { pw.print(x); } public static void print(long x) { pw.print(x); } public static void println(String s) { pw.println(s); } public static void println(char c) { pw.println(c); } public static void println(int x) { pw.println(x); } public static void flush() { pw.flush(); } public static void println(long x) { pw.println(x); } public static void close() { 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 11
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
fdfc2af8f818f9b4f9cd31d53a8235c3
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.*; import java.util.Map.Entry; public class codeforces { static int mod = 1000000007; public static void main(String[] args) { FastReader sc = new FastReader(); StringBuilder ss = new StringBuilder(); try { int t = sc.nextInt(); while(t-->0) { String s = sc.next(); String s1 = sc.next(); long ans = 0; if(s1.length() == 1 && s1.charAt(0)=='a') { ans = 1; } else { boolean flag = true; for(int i = 0 ; i<s1.length() ;i++) { if(s1.charAt(i) == 'a') { flag = false; break; } } if(flag) { long n = s.length(); long pow = (long) Math.pow(2, n); ans = pow; } else { ans = -1; } } ss.append(ans +"\n"); } System.out.println(ss); } catch(Exception e) { System.out.println(e.getMessage()); } } static class Pair1 implements Comparable<Pair1>{ long val; int i; Pair1(long val , int i){ this.val = val; this.i = i; } @Override public int compareTo(Pair1 o) { // TODO Auto-generated method stub return (int) (this.val - o.val); } } static long pow(long a, long b) { long ans = 1; long temp = a; while(b>0) { if((b&1) == 1) { ans*=temp; } temp = temp*temp; b = b>>1; } return ans; } static long ncr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(long n) { long res = 1; for (int i = 2; i <= n; i++) { res = (res * i); } return res; } static long gcd(long a, long b) { if(b == 0) { return a; } return gcd(b , a%b); } static long lcm(long a, long b) { long pr = a*b; return pr/gcd(a,b); } static ArrayList<Integer> factor(long n) { ArrayList<Integer> al = new ArrayList<>(); for(int i = 1 ; i*i<=n;i++) { if(n%i == 0) { if(n/i == i) { al.add(i); } else { al.add(i); al.add((int) (n/i)); } } } return al; } static class Pair implements Comparable<Pair>{ long a; long b ; int c; Pair(long a, long b, int c){ this.a = a; this.b = b; this.c = c; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub int temp = (int) (this.b - o.b); if(temp == 0) { return (int) (this.a - o.a); } return temp; } } static int[] sieve(int n) { int a[] = new int[n+1]; Arrays.fill(a,-1); a[1] = 1; for(int i = 2 ; i*i <=n ; i++) { if(a[i] == -1) { for(int j = 2*i ; j<=n ; j+=i) { a[j] = 1; } } } int prime = -1; for(int i = n ; i>=1 ; i--) { if(a[i] == 1) { a[i] =prime; } else { prime = i; a[i] = prime; } } return a; } static ArrayList<Long> pf(long n) { ArrayList<Long> al = new ArrayList<>(); while(n%2 == 0) { al.add(2l); n = n/2; } for(long i = 3 ; i*i<=n ; i+=2) { while(n%i == 0) { al.add(i); n = n/i; } } if(n>2) { al.add( n); } return al; } 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 11
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
dc8e5b7ebd82ddd7cf66bf2619530678
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author @debanjandhar12 */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "@debanjandhar12", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CInfiniteReplacement solver = new CInfiniteReplacement(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } } static class CInfiniteReplacement { public void solve(int testNumber, InputReader in, OutputWriter out) { String s = in.readString(); String t = in.readString(); if (t.length() == 1 && t.contains("a")) { out.printLine(1); return; } else if (t.contains("a")) { out.printLine(-1); return; } else { long a = 1; a = a << (s.length()); out.printLine(a); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } public void printLine(int i) { writer.println(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 11
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
df780795f9b768a8d8c26a3a0bffea69
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 Main2 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int t = in.nextInt(); while (t-- > 0) { String s = in.next(),b=in.next(); int a = 0, other = 0; for (char c : b.toCharArray()) { if(c=='a') a++; else other++; } if (a >0 && (other != 0||a>1)) { System.out.println(-1); } else if (other == 0) { System.out.println(1+(a==1?0:s.length())); }else{ System.out.println((long)(1L <<s.length())); } } } String solution(String s) { if (s.length()<2||s.charAt(s.length()-1)=='A') { return "NO"; } Deque<Character> dq = new ArrayDeque<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == 'B') { if (dq.isEmpty()) { return "NO"; } dq.pollLast(); } else if (c == 'A') { dq.offer('A'); } } return "YES"; } } static class ru_ifmo_niyaz_arrayutils_ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class net_egork_misc_ArrayUtils { public static long sumArray(int[] array) { long result = 0; for (int element : array) result += element; return result; } } static class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } }
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 11
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
5eb82dc3b26d46035a84231f3508906d
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
// Jai Shree Ram ⛳⛳⛳ // Jai Bajrang Bali // Jai Saraswati maa // Har Har Mahadev // Thanks Kalash Shah :) import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Character.isUpperCase; public class A { static FastScanner sn = new FastScanner(); static int mod = (int) (1e9 + 7); public static void main(String[] args) { int T = sn.nextInt(); while (T-- > 0) solve(); } public static void solve() { String s = sn.next(); String t = sn.next(); long count = 0; if (Objects.equals(t, "a")) { System.out.println(1); return; } for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == 'a') { count++; } } if (count == 0) { System.out.println((long) Math.pow(2, s.length())); } else { System.out.println(-1); } } //----------------------------------------------------------------------------------// public static void mergesort(long[] arr, int l, int r) { if (l >= r) { return; } int mid = l + (r - l) / 2; mergesort(arr, l, mid); mergesort(arr, mid + 1, r); Merge(arr, l, mid, r); } static long getFirstSetBitPos(long n) { return (long) ((Math.log10(n & -n)) / Math.log10(2)) + 1; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class Pair implements Comparable<Pair> { int ind; int ch; Pair(int a, int ch) { this.ind = a; this.ch = ch; } public int compareTo(Pair o) { if (this.ch == o.ch) { return this.ind - o.ind; } return this.ch - o.ch; } public String toString() { return ""; } } static int countSetBits(long n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } public static void Merge(long[] arr, int l, int mid, int r) { long[] B = new long[r - l + 1]; int i = l; int j = mid + 1; int k = 0; while (i <= mid && j <= r) { if (arr[i] <= arr[j]) { B[k++] = arr[i++]; } else { B[k++] = arr[j++]; } } while (i <= mid) { B[k++] = arr[i++]; } while (j <= r) { B[k++] = arr[j++]; } for (int i1 = 0, j1 = l; i1 < B.length; i1++, j1++) { arr[j1] = B[i1]; } } 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()); } char nextChar() { return next().charAt(0); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[] scan(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } void print(int[] a) { for (int j : a) { System.out.print(j + " "); } } void printl(long[] a) { for (long j : a) { System.out.print(j + " "); } } long nextLong() { return Long.parseLong(next()); } } static class Debug { public static void debug(long a) { System.out.println("--> " + a); } public static void debug(long a, long b) { System.out.println("--> " + a + " + " + b); } public static void debug(char a, char b) { System.out.println("--> " + a + " + " + b); } public static void debug(int[] array) { System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(char[] array) { System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(HashMap<Integer, Integer> map) { System.out.print("Map--> " + map.toString()); } } }
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 11
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
414be54c8508628ae3329a977703db90
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
/* JAIKARA SHERAWAALI DA BOLO SACHE DARBAR KI JAI HAR HAR MAHADEV JAI BHOLENAATH Rohit Kumar "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." __Hope is a big word, never lose it__ */ import java.io.*; import java.util.*; public class Main { 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();} long nextLong(){ return Long.parseLong(next());} int nextInt(){ return Integer.parseInt(next());} } static int mod=(int)1e9+7; 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"); } FastReader sc = new FastReader(); int tt=sc.nextInt(); while(tt-->0) { String s=sc.next(); String t=sc.next(); int as=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='a') { as++; } } int at=0; for(int i=0;i<t.length();i++) { if(t.charAt(i)=='a') { at++; } } if(at==1&&t.length()==1) { print(1); continue; } if(at>0) { print(-1); } else{ print((long)Math.pow(2,as)); } } } private static boolean check(String s,int n,int x) { if(s.length()!=n) { return false; } HashSet<Character> hs=new HashSet<>(); for(int i=0;i<s.length();i++) { hs.add(s.charAt(i)); } if(hs.size()!=x) { return false; } return true; } static <t>void print(t o){ System.out.println(o); } static int myXOR(int x, int y) { // Initialize result int res = 0; // Assuming 32-bit Integer for(int i = 31; i >= 0; i--) { // Find current bits in x and y int b1 = ((x & (1 << i)) == 0 ) ? 0 : 1; int b2 = ((y & (1 << i)) == 0 ) ? 0 : 1; // If both are 1 then 0 else xor is same as OR int xoredBit = ((b1 & b2) != 0) ? 0 : (b1 | b2); // Update result res <<= 1; res |= xoredBit; } return res; } static boolean ispalindrome(String str) { int l=0,r=str.length()-1; while(l<r) { if(str.charAt(l)!=str.charAt(r)) { return false; } l++; r--; } return true; } static void reverse(int[] arr, int from, int till) { for (int i = from, j = till; i < j; i++, j--) { swap(arr, i, j); } } static void swap(int[] arr, int a, int b) { int tmp = arr[a]; arr[a] = arr[b]; arr[b] = tmp; } void 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; } } for(int i = 2; i <= n; i++) { if(prime[i] == true) System.out.print(i + " "); } } static long gcd1(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static boolean isprime(int 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; } }
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 11
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
ea494f8f372658e9496f21b1d8c76e28
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 { // For fast input output static 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; } } // end of fast i/o code public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while (t-- > 0) { String st = s.next(); String tt = s.next(); boolean hasA = false; long ans = 0; for (char c : tt.toCharArray()) { if (c == 'a') { hasA = true; break; } } if (hasA && tt.length() > 1) { System.out.println(-1); } else if (hasA && tt.length() == 1) { System.out.println(1); } else { ans = (long) Math.pow(2, st.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 11
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
933480c35fb4b4b286cb841d012af1d0
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.io.Writer; import java.util.*; /* Name of the class has to be "Main" only if the class is public. */ public class codeforces{ static FastReader sc=new FastReader(); static PrintWriter writer = new PrintWriter(System.out); 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; } } public static void main(String[] args) { int t=sc.nextInt(); while(t-->0){ String s=sc.next(); String tstr=sc.next(); int a_contains=0; int not_a_contains=0; for(int i=0;i<tstr.length();i++){ if(tstr.charAt(i)=='a'){ a_contains=1; } if(tstr.charAt(i)!='a'){ not_a_contains=1; } } if(a_contains==1&&tstr.length()==1){ System.out.println("1"); }else if(a_contains==1){ 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 11
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
baefb84bd28a495c83392b6c6afab6c2
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 { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); int t = in.nextInt(); for (int i = 0; i < t; i++) { String s1 = in.next(), s2 = in.next(); boolean f = false; if ("a".equals(s2)) { System.out.println(1); continue; } for (int j = 0; j < s2.length(); j++) { if (s2.charAt(j) == 'a') { f = true; break; } } if (f) { System.out.println(-1); } else { int n = s1.length(); int count = 0; if (n > 2) { for (int j = 1; j <= n - 3; j++) { int nn = (n - j - 1); int num = nn * (nn - 1) / 2; num *= j; count += num; } } System.out.println((long)Math.pow(2, 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
5e30ffe8a2e27e209b328a2e40734d72
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 java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class C { public static void process() throws IOException { String s = sc.next(); int n = s.length(); String t = sc.next(); int m = t.length(); if(m == 1) { if(t.charAt(0) == 'a') { System.out.println(1); return; } System.out.println((long)Math.pow(2, n)); return; } for(int e : t.toCharArray()) { if(e == 'a') { System.out.println(-1); return; } } System.out.println((long)Math.pow(2, n)); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static void google(int tt) { System.out.print("Case #" + (tt) + ": "); } static FastScanner sc; static FastWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new FastWriter(System.out); } else { sc = new FastScanner("input.txt"); out = new FastWriter("output.txt"); } long s = System.currentTimeMillis(); int t = 1; t = sc.nextInt(); int TTT = 1; while (t-- > 0) { // google(TTT++); process(); } out.flush(); // tr(System.currentTimeMillis()-s+"ms"); } private static boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void tr(Object... o) { if (!oj) System.err.println(Arrays.deepToString(o)); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } /* @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair key = (Pair) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } */ } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long sqrt(long z) { long sqz = (long) Math.sqrt(z); while (sqz * 1L * sqz < z) { sqz++; } while (sqz * 1L * sqz > z) { sqz--; } return sqz; } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } 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 lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = -1; while (low <= high) { mid = (low + high) / 2; if (arr[mid] > x) { high = mid - 1; } else { ans = mid; low = mid + 1; } } return ans; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length - 1, mid = -1; int ans = arr.length; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) { ans = mid; high = mid - 1; } else { low = mid + 1; } } return ans; } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void reverseArray(int[] a) { int n = a.length; int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } //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); } // compress Big value to Time Limit 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; } // Fast Writer public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } // Fast Inputs 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[] readArray(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readArrayLong(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public int[][] readArrayMatrix(int N, int M, int Index) { if (Index == 0) { int[][] res = new int[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = (int) nextLong(); } return res; } int[][] res = new int[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = (int) nextLong(); } return res; } public long[][] readArrayMatrixLong(int N, int M, int Index) { if (Index == 0) { long[][] res = new long[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) res[i][j] = nextLong(); } return res; } long[][] res = new long[N][M]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) res[i][j] = 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[] readArrayDouble(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
9f6c1d9511dca5d35e6780903edcb5a6
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.InputStreamReader; import java.util.Scanner; public class InfiniteReplacement_1674C { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { String s1 = br.readLine(); String s2 = br.readLine(); if (s2.contains("a")) { if (s2.length() == 1) { System.out.println(1); } else { System.out.println(-1); } } else { long p = (long) Math.pow(2, s1.length()); System.out.println(p); } } // System.out.println(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
e794dd65d15af2448a7bbf9f6ad6f522
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; public class Cv { //==========================Solution============================// public static void main(String[] args) { FastScanner in = new FastScanner(); Scanner input = new Scanner(System.in); PrintWriter o = new PrintWriter(System.out); int T = input.nextInt(); while (T-- > 0) { String s = input.next(); String t = input.next(); long y = 0; int v = 0; int b = 0; for (int i = 0; i < t.length(); i++) { char c = t.charAt(i); if (c == 'a') { y++; break; } } for (int i = 0; i < s.length(); i++) { char f = s.charAt(i); if (f == 'a') { b++; } } if (y > 0) { if (b > 0 && t.length() != 1) { o.println(-1); } else { o.println(1); } } else { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == 'a') { v++; } } o.println((long)(Math.pow(2, v))); } } o.close(); } //==============================================================// 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(); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } byte nextByte() { return Byte.parseByte(next()); } short nextShort() { return Short.parseShort(next()); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return java.lang.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