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
ca83bb6ae759556b7e1a2ec2358d1756
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class A { void solve() throws IOException { int t = nextInt(); for (int tt = 0; tt < t; ++tt) { /* aab, 3 aabbaa, aabbaaaabbaa, aabbaaaabbaaaabbaaaabbaa baaaab, abc abccba cbaabc */ int n = nextInt(); int k = nextInt(); String s = nextToken(); Set<String> set = new HashSet<>(); Set<String> result = new HashSet<>(); int res = rec(s, Math.min(2, k), set, result); writer.println(result.size()); } } int rec(String s, int k, Set<String> visited, Set<String> result) { if (k <= 0) { result.add(s); return 1; } // if (visited.contains(s)) // return 0; // visited.add(s); return rec(rev(s) + s, k - 1, visited, result) + rec(s + rev(s), k - 1, visited, result); } String rev(String s) { char[] arr = s.toCharArray(); for (int i = 0; i < arr.length / 2; ++i) { char tmp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = tmp; } return new String(arr); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } StringTokenizer tokenizer; PrintWriter writer; BufferedReader reader; public void run() { try { writer = new PrintWriter(System.out); reader = new BufferedReader(new InputStreamReader(System.in)); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new A().run(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
8f667ee292a5041914dd7763b14f4b76
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class Template{ public static int gcd(int a,int b){ if(a==0) return b; return gcd(b%a,a); } public static int lcm(int a,int b){ return a*b/gcd(a,b); } public static void solve(){ Scanner sc=new Scanner(System.in); //System.out.println("Enter number T"); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); String rev=""; int ans=0; for(int i=n-1;i>=0;i--) rev=rev+s.charAt(i); if(!s.equals(rev) && k==1) ans=2; else if(s.equals(rev) || k==0 || k==1){ ans=1; } else{ ans=2; } System.out.println(ans); } } public static void main(String args[])throws IOException{ Scanner sc=new Scanner(System.in); solve(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
4c5f213a2bd7b742115774a53f35d4e1
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Main { public static boolean isPalindrome (String s) { int mid = s.length()/2; int j = s.length()-1; for (int i = 0; i < mid; i++) { if (s.charAt(i)!=s.charAt(j)) {return false;} j--; } return true; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int T = scan.nextInt(); while (T-->0) { int n = scan.nextInt(); int k = scan.nextInt(); String s = scan.next(); if (isPalindrome(s)||k==0) {System.out.println(1);} else {System.out.println(2);} } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
95e7a62ae3b12bcdbe0f8530a8f69aa2
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Main { static boolean isPal(StringBuilder s) { StringBuilder s1 = new StringBuilder(s); s.reverse(); if(s1.compareTo(s)==0) return true; else return false; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int l = sc.nextInt(); int k = sc.nextInt(); StringBuilder s = new StringBuilder(sc.next()); if(k==0) { System.out.println("1"); } else if(k==1) { if(isPal(s)) { System.out.println("1"); } else { System.out.println("2"); } } else { if(isPal(s)) { System.out.println("1"); } else { System.out.println("2"); } } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
38da890a21c417a4fc7d98f7b77b776b
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Rough { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test > 0) { int length = sc.nextInt(); int k = sc.nextInt(); String str = sc.next(); if (k == 0) { System.out.println(1); } else if (k == 1) { if (palindrome(str)) { System.out.println(1); } else { System.out.println(2); } } else if (k >= 2) { if (palindrome(str)) { System.out.println(1); } else { System.out.println(2); } } test--; } } private static boolean palindrome(String str) { // TODO Auto-generated method stub int si = 0; int ei = str.length() - 1; while (si <= ei) { if (str.charAt(si) != str.charAt(ei)) { return false; } si++; ei--; } return true ; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
f55f73557a2809f6ec296165be5b6797
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; public class sagar{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(""); Reader rd = new Reader(); int t = rd.nextInt(); int n ,k,i; String line; while(t>0){ n = rd.nextInt(); k = rd.nextInt(); line = rd.readLine(); for(i=0;i<n/2;i++){ if(line.charAt(i) != line.charAt(n-i-1)){ break; } } if(i==n/2){ sb.append(1+"\n"); } else{ if(k == 0){ sb.append(1+"\n"); } else{ sb.append(2+"\n"); } } t--; } System.out.println(sb.toString()); } 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[1200]; // line length int cnt = 0, c; c = read(); while (c <= ' ') c = read(); while ((c) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; c =read(); } 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(); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
3f671af6603dbf73beb25d85e17a14cf
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String... args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); while (t-- > 0) { int k, n; n = input.nextInt(); k = input.nextInt(); String str = input.next(); boolean flag = true; for (int i = 0, j = n-1; i <= j; i++, j--) { if (str.charAt(i) == str.charAt(j)) continue; flag = false; break; } if (flag || k == 0) System.out.println(1); else System.out.println(2); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
93db93b8e2c9c72ba8f9f1b9255c7340
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { // static HashSet<String> set = new HashSet<>(); // static HashSet<Pair> done = new HashSet<>(); static int ans; static String reverse(String s) { String ans = ""; for(int i = s.length()-1; i>=0; i--) { ans += s.charAt(i); } return ans; } static boolean check(String s) { int n = s.length(); int i = 0, j = n-1; while(i<=j) { if(s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } static void solve(int k, String s) { if(check(s)) { ans++; return; } if(k==0) { ans++; return; } solve(k-1, s+reverse(s)); solve(k-1, reverse(s)+s); } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t != 0) { t--; ans = 0; int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); // HashSet<String> set = new HashSet<>(); solve(k, s); // int ans = set.size(); System.out.println(ans); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
672d7e09d9ff3c4265760c897765489b
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class assignment { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); int []arr=new int[t]; for (int i = 0; i < t; i++) { int n = scan.nextInt(); int k = scan.nextInt(); String s1 = scan.next(); int counter = 0; for (int j = 0; j < s1.length() / 2; j++) { if (s1.charAt(j) == s1.charAt(s1.length() - j - 1)) { counter++; } } if (counter==s1.length()/2 && s1.length()>1 && k!=0){ arr[i]=1; } else if(s1.length()==1 || k==0){ arr[i]=1; } else { arr[i]=2; } } for(int i=0;i<t;i++){ System.out.println(arr[i]); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
552816ae39e99d4d8c4f6b5ec91cb95a
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class CodeForces { static String reverse(String string) { StringBuilder s = new StringBuilder(string); return s.reverse().toString(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); if (k == 0) { System.out.println(1); } else if (s.equals(reverse(s))) { System.out.println(1); } else { System.out.println(2); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
647fac4fd1df525631f012685b72a953
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class MyLogger { // // List<Level> logLevel; // Map<String,Level> map; // // MyLogger (List<Level> logLevel){ // this.logLevel = logLevel; // for(Level lev : logLevel){ // map.put(lev.getName(),lev); // } // } // // public void error(String msg){ // Level error = map.get("ERROR"); // // print red // // } // // void printLevels(){ // System.out.println(logLevel); // } // enum Level { //// //// ERROR("ERROR",true), //// INFO(true), //// DEBUG(false); // // // public boolean isEnabled() { // return enable; // } // // public void setEnabled(boolean level) { // this.enable = level; // } // // boolean enable; // // public String getName() { // return name; // } // // public void setName(String name) { // this.name = name; // } // // String name; // // Level(String name, boolean enable){ // this.enable = enable; // } // // // } public static String rev(String s) { int n=s.length(); String r=""; for(int i=n-1;i>=0;i--) { r+=s.charAt(i); } return r; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t>0) { int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); if(k==0||(rev(s).equals(s))) { System.out.println(1); } else { System.out.println(2); } t--; } // Graph graph=new Graph(7); // graph.graph[0].add(new Edge(3,40)); // graph.graph[3].add(new Edge(0,40)); // // graph.graph[0].add(new Edge(1,10)); // graph.graph[1].add(new Edge(0,10)); // // graph.graph[1].add(new Edge(2,10)); // graph.graph[2].add(new Edge(1,10)); // // graph.graph[2].add(new Edge(3,10)); // graph.graph[3].add(new Edge(2,10)); // // graph.graph[4].add(new Edge(3,10)); // graph.graph[3].add(new Edge(4,10)); // // graph.graph[5].add(new Edge(6,1)); // graph.graph[6].add(new Edge(5,1)); // // graph.graph[4].add(new Edge(5,1)); // graph.graph[5].add(new Edge(4,1)); // // graph.graph[4].add(new Edge(6,6)); // graph.graph[6].add(new Edge(4,6)); // int a[][]={ // {0,1,1}, // {0,0,0}, // {0,1,1} // // }; // graph.bridgeLength(a); // for(int i=0;i<a.length;i++) // { // for(int j=0;j<a[0].length;j++) // { // System.out.print(a[i][j]+" "); // } // System.out.println(); // } // LinkedList q=new LinkedList<>(); } } class Po { int key,value; Po(int k,int v) { key=k; value=v; } } class Graph { ArrayList<Edge> graph[]; Graph(){ } Graph(int v) { graph = new ArrayList[v]; for (int i = 0; i < v; i++) { graph[i] = new ArrayList<>(); } } private void bridgeLength(int a[][]) { int n = a.length; int m = a[0].length; boolean flag = true; Queue<Cor> q = new LinkedList<>(); for (int i = 0; i < n && flag; i++) { for (int j = 0; j < m && flag; j++) { if (a[i][j] == 1) { dfs(a, i, j, q); flag = false; } } } System.out.println(bridge(a, q)); } public int bridge(int a[][], Queue<Cor> q) { int n = a.length; int m = a[0].length; int level = 0; while (q.size() > 0) { int size = q.size(); while (size-- > 0) { Cor pair = q.remove(); if (a[pair.x][pair.y] == 1) return level; int i = pair.x; int j = pair.y; if (i + 1 < a.length && a[i + 1][j] != -1) { q.add(new Cor(i + 1, j)); a[i + 1][j] = -1; } if (i - 1 >= 0 && a[i - 1][j] != -1) { q.add(new Cor(i - 1, j)); a[i - 1][j] = -1; } if (j + 1 < a[0].length && a[i][j + 1] != -1) { q.add(new Cor(i, j + 1)); a[i][j + 1] = -1; } if (j - 1 >= 0 && a[i][j - 1] != -1) { q.add(new Cor(i, j - 1)); a[i][j - 1] = -1; } } level++; } return -1; } public void dfs(int a[][], int i, int j, Queue<Cor> q) { a[i][j] = -1; q.add(new Cor(i, j)); if (i + 1 < a.length && a[i + 1][j] == 1) { dfs(a, i + 1, j, q); } if (i - 1 >= 0 && a[i - 1][j] == 1) { dfs(a, i - 1, j, q); } if (j + 1 < a[0].length && a[i][j + 1] == 1) { dfs(a, i, j + 1, q); } if (j - 1 >= 0 && a[i][j - 1] == 1) { dfs(a, i, j - 1, q); } } public void dijkastra(int src) { boolean visited[] = new boolean[7]; PriorityQueue<Pair> q = new PriorityQueue<>(); q.add(new Pair(src, src + "", 0)); while (q.size() > 0) { Pair pair = q.remove(); if (visited[pair.v]) { continue; } visited[pair.v] = true; System.out.println(pair.v + "####" + pair.psf + "##########" + pair.wsf); for (Edge edge : graph[pair.v]) { if (!visited[edge.nbr]) { q.add(new Pair(edge.nbr, pair.psf + edge.nbr, pair.wsf + edge.wt)); } } } } public void nearestZero(int a[][]) { int n = a.length; int m = a[0].length; Queue<Cor> q = new LinkedList<Cor>(); int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == 0) { q.add(new Cor(i, j)); } else { count++; a[i][j] = -1; } } } int level = 1; int dirs[][] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; while (q.size() > 0) { int size = q.size(); while (size-- > 0) { Cor pair = q.remove(); for (int i = 0; i < 4; i++) { int x = pair.x + dirs[i][0]; int y = pair.y + dirs[i][1]; if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == -1) { q.add(new Cor(x, y)); a[x][y] = level; count--; } } } level++; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } class Cor { int x, y; Cor(int a, int b) { x = a; y = b; } } public boolean isBipartite() { int visited[] = new int[7]; Arrays.fill(visited, -1); Queue<Pai> q = new LinkedList<>(); q.add(new Pai(0, 0)); while (q.size() > 0) { int size = q.size(); while (size-- > 0) { Pai pair = q.poll(); if (visited[pair.v] != -1) { if (visited[pair.v] != pair.level) return false; } visited[pair.v] = pair.level; for (Edge edge : graph[pair.v]) { if (visited[edge.nbr] == -1) { q.add(new Pai(edge.nbr, pair.level + 1)); } } } } return true; } class Pai { int v; int level; Pai(int v, int l) { this.v = v; level = l; } } // public boolean isCyclic() // { // Queue<> q=new LinkedList<>(); // q.add(new Pair(0,0+"")); // String visited[]=new String[7]; // while(q.size()>0) // { // Pair rem=q.poll(); // if(visited[rem.v]!=null) // { // System.out.println(visited[rem.v]); // System.out.println(rem.psf); // return true; // } // visited[rem.v]=rem.psf; // for(Edge edge:graph[rem.v]) // { // if(visited[edge.nbr]==null) // { // q.add(new Pair(edge.nbr,rem.psf+edge.nbr)); // } // } // } // return false; // } public void prims(int src) { boolean visited[] = new boolean[7]; PriorityQueue<Pair> q = new PriorityQueue<>(); q.add(new Pair(src, src + "", 0)); while (q.size() > 0) { Pair pair = q.remove(); if (visited[pair.v]) { continue; } visited[pair.v] = true; System.out.println(pair.v + "####" + pair.psf + "##########" + pair.wsf); for (Edge edge : graph[pair.v]) { if (!visited[edge.nbr]) { q.add(new Pair(edge.nbr, pair.v + "", edge.wt)); } } } } public void compo() { boolean visited[] = new boolean[7]; ArrayList<ArrayList<Integer>> comps = new ArrayList<>(); for (int i = 0; i < 7; i++) { if (!visited[i]) { ArrayList<Integer> comp = new ArrayList<>(); bfs(i, comp, visited); comps.add(comp); } } System.out.println(comps); } public void bfs(int src, ArrayList<Integer> comp, boolean visited[]) { Queue<Integer> q = new LinkedList<>(); q.add(src); while (q.size() > 0) { int rem = q.poll(); if (visited[rem]) continue; visited[rem] = true; comp.add(rem); for (Edge edge : graph[rem]) { if (!visited[edge.nbr]) { q.add(edge.nbr); } } } } class Pair implements Comparable<Pair> { int v; int wsf; String psf; Pair(int v, String psf) { this.v = v; this.psf = psf; } Pair(int v, String psf, int w) { this.v = v; this.psf = psf; wsf = w; } public int compareTo(Pair pair) { return this.wsf - pair.wsf; } } } class Edge { int nbr; int wt; Edge(int n, int w) { nbr = n; wt = w; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
772dc712420b5f09599028565e6ef0d2
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class ReverseAndConcatenate { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); for (int c = 0; c < t; c++) { int n = sc.nextInt(); int k = sc.nextInt(); StringBuilder temp = new StringBuilder(sc.next()); StringBuilder s = new StringBuilder(temp); if (k == 0) { pw.println(1); } else { if (temp.reverse().toString().equals(s.toString())) { pw.println(1); } else { pw.println(2); } } } sc.close(); pw.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
830559bca4c48b32004011c9751b2a3d
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class codeForces { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); Set<Character> st = new HashSet<>(); for (int i = 0; i < s.length(); i++) { st.add(s.charAt(i)); continue; } if (st.size() == 1 || k == 0) { System.out.println(1); continue; } int x = 0; for (int i = 0, j = s.length() - 1; j > i; i++, j--) { if (s.charAt(i) != s.charAt(j)) { x = 1; break; } else { x = 0; } } if (x == 1) { System.out.println(2); } else { System.out.println(1); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
1e076f6a5838a13452c904cbe14de45d
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-- > 0){ int n = scn.nextInt(); int k = scn.nextInt(); String s = scn.next(); solve(k, s); } } public static void solve(int k, String s){ int count = 1; while(k-->0){ String rev = reverse(s); if(!s.equals(rev)){ s += rev; count++; }else { break; } } System.out.println(count); } public static String reverse(String s){ StringBuilder sb = new StringBuilder(s); sb.reverse(); return sb.toString(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
c6ba0d6c30b73d14c465867a6c165fae
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
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 Solution{ public static boolean isPalindrome(String s){ int i=0; int j=s.length()-1; while(true){ if(i>=j){ return true; } else if(s.charAt(i)!=s.charAt(j)){ return false; } i++; j--; } } public static void main(String[] args){ FastReader sc=new FastReader(); HashSet<String> hash=new HashSet(); int n=sc.nextInt(); for(int j=0;j<n;j++){ int p=sc.nextInt(); int k=sc.nextInt(); String s=sc.nextLine(); if(k==0 || isPalindrome(s)){ System.out.println(1); } else{ System.out.println(2); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
4d0e4651453f1f41dcf7d3e88c24af2f
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; import java.util.Map.Entry; public class MacroTemplates { static class pair { int e1,e2; pair(int e1,int e2) { this.e1=e1; this.e2=e2; } } public static void main(String hi[]) throws Exception { PrintWriter out = new PrintWriter(System.out); FastReader ob=new FastReader(); int T = ob.nextInt(); StringBuilder sb = new StringBuilder(); while(T-->0) { int n=ob.nextInt(); int k=ob.nextInt(); String str=ob.next(); if(k == 0){ System.out.println("1"); } else { StringBuilder s2=new StringBuilder(str); StringBuilder s1=new StringBuilder(str); s1.reverse(); if(s1.toString().equals(s2.toString())) { System.out.println("1"); } else{ System.out.println("2"); } } out.flush(); } } static int isCheck(long s) { double k = (-1.0 + Math.sqrt(1 + 8 * s)) / 2; if (Math.ceil(k) == Math.floor(k)) { return (int)k; } else { return -1; } } static String middle(int i) { String str=""; for(int j=1;j<=i;j++) { str+="()"; } return str; } static String start(int i,int n) { String str=""; for(int j=1;j<=(n-i);j++) { str+="("; } return str; } static String close(int i,int n) { String str=""; for(int j=1;j<=(n-i);j++) { str+=")"; } return str; } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
b9933d557e8cb4ec5b09bf0e2438ea26
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ReverseAndConcatenate { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(bf.readLine()); while (t-- > 0){ String s[] = bf.readLine().split(" "); int n = Integer.parseInt(s[0]); int k = Integer.parseInt(s[1]); String str = bf.readLine(); System.out.println(solve(n,k,str)); } } private static int solve(int n, int k, String str) { if (k==0 || isPalindrome(str)) return 1; // if (k>=2) else return 2; } 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; } i++; j--; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
2d367264d0f422443eb2752979ac7a29
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { // write your code here Scanner scan= new Scanner(System.in); int t=scan.nextInt(); while( t!=0 ) { int a,b; a=scan.nextInt(); b=scan.nextInt(); String s=scan.next(); int flag=0; for(int i=0;i<a/2;i++){ if(s.charAt(i)!=s.charAt(a-i-1)){ flag=1; break; } } if(b==0){ System.out.println(1); } else if(flag==1){ System.out.println(2); } else System.out.println(1); t--; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
1621d991c8f79515e7bde1237d4eedef
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) { Scanner scn = new Scanner (System.in); int n= scn.nextInt(); while(n>0){ int n1 = scn.nextInt(); int n2 = scn.nextInt(); scn.nextLine(); String s = scn.nextLine(); boolean b= true; int x =s.length(); for(int i=0,j=x-1;i<s.length()/2;i++,j--){ if(s.charAt(j)==s.charAt(i)){ continue; } else{ b=false; break; } } // System.out.println(b); if(n2==0 || b==true){ System.out.println("1"); } else{ System.out.println("2"); } n--; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
68604e7b497b3261e483ea299554ae26
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class codechef { 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++) { int x=sc.nextInt(); int y=sc.nextInt(); sc.nextLine(); String s=sc.nextLine(); int j=0,k=s.length()-1; while(j<k){ if(s.charAt(j)!=s.charAt(k)) break; j++;k--; } if(j==k||j-k==1){ System.out.println("1"); } else{ if(y==0) System.out.println("1"); else if(y==1) System.out.println("2"); else System.out.println("2"); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
41e2c954b9ca63bd3ff77c2c8ec03dfd
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
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.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[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); int x = 1; for (int i = 0; i < n; i++) { if (s.charAt(i) != s.charAt(n - 1 - i)) { x = 0; break; } } if (k == 0) { System.out.println(1); } else if (x == 1) { System.out.println(1); } else { System.out.println(2); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
0ec4b5fc9ee55d2c8185f0c31190a13f
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.StringTokenizer; public class TaskA { public static void main(String[] args) { new TaskA().run(); } private void run() { FastScanner scanner = new FastScanner(); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); int k = scanner.nextInt(); String s = scanner.next(); HashSet<String> set = new HashSet<>(k); iterate(s, k, set); System.out.println(set.size()); } } private void iterate(String s, int k, HashSet<String> set) { if (k <= 0) { set.add(s); return; } String rev = rev(s); if (rev.equals(s)){ set.add(rev); return; } iterate(s + rev, k - 1, set); iterate(rev + s, k - 1, set); } private String rev(String s) { return new StringBuilder().append(s).reverse().toString(); } 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()); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
1852a0db6ef751b372ef210ca4d5eb8d
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; 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 FastReader obj = new FastReader(); public static PrintWriter out = new PrintWriter(System.out); public static void sort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } public static void revsort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr, Collections.reverseOrder()); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } //Cover the small test cases like for n=1 . public static class pair { long a; long b; pair(long x, long y) { a = x; b = y; } } public static long l() { return obj.nextLong(); } public static int i() { return obj.nextInt(); } public static String s() { return obj.next(); } public static long[] l(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = l(); return arr; } public static int[] i(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = i(); return arr; } public static long ceil(long a, long b) { return (a + b - 1) / b; } public static void p(long val) { out.println(val); } public static void p(String s) { out.println(s); } public static void pl(long[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void p(int[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void sortpair(Vector<pair> arr) { //ascending just change return 1 to return -1 and vice versa to get descending. //compare based on value of pair.a arr.sort(new Comparator<pair>() { public int compare(pair o1, pair o2) { long val = o1.a - o2.a; if (val == 0) return 0; else if (val > 0) return 1; else return -1; } }); } // Take of the small test cases such as when n=1,2 etc. // remember in case of fenwick tree ft is 1 based but our array should be 0 based. // in fenwick tree when we update some index it doesn't change the value to val but it // adds the val value in it so remember to add val-a[i] instead of just adding val. //in case of finding the inverse mod do it (biexpo(a,mod-2)%mod + mod )%mod public static void main(String[] args) { int len = i(); while (len-- != 0) { int n = i(); int m=i(); String s=s(); char[] ch=s.toCharArray(); boolean pal=true; for(int i=0;i<n;i++) { if(ch[i]!=ch[n-1-i]) { pal=false; break; } } if(pal || m==0)out.println(1); else out.println(2); } out.flush(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
8e8f98e151aee1891179926eb27f5185
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class New { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int testCases = Integer.parseInt(br.readLine()); while(testCases-->0){ String input[]=br.readLine().split(" "); int n=Integer.parseInt(input[0]); int k=Integer.parseInt(input[1]); String s=br.readLine(); int i=0; int j=s.length()-1; while(i<j) { if(s.charAt(i)!=s.charAt(j)) break; i++; j--; } if(i>=j||k==0) out.println(1); else out.println(2); } out.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
dbc4b0acd96913747f9e119cd1e42d69
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); int b = sc.nextInt(); String s = sc.next(); StringBuilder sb = new StringBuilder(s); sb.reverse(); String str = String.valueOf(sb); if (s.length() == 1) { System.out.println(1); } else if (b == 0) { System.out.println(1); } else if (str.equals(s)) { System.out.println(1); } else { System.out.println(2); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
5e7997bea4e0d005f84583300bac4eb4
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.*; public class b { public static void main(String []args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int k=s.nextInt(); String str=s.next(); Set<Character>set=new HashSet<>(); for(int i=0;i<n;i++) { set.add(str.charAt(i)); } if(set.size()==1||k==0||is(str)) System.out.println(1); else System.out.println(2); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } public static boolean is(String s) { int left=0; int right=s.length()-1; while(left<right) { if(s.charAt(left)!=s.charAt(right)) return false; left++; right--; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
b3ebbe112db3b1cc2e14c9daa40be9ac
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.*; public class b { public static void main(String []args) { MyScanner s=new MyScanner(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int k=s.nextInt(); String str=s.next(); Set<Character>set=new HashSet<>(); for(int i=0;i<n;i++) { set.add(str.charAt(i)); } if(set.size()==1||k==0||is(str)) System.out.println(1); else System.out.println(2); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } public static boolean is(String s) { int left=0; int right=s.length()-1; while(left<right) { if(s.charAt(left)!=s.charAt(right)) return false; left++; right--; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
adc981879991a79800797317fa0b2f84
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class a { public static void main(String[] args) { int t; StringBuilder sb = new StringBuilder(); Scanner sc = new Scanner(System.in); t = sc.nextInt(); while(t-- > 0) { int n,m; n = sc.nextInt(); m = sc.nextInt(); String s = sc.next(); int i = 0; int j = s.length()-1; boolean pal = true; while(i<j) { if(s.charAt(i)!=s.charAt(j)){ pal = false; } i++; j--; } if(m==0 || pal==true){ sb.append(1+"\n"); } else{ sb.append(2+"\n"); } } System.out.print(sb); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
44931b7523ce4efa8122e5c35472c143
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class a { public static void main(String[] args) { // TODO Auto-generated method stub int t; StringBuilder sb = new StringBuilder(); Scanner sc = new Scanner(System.in); t = sc.nextInt(); while(t-- > 0) { int n,m; n = sc.nextInt(); m = sc.nextInt(); String s = sc.next(); int i = 0; int j = s.length()-1; boolean pal = true; while(i<j) { if(s.charAt(i)!=s.charAt(j)) pal = false; i++;j--; } if(m==0 || pal==true) sb.append(1+"\n"); else sb.append(2+"\n"); } System.out.print(sb); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
3078a7556637e9c8fe5f671826214963
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import com.sun.source.tree.Tree; import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { static Kattio io; static long mod = 998244353, inv2 = 499122177; static { io = new Kattio(); } public static void main(String[] args) { int t = io.nextInt(); for (int i = 0; i < t; i++) { solve(); } io.close(); } private static void solve() { int n = io.nextInt(), op = io.nextInt(); String s = io .next(); String rev = new StringBuilder(s).reverse().toString(); if(op==0) {io.println(1); return;} if(rev.equals(s)){ io.println(1); }else{ io .println(2); } } static class pair implements Comparable<pair> { private final int x; private final int y; public pair(final int x, final int y) { this.x = x; this.y = y; } @Override public String toString() { return "pair{" + "x=" + x + ", y=" + y + '}'; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof pair)) { return false; } final pair pair = (pair) o; if (x != pair.x) { return false; } return y == pair.y; } @Override public int hashCode() { int result = x; result = (int) (31 * result + y); return result; } @Override public int compareTo(pair pair) { if(this.y==pair.y) return Integer.compare(this.x, pair.x); return Integer.compare(this.y, pair.y); } } 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()); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
7aee8b83d61d4764b1825beb66ded8bd
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import com.sun.source.tree.Tree; import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { static Kattio io; static long mod = 998244353, inv2 = 499122177; static { io = new Kattio(); } public static void main(String[] args) { int t = io.nextInt(); for (int i = 0; i < t; i++) { solve(); } io.close(); } private static void solve() { int n = io.nextInt(), op = io.nextInt(); String s = io .next(); String rev = new StringBuilder(s).reverse().toString(); if(op==0) {io.println(1); return;} if(rev.equals(s)){ io.println(1); }else{ io .println(2); } } static class pair implements Comparable<pair> { private final int x; private final int y; public pair(final int x, final int y) { this.x = x; this.y = y; } @Override public String toString() { return "pair{" + "x=" + x + ", y=" + y + '}'; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof pair)) { return false; } final pair pair = (pair) o; if (x != pair.x) { return false; } return y == pair.y; } @Override public int hashCode() { int result = x; result = (int) (31 * result + y); return result; } @Override public int compareTo(pair pair) { if(this.y==pair.y) return Integer.compare(this.x, pair.x); return Integer.compare(this.y, pair.y); } } 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()); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
fd3b712d436892963d6ef1cd16455c51
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class Q1634A { static int mod = (int) (1e9 + 7); static void solve() { int n=i(); int k=i(); String s=s(); boolean ispal=isPaling(s); if(ispal || k==0){ System.out.println(1); }else{ System.out.println(2); } } public static boolean isPaling(String s){ int i=0; int j=s.length()-1; char []arr=s.toCharArray(); while(i<j){ if(arr[i]!=arr[j]){ return false; } i++; j--; } return true; } public static void main(String[] args) { int test = i(); while (test-- > 0) { solve(); } } // -----> POWER ---> long power(long x, long y) <---- power // -----> LCM ---> long lcm(long x, long y) <---- lcm // -----> GCD ---> long gcd(long x, long y) <---- gcd // -----> NCR ---> long ncr(int n, int r) <---- ncr // -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<-- // -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<- // tempstart static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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 Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static InputReader in = new InputReader(System.in); public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
40232b6a724c0ffbd650f83c720c9b03
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class B { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); sb.append(solve(n,k,s)+"\n"); } System.out.println(sb.toString()); } private static int solve(int n, int k, String s) { StringBuilder sb =new StringBuilder(); sb.append(s).reverse(); if(k==0) return 1; if(s.equals(sb.toString())) { return 1; }else { return 2; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
d9dd04c86b92ebbad6458882eeba9d91
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); A solver = new A(); int t = sc.nextInt(); solver.solve(t, sc, out); out.close(); } private void solve(int t, InputReader sc, PrintWriter out) { while(t-->0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); out.println(solve(n,k,s)); } } private int solve(int n, int k, String s) { StringBuilder sb =new StringBuilder(); sb.append(s).reverse(); if(k==0) return 1; if(s.equals(sb.toString())) { return 1; }else { return 2; } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
cab8daab38b9a64615472fe163942c3a
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.Vector; public class Main { static int mod = (int) (1e9) + 7; /* ======================DSU===================== */ static class dsu { static int parent[], n;// min[],value[]; static long size[]; dsu(int n) { parent = new int[n + 1]; size = new long[n + 1]; // min=new int[n+1]; // value=new int[n+1]; Main.dsu.n = n; makeSet(); } static void makeSet() { for (int i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; // min[i]=i; } } static int find(int a) { if (parent[a] == a) return a; else { return parent[a] = find(parent[a]);// Path Compression } } static void union(int a, int b) { int setA = find(a); int setB = find(b); if (setA == setB) return; if (size[setA] >= size[setB]) { parent[setB] = setA; size[setA] += size[setB]; } else { parent[setA] = setB; size[setB] += size[setA]; } } } /* ======================================================== */ static class Pair<X extends Number, Y extends Number> implements Comparator<Pair> { X x; Y y; // Constructor public Pair(X x, Y y) { this.x = x; this.y = y; } public Pair() { } @Override public int compare(Main.Pair o1, Main.Pair o2) { return ((int) (o1.y.intValue() - o2.y.intValue()));// Ascending Order based on 'y' } } /* ===============================Tries================================= */ static class TrieNode { private HashMap<Character, TrieNode> children = new HashMap<>(); public int size; boolean endOfWord; public void putChildIfAbsent(char ch) { children.putIfAbsent(ch, new TrieNode()); } public TrieNode getChild(char ch) { return children.get(ch); } } static private TrieNode root; public static void insert(String str) { TrieNode curr = root; for (char ch : str.toCharArray()) { curr.putChildIfAbsent(ch); curr = curr.getChild(ch); curr.size++; } // mark the current nodes endOfWord as true curr.endOfWord = true; } public static int search(String word) { TrieNode curr = root; for (char ch : word.toCharArray()) { curr = curr.getChild(ch); if (curr == null) { return 0; } } // size contains words starting with prefix- word return curr.size; } public boolean delete(TrieNode current, String word, int index) { if (index == word.length()) { // when end of word is reached only delete if currrent.endOfWord is true. if (!current.endOfWord) { return false; } current.endOfWord = false; // if current has no other mapping then return true return current.children.size() == 0; } char ch = word.charAt(index); TrieNode node = current.children.get(ch); if (node == null) { return false; } boolean shouldDeleteCurrentNode = delete(node, word, index + 1); // if true is returned then delete the mapping of character and trienode // reference from map. if (shouldDeleteCurrentNode) { current.children.remove(ch); // return true if no mappings are left in the map. return current.children.size() == 0; } return false; } /* ================================================================= */ static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static Pair<Integer, Integer> lowerBound(long[] a, long x) { // x is the target, returns lowerBound. If not found // return -1 int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] == x) { return new Pair<>(m, 1); } if (a[m] >= x) r = m; else l = m; } return new Pair<>(r, 0); } static Pair<Integer, Integer> upperBound(long[] a, long x) {// x is the target, returns upperBound. If not found int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] == x) { return new Pair<>(m, 1); } if (a[m] <= x) l = m; else r = m; } return new Pair<>(l + 1, 0); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } public double binPow(double x, int n) {// binary exponentiation with negative power as well if (n == 0) return 1.0; double binPow = binPow(x, n / 2); if (n % 2 == 0) { return binPow * binPow; } else { return n > 0 ? (binPow * binPow * x) : (binPow * binPow / x); } } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } /* * ===========Modular Operations================== */ static long modPower(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPower(n, p - 2, p); } static long modAdd(long a, long b) { return ((a + b + mod) % mod); } static long modMul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long[] fac = new long[200000 + 5]; static long[] invFac = new long[200000 + 5]; static long nCrModPFermat(int n, int r) { if (r == 0) return 1; // return (fac[n] * modInverse(fac[r], mod) % mod * modInverse(fac[n - r], mod) // % mod) % mod; return (fac[n] * invFac[r] % mod * invFac[n - r] % mod) % mod; } /* * =============================================== */ static void ruffleSort(long[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); int temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static boolean isVowel(char ch) { return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } static String binString32(int n) { StringBuilder str = new StringBuilder(""); String bin = Integer.toBinaryString(n); if (bin.length() != 32) { for (int k = 0; k < 32 - bin.length(); k++) { str.append("0"); } str.append(bin); } return str.toString(); } static class sparseTable { public static int st[][]; public static int log = 4; static int func(int a, int b) {// make func as per question(here min range query) return (int) gcd(a, b); } void makeTable(int n, int a[]) { st = new int[n][log]; for (int i = 0; i < n; i++) { st[i][0] = a[i]; } for (int j = 1; j < log; j++) { for (int i = 0; i + (1 << j) <= n; i++) { st[i][j] = func(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } } } static int query(int l, int r) { int length = r - l + 1; int k = 0; while ((1 << (k + 1)) <= length) { k++; } return func(st[l][k], st[r - (1 << k) + 1][k]); } static void printTable(int n) { for (int j = 0; j < log; j++) { for (int i = 0; i < n; i++) { System.out.print(st[i][j] + " "); } System.out.println(); } } } /* * ====================================Main================================= */ // static int st[], a[]; // static void buildTree(int treeIndex, int lo, int hi) { // if (hi == lo) { // st[treeIndex] = a[lo]; // return; // } // int mid = (lo + hi) / 2; // buildTree(treeIndex * 2 + 1, lo, mid); // buildTree(treeIndex * 2 + 2, mid + 1, hi); // st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]); // } // static void update(int treeIndex, int lo, int hi, int arrIndex, int val) { // if (hi == lo) { // st[treeIndex] = val; // a[arrIndex] = val; // return; // } // int mid = (hi + lo) / 2; // if (mid < arrIndex) { // update(treeIndex * 2 + 2, mid + 1, hi, arrIndex, val); // } else { // update(treeIndex * 2 + 1, lo, mid, arrIndex, val); // } // st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]); // } // static int query(int treeIndex, int lo, int hi, int l, int r) { // if (l <= lo && r >= hi) { // return st[treeIndex]; // } // if (l > hi || r < lo) { // return 0; // } // int mid = (hi + lo) / 2; // return query(treeIndex * 2 + 1, lo, mid, l, Math.min(mid, r)); // } // static int merge(int a, int b) { // return a + b; // } public static long findKthPositive(long[] A, long k) { int l = 0, r = A.length, m; while (l < r) { m = (l + r) / 2; if (A[m] - 1 - m < k) l = m + 1; else r = m; } return l + k; } static int[] z_function(char ar[]) { int[] z = new int[ar.length]; z[0] = ar.length; int l = 0; int r = 0; for (int i = 1; i < ar.length; i++) { if (r < i) { l = i; r = i; while (r < ar.length && ar[r - l] == ar[r]) r++; z[i] = r - l; r--; } else { int k = i - l; if (z[k] < r - i + 1) { z[i] = z[k]; } else { l = i; while (r < ar.length && ar[r - l] == ar[r]) r++; z[i] = r - l; r--; } } } return z; } static void mergeSort(int a[]) { int n = a.length; if (n >= 2) { int mid = n / 2; int left[] = new int[mid]; int right[] = new int[n - mid]; for (int i = 0; i < mid; i++) { left[i] = a[i]; } for (int i = mid; i < n; i++) { right[i - mid] = a[i]; } mergeSort(left); mergeSort(right); mergeSortedArray(left, right, a); } } static void mergeSortedArray(int left[], int right[], int a[]) { int i = 0, j = 0, k = 0, n = left.length, m = right.length; while (i != n && j != m) { if (left[i] < right[j]) { a[k++] = left[i++]; } else { a[k++] = right[j++]; } } while (i != n) { a[k++] = left[i++]; } while (j != m) { a[k++] = right[j++]; } } // BINARY SEARCH // count of set bits in a particular position // suffix max array/ suffix sum array/ prefix 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; } int[] intArr(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] longArr(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } static FastReader f = new FastReader(); static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out)); static long dp[][]; public static void main(String args[]) throws Exception { int t = 1; t = f.nextInt(); int tc = 1; // fac[0] = 1; // for (int i = 1; i <= 200000; i++) { // fac[i] = fac[i - 1] * i % mod; // invFac[i] = modInverse(fac[i], mod); // } while (t-- != 0) { int n = f.nextInt(); int k=f.nextInt(); String s=f.next(); if(isPal(s,n) || k==0){ w.write("1\n"); }else{ w.write("2\n"); } } w.flush(); } static boolean isPal(String s, int n){ StringBuilder sb=new StringBuilder(s); return sb.reverse().toString().equals(s); } } /* abaaba abaabaabaaba */
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
4df2768fbd6f726d9d596f46dbd42537
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class ReverseAndConcatenate { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- != 0) { sc.nextInt(); System.out.println(solve(sc.nextInt(), sc.next())); } sc.close(); } private static int solve(int k, String s) { if (k == 0 || isPalindrome(s)) { return 1; } return 2; } private static boolean isPalindrome(String text){ String reverse = reverse(text); if(text.equals(reverse)) { return true; } return false; } private static String reverse(String input) { if(input == null || input.isEmpty()) { return input; } return input.charAt(input.length()- 1) + reverse(input.substring(0, input.length() - 1)); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
c47bde174544cb2319ecca02ab3d125a
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
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.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } } public static void main(String[] args){ FastReader in = new FastReader(); int t = in.nextInt(); StringBuilder sb = new StringBuilder(); while(t-->0){ int n = in.nextInt(); int k = in.nextInt(); String s = in.next(); if(k==0 ||n==1|| isPalindrome(s)) sb.append(1+"\n"); else sb.append(2+"\n"); } System.out.println(sb); } public static boolean isPalindrome(String st){ int i=0; int j=st.length()-1; while(i<j){ if(st.charAt(i)!=st.charAt(j)) return false; i++;j--; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
6bf39ac322ae82ee5c7024aba1b712da
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class practice { public static void main(String[] args) { Scanner scan = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int t = scan.nextInt(); while (t --> 0) { int n = scan.nextInt(); int k = scan.nextInt(); String s = scan.next(); boolean flag = false; for (int i = 0; i < n / 2; i++) { if (s.charAt(i) != s.charAt(n - i - 1)) { flag = true; break; } } sb.append((!flag || k < 1 ? 1 : 2) + "\n"); } System.out.println(sb.toString().trim()); scan.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
5abfa4c924f2936ca851352102295143
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Reverse_and_Concatenate { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ int n = s.nextInt(); int k = s.nextInt(); String str = s.next(); if(k==0){ System.out.println(1); continue; } // Check palindrome int p1 = 0, p2 = str.length()-1, f = 1; while(p1<p2){ if(str.charAt(p1)!=str.charAt(p2)){ f = 0; break; } p1++; p2--; } // Final result if(f==1){ System.out.println(1); continue; }else System.out.println(2); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
40f98b7072d2b7fd57e3c952176c3ebb
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Reverse_and_Concatenate { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ int n = s.nextInt(); int k = s.nextInt(); String str = s.next(); if(k==0){ System.out.println(1); continue; } // Check palindrome int p1 = 0, p2 = str.length()-1, f = 1; while(p1<p2){ if(str.charAt(p1)!=str.charAt(p2)){ f = 0; break; } p1++; p2--; } if(f==1){ System.out.println(1); continue; }else System.out.println(2); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
4aa1e0bb2f5e593da1da20da041170c2
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class ReverseAndConcatenate { String rev(String x){ String r = ""; for (int i=x.length()-1;i>=0;i--) { r=r+x.charAt(i); } return r; } public static void main(String[] args) { Scanner s=new Scanner (System.in); // String a=s.next(); ReverseAndConcatenate r=new ReverseAndConcatenate(); int c; String first ; String second; // System.out.println(r.rev(a)); int t=s.nextInt(); int result[]=new int[t]; for (int i=0;i<t;i++){ c=0; int n=s.nextInt(); int k=s.nextInt(); String a=s.next(); while (k>0) { first =r.rev(a)+a; second =a+r.rev(a); if (!first.equalsIgnoreCase(second)){ c+=2; a=r.rev(a)+a; k--;} else { break; } //System.out.println("......................"); // System.out.println(first+" "+second); } if (c==0) c++; result[i]=c; } for (int i=0;i<t;i++) { System.out.println(result[i]); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
10a69b9dfc520ac9b8ba4cb678140a7a
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int testcase= scan.nextInt(); int n=0,k=0; String s=""; for (int i=0;i<testcase;i++){ int counter=0; n=scan.nextInt(); k= scan.nextInt(); scan.nextLine(); s= scan.nextLine(); if(k==0){ System.out.println(1); continue; }else{ int flg=0; for(int r=0,l=n-1;r<l;r++,l--){ if(s.charAt(r)!=s.charAt(l)){ flg=1; break; } } if(flg==1){ System.out.println(2); }else{ System.out.println(1); } } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
282a1e5b7b68b8def93d473d9631e211
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; /** * Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools * <p> * To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other */ public class Main { public static void main(String[] args) { FastScanner fastScanner = new FastScanner(); OutputStream outputStream = System.out; try (PrintWriter out = new PrintWriter(outputStream)) { int numberOfTestCases = fastScanner.nextInt(); for (int testCase = 0; testCase < numberOfTestCases; testCase++) { // read inputs int stringLength = fastScanner.nextInt(); int numberOfOperations = fastScanner.nextInt(); String s = fastScanner.next(); Main main = new Main(); out.println(main.countStrings(s, numberOfOperations)); } } } public int countStrings(String original, int numberOfOperations) { Set<String> previousSolutions = new HashSet<>(); previousSolutions.add(original); if (numberOfOperations > 0) { // check if our original string is a palindrome or not Set<String> nextSolutions = new HashSet<>(); for (String previous : previousSolutions) { StringBuilder stringBuilder = new StringBuilder(previous); String reversed = stringBuilder.reverse().toString(); nextSolutions.add(previous + reversed); nextSolutions.add(reversed + previous); } // replace previous solutions previousSolutions.clear(); previousSolutions.addAll(nextSolutions); } return previousSolutions.size(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
c4f82a157d50338a64dc0e94d9ebb6ce
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; public class codeforce{ public static boolean isPalindrome(String s){ int i=0,j=s.length()-1; while(i<=j){ if(s.charAt(i++) != s.charAt(j--)) return false; } return true; } public static int helper(String s,int n,int k){ if(k==0) return 1; if(isPalindrome(s)) return 1; return 2; } 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 t = Integer.parseInt(br.readLine()); while(t-- > 0){ String[] in = br.readLine().split(" "); int n = Integer.parseInt(in[0]); int k = Integer.parseInt(in[1]); String s = br.readLine(); bw.write(helper(s,n,k)+"\n"); } bw.flush(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
fb95aacacbc380f5e0be9fb7ab6d9a95
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { 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; } } static class TreeMultiSet<T> implements Iterable<T> { private final TreeMap<T,Integer> map; private int size; public TreeMultiSet(){map=new TreeMap<>(); size=0;} public TreeMultiSet(boolean reverse) { if(reverse) map=new TreeMap<>(Collections.reverseOrder()); else map=new TreeMap<>(); size=0; } public void clear(){map.clear(); size=0;} public int size(){return size;} public int setSize(){return map.size();} public boolean contains(T a){return map.containsKey(a);} public boolean isEmpty(){return size==0;} public Integer get(T a){return map.getOrDefault(a,0);} public void add(T a, int count) { int cur=get(a);map.put(a,cur+count); size+=count; if(cur+count==0) map.remove(a); } public void addOne(T a){add(a,1);} public void remove(T a, int count){add(a,Math.max(-get(a),-count));} public void removeOne(T a){remove(a,1);} public void removeAll(T a){remove(a,Integer.MAX_VALUE-10);} public T ceiling(T a){return map.ceilingKey(a);} public T floor(T a){return map.floorKey(a);} public T first(){return map.firstKey();} public T last(){return map.lastKey();} public T higher(T a){return map.higherKey(a);} public T lower(T a){return map.lowerKey(a);} public T pollFirst(){T a=first(); removeOne(a); return a;} public T pollLast(){T a=last(); removeOne(a); return a;} public Iterator<T> iterator() { return new Iterator<>() { private final Iterator<T> iter = map.keySet().iterator(); private int count = 0; private T curElement; public boolean hasNext(){return iter.hasNext()||count>0;} public T next() { if(count==0) { curElement=iter.next(); count=get(curElement); } count--; return curElement; } }; } } static long abs(long x){ if(x<0) x*=-1; return x; } static boolean checkPalindorm(String x) { for(int i=0;i<x.length();i++) { if(x.charAt(i) != x.charAt(x.length() -1 - i)) { return false; } } return true; } public static void main(String[] args) { MyScanner scanner = new MyScanner(); int t = scanner.nextInt(); while(t-- != 0){ int n = scanner.nextInt(); int op = scanner.nextInt(); String x = scanner.nextLine(); boolean ispalin = checkPalindorm(x); if(op == 0){ System.out.println(1); }else if(ispalin) { System.out.println(1); }else{ System.out.println(2); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 11
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
71f249dee213ac8985e40c5c6e0b3ade
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); for (int tt = 0; tt < t; tt++) { int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); boolean isPalindrome = new StringBuilder(s).reverse().toString().equals(s); if (isPalindrome || m == 0) { System.out.println(1); } else { if (m > 0) { System.out.println(2); } } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 17
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
b88724e91d021046d1853a406139f769
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc-- > 0){ int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); if(k == 0){ System.out.println(1); continue; } boolean isp = true; int l = 0; int hi = n-1; while(l < hi){ if(s.charAt(l) != s.charAt(hi)){ isp = false; break; } l++; hi--; } if(isp) System.out.println(1); else System.out.println(2); // System.out.println(ans); } sc.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 17
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
4e5bcea761e3383da01c16bd6a742281
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static int arr[] = new int[100001]; public static int prev[] = new int[100001]; public static int odd[] = new int[100001]; public static int even[] = new int[100001]; 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; } 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(); } } public static void main(String[] args) { int t; Scanner scanner = new Scanner(System.in); t = scanner.nextInt(); while(t>0){ int n = scanner.nextInt(); int k = scanner.nextInt(); scanner.nextLine(); String s = scanner.nextLine(); int pal = 1; for(int i=0;i<n;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ pal = 0; } } if(pal==1 || k==0){ System.out.println(1); } else { System.out.println(2); } t--; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 17
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
e4b41092fc36210984077e74cc1b2dcb
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int k = in.nextInt(); String st = in.next(); if (k <= 0) { out.println(1); } else { int ans = 2; boolean isPalindrome = true; int l = 0, r = st.length() - 1; while (l <= r) { if (st.charAt(l) != st.charAt(r)) { isPalindrome = false; break; } l++; r--; } if (isPalindrome) { ans--; } out.println(ans); } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } String nextLine() throws Exception{ String str = ""; try{ str = reader.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
fed2a495f940ab709abc4cb469b7d723
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class MyClass { public static void main(String[]args) { Scanner scan=new Scanner(System.in); StringBuilder sb=new StringBuilder(); int test=scan.nextInt(); for(int z=0;z<test;z++) { int n=scan.nextInt(),k=scan.nextInt(); String input=scan.next(); if(k==0) sb.append("1\n"); else { if(isPalindrome(input,0,input.length()-1)) { sb.append("1\n"); } else { sb.append("2\n"); } } } System.out.println(sb.toString().substring(0,sb.toString().length()-1)); } public static boolean isPalindrome(String input,int low,int high) { if(low>=high)return true; else return input.charAt(low)==input.charAt(high) && isPalindrome(input,low+1,high-1); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
c3483b921d39557be163f7e778615d5e
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class cf22 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); for (int i=0;i<a;i++){ int n=in.nextInt(); int k = in.nextInt(); in.nextLine(); String str = in.nextLine(); if (k==0||k>=1&&isPalindrome(str)){ System.out.println(1);; continue; } else { System.out.println(2); continue; } } } 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; }}
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
43eb3ab66cf53aa6da59475b46515fb3
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class P1634A { public static void main(String[] args) throws IOException{ BufferedReader io = new BufferedReader(new InputStreamReader(System.in)); Integer cnt = Integer.valueOf(io.readLine()); List<Integer> rs = new ArrayList<Integer>(); while (cnt > 0) { cnt--; String[] nk = io.readLine().split(" "); Integer k = Integer.valueOf(nk[1]); StringBuffer input = new StringBuffer(io.readLine()); Set<StringBuffer> set = new HashSet<StringBuffer>(); List<StringBuffer> list = new ArrayList<StringBuffer>(); list.add(input); if (input.length() == 1 || k.equals(0)) { rs.add(1); } else { StringBuffer ori = new StringBuffer(input); StringBuffer tmp = new StringBuffer(input); StringBuffer rev = tmp.reverse(); if (equals(rev, ori)) { rs.add(1); } else { rs.add(2); } } } for (Integer integer : rs) { System.out.println(integer); } } public static boolean equals(StringBuffer sb1, StringBuffer sb2){ if(sb1 == sb2) return true; if(sb1 == null || sb2 == null) return false; boolean isEqual = true; if(sb1.length() == sb2.length()){ for(int i = 0 ; i < sb1.length(); i++){ if(sb1.charAt(i) != sb2.charAt(i)){ isEqual = false; break; } } }else{ isEqual = false; } return isEqual; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
ca468cda22e28041e48624cefaf84e67
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t,n,k; String s="",rev=""; char ch=' '; t=sc.nextInt(); while(t-->0) { n=sc.nextInt(); k=sc.nextInt(); s=sc.next(); for(int i=0;i<s.length();i++) { ch=s.charAt(i); rev=ch+rev; } if(s.equals(rev) || k==0) { System.out.println("1"); } else { System.out.println("2"); } rev=""; } // write your code here } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
1680c39ae540d82cfd82d1b6bef22431
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class practice { public static boolean isDistinct(int n) { int[] a = new int[10]; while (n != 0) { a[n % 10]++; n /= 10; } int c = 0; for (int i = 0; i < 10; i++) { if (a[i] >= 1) c++; } return (c == 4) ? true : false; } public static boolean isPalindrome(String s) { for (int i = 0; i < (s.length() + 1) / 2; i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) return false; } return true; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while (t-- > 0) { String s = sc.nextLine(); StringTokenizer st =new StringTokenizer(s); int n=Integer.parseInt(st.nextToken(" ")); int k=Integer.parseInt(st.nextToken(" ")); s = sc.next(); if (k == 0) sb.append(1 + "\n"); else if (isPalindrome(s)) sb.append(1 + "\n"); else sb.append(2 + "\n"); } System.out.println(sb); } // boolean [] col=new boolean [m], ro=new boolean [n]; // for (int i = 0; i < n; i++) { // for (int j = 0; j < m; j++) { // if(a[i][j]==1) // col[j]=ro[i]=true; // } // } // int x=0,y=0; // for(int i=0;i<m;i++) { // if(!col[i]) // x++; // } // for(int i=0;i<n;i++) { // if(!ro[i]) // y++; // } // int z = Math.min(x, y); // if (z % 2 == 1) // sb.append("Ashish" + "\n"); // else // sb.append("Vivek" + "\n"); } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
cad4d4bfe3c14f69e57391ae43d5f486
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class ReverseConcat { //-----------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; } } public static void main(String[] args) { ReverseConcat.MyScanner sc = new ReverseConcat.MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.nextLine(); Set<String> set = new HashSet<>(); StringBuilder sb = new StringBuilder(s); set.add(sb.toString()); StringBuilder sbe = new StringBuilder(sb); StringBuilder reve = new StringBuilder(sbe); reve.reverse(); if (k == 0) System.out.println(1); else { if (sbe.toString().equals(reve.toString())) { System.out.println(1); } else { System.out.println(2); } } } out.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
72c969942d4dd8ee0ca0e53ead6ae0c8
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.io.FileNotFoundException; import java.io.FileReader; import java.math.BigInteger; import java.util.Arrays; import javax.lang.model.util.ElementScanner6; public class Main { static final long M = 998244353; static final BigInteger MOD = new BigInteger(M+""); public static void main(String[] args) { FastInput sc = new FastInput(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); int j = n - 1; boolean isP = true; for(int i = 0; i<j;i++) { //System.out.println(j); if(s.charAt(i) != s.charAt(j)) { isP = false; } j--; } if(k == 0) { System.out.println("1"); continue; } if(isP) System.out.println("1"); else System.out.println("2"); } } } class Pair implements Comparable<Pair>{ long i; long j; public Pair(long i,long j) { this.i = i; this.j = j; } public Pair(){ } public int compareTo(Pair t) { if(t.i>i) return -1; else if(t.i<i) return 1; //else return 0; } } class FastInput { BufferedReader br; StringTokenizer st; public FastInput() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { System.out.println(e.toString()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { System.out.println(e.toString()); } return str; } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void sort(long[] arr) { 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 int [] readArr(int n) { int [] a = new int[n]; for(int i = 0; i<n; i++) a[i] = nextInt(); return a; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
67943bd39cdb64bff6868adb102d1394
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.io.FileNotFoundException; import java.io.FileReader; import java.math.BigInteger; import java.util.Arrays; import javax.lang.model.util.ElementScanner6; public class Main { static final long M = 998244353; static final BigInteger MOD = new BigInteger(M+""); public static void main(String[] args) { FastInput sc = new FastInput(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); int j = n - 1; boolean isP = true; for(int i = 0; i<=n/2;i++) { //System.out.println(j); if(s.charAt(i) != s.charAt(n-i-1)) { isP = false; } j--; } if(k == 0) { System.out.println("1"); continue; } if(isP) System.out.println("1"); else System.out.println("2"); } } } class Pair implements Comparable<Pair>{ long i; long j; public Pair(long i,long j) { this.i = i; this.j = j; } public Pair(){ } public int compareTo(Pair t) { if(t.i>i) return -1; else if(t.i<i) return 1; //else return 0; } } class FastInput { BufferedReader br; StringTokenizer st; public FastInput() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { System.out.println(e.toString()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { System.out.println(e.toString()); } return str; } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void sort(long[] arr) { 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 int [] readArr(int n) { int [] a = new int[n]; for(int i = 0; i<n; i++) a[i] = nextInt(); return a; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
2f5e5e43da23c5f4237f99c8dd934d59
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.io.FileNotFoundException; import java.io.FileReader; import java.math.BigInteger; import java.util.Arrays; import javax.lang.model.util.ElementScanner6; public class Main { static final long M = 998244353; static final BigInteger MOD = new BigInteger(M+""); public static void main(String[] args) { FastInput sc = new FastInput(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); StringBuilder str = new StringBuilder(s); str.reverse(); if(k == 0){ System.out.println("1"); continue; } if(s.equals(str.toString())) System.out.println("1"); else System.out.println("2"); } } } class Pair implements Comparable<Pair>{ long i; long j; public Pair(long i,long j) { this.i = i; this.j = j; } public Pair(){ } public int compareTo(Pair t) { if(t.i>i) return -1; else if(t.i<i) return 1; //else return 0; } } class FastInput { BufferedReader br; StringTokenizer st; public FastInput() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { System.out.println(e.toString()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { System.out.println(e.toString()); } return str; } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void sort(long[] arr) { 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 int [] readArr(int n) { int [] a = new int[n]; for(int i = 0; i<n; i++) a[i] = nextInt(); return a; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
377c5c86c67e74c8f40f51d4c546f144
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class Reverse_Concatenate { String rev(String x){ String reversed = new StringBuilder(x).reverse().toString(); //System.out.println(reversed); return reversed; /*String rev=""; for(int i=0;i<x.length();i++){ rev=rev+x.substring(x.length()-1-i,x.length()-i); // System.out.println(rev); } return rev; */ } public static void main(String[] args) { Scanner s1=new Scanner(System.in); Reverse_Concatenate reverse = new Reverse_Concatenate(); int t= s1.nextInt(); for(int i=0;i<t;i++){ int n,k,count=1; n=s1.nextInt(); k= s1.nextInt(); String re,x; x=s1.next(); re= reverse.rev(x); if(k==0||x.equals(re)){ count=1; //break; } else{ count=2; } /*for(int j =0;j<k;j++){ re=reverse.rev(x); if(x.equals(re)){ //count=2; break; //count++; } else{ count=count+1; } x=x+re; } */ System.out.println(count); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
95c59d3d82830f9d5347cbe4f275a6a8
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t--!=0){ int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String str = sc.nextLine(); if(isPalindrome(str) || k==0){ System.out.println(1); } else{ System.out.println(2); } } } public static boolean isPalindrome(String str){ String reverse = ""; for(int i=0;i<str.length();i++){ reverse += str.charAt(str.length()-1-i); } if(str.equals(reverse)){ return true; } return false; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
d6bacc0916095d67b02a2773cdd51ac2
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Reverse_and_concat { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); String temp=new StringBuilder().append(s).reverse().toString(); if(s.equals(temp)|| k==0) System.out.println("1"); else System.out.println("2"); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
ec3c13f452e66b74421c64efc8c9c06e
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int[] results = new int[t]; for (int i=0; i<t; i++){ int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); char[] arr = s.toCharArray(); boolean flag = true; for (int j=0; j<n; j++){ if (arr[j]!=arr[(n-1)-j]){ flag=false; break; } } if (flag || k==0){ results[i]=1; } else{ results[i]=2; } } sc.close(); for (int i=0; i<t; i++){ System.out.println(results[i]); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
db0b68ff78be9a955e23c5817edbf106
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Cf { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); String str = sc.next(); boolean pal = true; int index = 0; for (int a = str.length() - 1; index <= a; a--, index++) { if (str.charAt(a) != str.charAt(index)) { pal = false; break; } } if (k == 0) { System.out.println(1); continue; } if (pal) { System.out.println(1); } else { System.out.println(2); } } } } // 4 2 1 3 5 6
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
8ab4372d05913ee8906031a86279ae64
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; public class Reverse { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int test=sc.nextInt(); while(test>0) { int n=sc.nextInt(); int k=sc.nextInt(); sc.nextLine(); String s=sc.nextLine(); boolean flag=check(s); if(flag==true || k==0) System.out.println("1"); else System.out.println("2"); test--; } } static boolean check(String s) { int n=s.length(); for(int i=0; i<n/2; i++) { if(s.charAt(i)!=s.charAt(n-i-1)) return false; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
c0748589efb08a924564c3863b1f7558
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Locale; import java.util.StringTokenizer; public class Solution implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new Solution(), "", 256 * (1L << 20)).start(); } public void run() { try { long t1 = System.currentTimeMillis(); if (true) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } Locale.setDefault(Locale.US); solve(); in.close(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } // solution void solve() throws IOException { int testCount = readInt(); for (int i=0; i<testCount;i++) { solution(); } } void solution() throws IOException { int n = readInt(); int k = readInt(); String s = readString(); if (k == 0) { out.println(1); return; } if (isPal(s)) { out.println(1); } else { out.println(2); } } boolean isPal(String s) { int last = s.length()-1; for (int i=0; i<s.length()/2; i++) { if (s.charAt(i) != s.charAt(last)) return false; --last; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
b69e913f557574da462e16faf70c5a78
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; import java.math.*; public class temp { // Let's Go!! -------------> static FastScanner sc; static PrintWriter out; public static void main(String[] args) { sc = new FastScanner(); out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = ni(); int m = ni(); StringBuilder sb = new StringBuilder(); sb.append(nln()); if(m == 0) { pn(1); continue; } if(sb.length() == 1) { pn(1); continue; } int start = 0, end = n-1; boolean flag = true; while(start < end) { if(sb.charAt(start++) != sb.charAt(end--)) flag = false; } if(m > 0 && flag == true) { pn(1); } else { pn(2); } } // -------END------- out.close(); } // <-----------------------Template ---------------------> // Static Initializations ------> static final long MOD = (long)1e9+7; static final int MAX = (int)1e5+1, INF = (int)1e9; // GCD --------> static long gcdl(long a, long b){return (b==0)?a:gcdl(b,a%b);} static int gcdi(int a, int b){return (b==0)?a:gcdi(b,a%b);} // Pair Class && Sort by First Value(Asc)-----------> static class pair<T> implements Comparable<pair>{ T first, second; pair(T first, T second) { this.first = first; this.second = second; } @Override public int compareTo(pair o) { return (Integer) this.first - (Integer) o.first; } } // Ruffle Sort static void ruffleSort(int[] a) { //Shuffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Fast I/O static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } //Quick Print Statements ----> static void p(Object o){out.print(o);} static void pn(Object o){out.println(o);} static void pni(Object o){out.println(o);out.flush();} // Quick Input Statements -----> static String n(){return sc.next();} static String nln(){return sc.nextLine();} static int ni(){return Integer.parseInt(sc.next());} static long nl(){return Long.parseLong(sc.next());} static double nd(){return Double.parseDouble(sc.next());} //Integer Array Input ----> static int[] readIntArr(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]= sc.nextInt(); return a; } //Long Array Input -----> static long[] readLongArr(int N){ long[] a = new long[N]; for(int i = 0; i<N; i++)a[i] = sc.nextLong(); return a; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
21fe30c0279dd88458e2b095e1a99c67
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class R770A { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while (t-- > 0) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); String word = st.nextToken(); solve(word, n, k); } } private static void solve(String word, int n, int k) { Set<String> set = new HashSet<>(); Set<String> newSet = new HashSet<>(); set.add(word); if (k >= 1) { for (String string : set) { set.remove(string); set.add(string + reverse(string)); set.add(reverse(string) + string); } } System.out.println(set.size()); } private static String reverse(String word) { return new StringBuilder(word).reverse().toString(); } } /* 1 1 0 1 0 1 1 0 0 0 1 */
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
17e09dfb829ac0a586ebc84407866bad
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.awt.image.ImageProducer; import java.util.*; public class Solution { static boolean prime[] = new boolean[1000001]; static Set<Long> cubes=new HashSet<>(); static { long N = 1000000000000L; // // // for(int i=1;i*i<=n;i++) // { // long x=i*i; // set.add(x); // } for (long i = 1; i * i * i <= N; i++) { cubes.add(i * i * i); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); sc.nextLine(); String str=sc.nextLine(); int start=0; int end=str.length()-1; boolean flag=true; if(k==0) { System.out.println(1); continue; } while (start<end) { if(str.charAt(start++)!=str.charAt(end--)) { flag=false; break; } } if(flag) { System.out.println(1); } else { System.out.println(2); } } } // 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 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; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
f50003d9084504647030e5bee014b31b
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
//package com.company; import java.util.*; import java.lang.*; import java.io.*; public class Buffered_Reader { public static void main(String[] args) throws IOException { BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(sc.readLine()); while (t-->0) { String[] arr = sc.readLine().split(" "); String s = sc.readLine(); if (Integer.parseInt(arr[1]) == 0) out.println(1); else { StringBuilder sb = new StringBuilder(s); sb.reverse(); if (s.equals(sb.toString())) out.println(1); else out.println(2); } } sc.close(); out.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
56c80cd3f2314f00dd17ab8e6bb9c57e
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(),k=sc.nextInt(); String s=sc.next(); StringBuilder sb=new StringBuilder(s); String p=sb.reverse().toString(); if(s.equals(p) || k==0){ System.out.println("1"); }else{ System.out.println("2"); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
44284c7f39070fbcdebe3a9de5824ce4
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.sqrt; import static java.lang.Math.floor; public class topcoder { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } static int x = -1; static int y = -1; public static int first_search(TreeNode root, TreeNode main_root) { if(root == null) return 0; int a = first_search(root.left,main_root); int b = first_search(root.right,main_root); if(a > main_root.val) x = a; if(b < main_root.val) y = b; return root.val; } public static void fix(TreeNode root, TreeNode main_root) { if(root == null) return; fix(root.left,main_root); if(root.val > main_root.val) { root.val = y; } fix(root.right,main_root); if(root.val < main_root.val); root.val = x; } public static int max(int []nums, int s, int e) { int max = Integer.MIN_VALUE; for(int i = s; i <= e; i++) { max = Math.max(max, nums[i]); } return max; } public static TreeNode new_node(int []nums, int s, int e) { int max = max(nums,s,e); TreeNode node = new TreeNode(max); return node; } public static TreeNode root; public static void res(int []nums, int s, int e) { if(s > e)return; if(root == null) { root = new_node(nums,s,e); } root.left = new_node(nums,s,e); root.right = new_node(nums,s,e); return; } static int depth(TreeNode root) { if(root == null)return 0; int a = 1+ depth(root.left); int b = 1+ depth(root.right); return Math.max(a,b); } static HashSet<Integer>set = new HashSet<>(); static void deepestLeaves(TreeNode root, int cur_depth, int depth) { if(root == null)return; if(cur_depth == depth)set.add(root.val); deepestLeaves(root.left,cur_depth+1,depth); deepestLeaves(root.right,cur_depth+1,depth); } public static void print(TreeNode root) { if(root == null)return; System.out.print(root.val+" "); System.out.println("er"); print(root.left); print(root.right); } public static HashSet<Integer>original(TreeNode root){ int d = depth(root); deepestLeaves(root,0,d); return set; } static HashSet<Integer>set1 = new HashSet<>(); static void leaves(TreeNode root) { if(root == null)return; if(root.left == null && root.right == null)set1.add(root.val); leaves(root.left); leaves(root.right); } public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) { if(s.size() != s1.size())return false; for(int a : s) { if(!s1.contains(a))return false; } return true; } static TreeNode subTree; public static void smallest_subTree(TreeNode root) { if(root == null)return; smallest_subTree(root.left); smallest_subTree(root.right); set1 = new HashSet<>(); leaves(root); boolean smallest = check(set,set1); if(smallest) { subTree = root; return; } } public static TreeNode answer(TreeNode root) { smallest_subTree(root); return subTree; } } static class pair{ long first; long second; public pair(long first, long second) { this.first = first; this.second = second; } public long compareTo(pair p) { if(first == p.first)return second-p.second; return first-p.first; } } static class Compare{ static void compare(ArrayList<pair>arr) { Collections.sort(arr,new Comparator<pair>() { public int compare(pair p1, pair p2) { return (int) (p1.first-p2.first); } }); } } public static HashMap<Integer,Integer>sortByValue(HashMap<Integer,Integer>hm){ List<Map.Entry<Integer,Integer>>list = new LinkedList<Map.Entry<Integer,Integer>>(hm.entrySet()); 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()); }}); 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 pairr implements Comparable<pairr>{ static Long value; Long index; public pairr(Long value, Long index) { this.value = value; this.index = index; } public int compareTo(pairr o) { return (int)(value-o.value); } } static class Key<K1, K2> { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return "[" + key1 + ", " + key2 + "]"; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static long binary_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return ar[s]; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return binary_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return binary_search(s,mid,num,ar); } return -1; } public static int index_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return s; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return index_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return index_search(s,mid,num,ar); } return -1; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static boolean digit_exists(long n) { while(n > 0) { if(n%10 == 9) return true; n = n/10; } return false; } public static int log(int n) { int c = 0; while(n > 0) { c++; n /=2; } return c; } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } static void simpleSieve(int limit, Vector<Integer> prime) { // Create a boolean array "mark[0..n-1]" and initialize // all entries of it as true. A value in mark[p] will // finally be false if 'p' is Not a prime, else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; for (int p=2; p*p<limit; p++) { // If p is not changed, then it is a prime if (mark[p] == true) { // Update all multiples of p for (int i=p*p; i<limit; i+=p) mark[i] = false; } } // Print all prime numbers and store them in prime for (int p=2; p<limit; p++) { if (mark[p] == true) { prime.add(p); } } } // Prints all prime numbers smaller than 'n' public static void segmentedSieve(int n, ArrayList<Integer>l) { // Compute all primes smaller than or equal // to square root of n using simple sieve int limit = (int) (floor(sqrt(n))+1); Vector<Integer> prime = new Vector<>(); simpleSieve(limit, prime); // Divide the range [0..n-1] in different segments // We have chosen segment size as sqrt(n). int low = limit; int high = 2*limit; // While all segments of range [0..n-1] are not processed, // process one segment at a time while (low < n) { if (high >= n) high = n; // To mark primes in current range. A value in mark[i] // will finally be false if 'i-low' is Not a prime, // else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; // Use the found primes by simpleSieve() to find // primes in current range for (int i = 0; i < prime.size(); i++) { // Find the minimum number in [low..high] that is // a multiple of prime.get(i) (divisible by prime.get(i)) // For example, if low is 31 and prime.get(i) is 3, // we start with 33. int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); if (loLim < low) loLim += prime.get(i); /* Mark multiples of prime.get(i) in [low..high]: We are marking j - low for j, i.e. each number in range [low, high] is mapped to [0, high-low] so if range is [50, 100] marking 50 corresponds to marking 0, marking 51 corresponds to 1 and so on. In this way we need to allocate space only for range */ for (int j=loLim; j<high; j+=prime.get(i)) mark[j-low] = false; } // Numbers which are not marked as false are prime for (int i = low; i<high; i++) if (mark[i - low] == true) l.add(i); // Update low and high for next segment low = low + limit; high = high + limit; } } public static int find_indexNum(long k) { long k1 = k; int power = 0; while(k > 0) { power++; k /=2 ; } long check = (long)Math.pow(2, power-1); if(k1 == check) { return power; } // System.out.println(power); long f = (long)Math.pow(2, power-1); long rem = k1-f; return find_indexNum(rem); } public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { for(int i = 0; i < m; i++) { if(vis[i] == false) { int temp = array[i]; if(i < t_index) { vis[i] = true; } array[i] = num; array[t_index] = temp; // System.out.println(array[t_index]+" "+array[i]); break; } } } public static void rotate(int []arr,int j, int times, int m) { if(j == 0) { int temp1 = arr[0]; arr[0] = arr[times]; arr[times] = temp1; }else { int temp = arr[j]; int z = arr[0]; arr[0] = arr[times]; arr[j] = z; arr[times] = temp; } } public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { if(i-A <= 0 && i-B <= 0)return; int count = 0; for(int j = 1; j <= n; j++) { if(dp[j] >= metal[j]) { count++; } } if(count == n)return; if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { dp[i]--; dp[i-A]++; dp[i-B]++; } if(ind == 6) { // System.out.println(Arrays.toString(dp)); } recur(i-A,A,B,dp,metal,n,took,ind); recur(i-B,A,B,dp,metal,n,took,ind); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static void dfs(LinkedList<Integer>[]list, HashMap<Integer,Integer>map, int parent, int n) { Stack<Integer>st = new Stack<>(); } public static boolean pos(int n) { int i = 1; boolean pos = false; while(i*i <= n) { if(i*i*2 == n || i*i*4 == n) { pos = true; break; } i++; } if(pos)return true; return false; } static long count = 0; public static void pairs (int []ar, int s, int e) { if(e <= s)return; // System.out.println(ar[s]+" "+ar[e]+" "+s+" "+e); if(ar[e]-ar[s] == e-s) { count++; //System.out.println("sdf"); } pairs(ar,s+1,e); pairs(ar,s,e-1); } public static class Pair1 implements Comparable <Pair1>{ int value; int index; public Pair1(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pair1 o) { return o.value-value; } } public static long ways(long n) { return (n*(n-1))/2; } 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+= 6) { if(n%i == 0 || n%(i+2) == 0) return false; } return true; } static long nextPrime(long n) { boolean found = false; long prime = n; while(!found) { prime++; if(isPrime(prime)) found = true; } return prime; } public static boolean isValid(int h, int m, int hour, int minute) { int a = flip(hour / 10); if (a == -1) return false; int b = flip(hour % 10); if (b == -1) return false; int c = flip(minute / 10); if (c == -1) return false; int d = flip(minute % 10); if (d == -1) return false; if (10 * d + c >= h) return false; if (10 * b + a >= m) return false; return true; } public static int flip(int x) { if (x == 0) return 0; if (x == 1) return 1; if (x == 2) return 5; if (x == 5) return 2; if (x == 8) return 8; return -1; } static long maximum(long a, long b, long c, long d) { long m = Math.max(a, b); long m1 = Math.max(c, d); return Math.max(m1, m1); } static long minimum(long a, long b, long c,long d) { long m = Math.min(a, b); long m1 = Math.min(c, d); return Math.min(m, m1); } static long ans = 0; public static void solve1(boolean [][]vis,long [][]mat, int r, int c, int r2, int c2, int r1, int c1, int r3, int c3) { if(r > r1 || c > c1 || r > r2 || c > c2 || r1 > r3 || c1 > c3 || r3 < r2 || c3 < c2 || vis[r][c] || vis[r1][c1]|| vis[r2][c2] || vis[r3][c3]) return; vis[r][c] = true; vis[r1][c1] = true; vis[r2][c2] = true; vis[r3][c3] = true; long max = maximum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long min = minimum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long a = mat[r][c]; long b = mat[r1][c1]; long c4 = mat[r2][c2]; long d =mat[r3][c3]; long []p = {a,b,c4,d}; Arrays.sort(p); long temp = (p[2]+p[3]-p[0]-p[1]); if(r == r1 && r == r2 && r2 == r3 && r1 == r3) temp /= 2; System.out.println(Arrays.toString(p)); ans += temp; solve1(vis,mat,r+1,c,r2+1,c2,r1-1,c1,r3-1,c3); solve1(vis,mat,r,c+1,r2,c2-1,r1,c1+1,r3,c3-1); solve1 (vis,mat,r+1,c+1,r2+1,c2-1,r1-1,c1+1,r3-1,c3-1); } private static int solve(int[][] mat, int i, int c, int j, int c2, int k, int c1, int l, int c3) { // TODO Auto-generated method stub return 0; } public static int dfs(int parent, LinkedList<Integer>[]list) { for(int i : list[parent]) { if(list[parent].size() == 0) { return 0; }else { return 1 + dfs(i,list); } } return 0; } public static long answer = Integer.MAX_VALUE; public static void min_Time(int [][]dp, int i, HashSet<Integer>set, int min, int r, int c) { if(i > r) { answer = Math.min(answer, min); return; } if(min > answer)return; for(int j = i; j <= c; j++) { if(!set.contains(j)) { set.add(j); min += dp[i][j]; min_Time(dp,i+1,set,min,r,c); min -= dp[i][j]; set.remove(j); } } } public static void dp(int [][]dp, int r, int c, int o, int z, long sum) { if(r > o) { answer = Math.min(answer, sum); } if(r > o || c > z) { return; } if(sum > answer)return; sum += dp[r][c]; dp(dp,r+1,c+1,o,z,sum); sum -= dp[r][c]; dp(dp,r,c+1,o,z,sum); } static HashSet<ArrayList<Integer>>l = new HashSet<>(); public static void fourSum(Deque<Integer>ll, int i, int target, int []ar, int n) { if(ll.size() == 4) { int sum = 0; ArrayList<Integer>list = new ArrayList<>(); for(int a : ll) { sum += a; list.add(a); } if(sum == target) { Collections.sort(list); l.add(list); // System.out.println(ll); } return; } for(int j = i; j < n; j++) { ll.add(ar[j]); fourSum(ll,j+1,target,ar,n); ll.removeLast(); } } static int max_bottles(int cur, int exchange, int n){ if(cur == exchange){ cur = 0; n++; } if(n == 0)return 0; return 1+ max_bottles(cur+1,exchange,n-1); } public static void fill(int [][]mat, List<Integer>ans, int row_start, int row_end, int col_start, int col_end) { for(int i = col_start; i <= col_end; i++) { ans.add(mat[row_start][i]); } for(int i = row_start+1; i <= row_end; i++) { ans.add(mat[i][col_end]); } if(col_start == col_end)return; if(row_start == row_end)return; for(int i = col_end-1; i >= col_start; i--) { ans.add(mat[row_end][i]); } for(int i = row_end-1; i >= row_start+1; i--) { ans.add(mat[i][col_start]); } } public static void create(int [][]mat, int j, int i, int k) { if(i < 1 || j >= mat.length)return; mat[j][i] = k; create(mat,j+1,i-1,k+1); } public static long sum(int [][]mat, int x1, int y1, int x2, int y2) { long sum = 0; while(x1 <= x2) { sum += mat[x1][y1]; // System.out.println(mat[x1][y1]); x1++; } y1++; while(y1 <= y2) { sum += mat[x2][y1]; y1++; } return sum; } public static boolean allneg(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] >= 0)return false; } return true; } public static boolean allpos(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] <= 0)return false; } return true; } public static int max_pos(int []ar, int n) { int min = Integer.MAX_VALUE; for(int i = 1; i < n; i++) { if(ar[i] > 0) { break; } int a = Math.abs(ar[i]-ar[i-1]); min = Math.min(min, a); } int c = 0; boolean zero = false; TreeSet<Integer>set = new TreeSet<>(); int neg = 0; for(int i = 0; i < n; i++) { if(ar[i] <= 0) {neg++; if(ar[i] == 0) zero = true; continue;} if(ar[i] <= min) { c = 1; } } neg += c; return neg; } static final int MAX = 10000000; // prefix[i] is going to store count // of primes till i (including i). static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } static void alter(int n) { int ans = 0; boolean []vis = new boolean[n+1]; for(int i = 2; i <= n; i++){ boolean p = false; if(vis[i] == false) { for(int j = i; j <= n; j+=i) { if(vis[j] == true) { p = true; }else { vis[j] = true; } } if(!p)ans++; } } System.out.println(ans); } public static void solveDK(int []dp, int i, int D, int K) { int d = D/K; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } public static void solveKD(int []dp, int i, int D, int K) { int d = K/D; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } static int countGreater(int arr[], int n, int k) { int l = 0; int r = n - 1; // Stores the index of the left most element // from the array which is greater than k int leftGreater = n; // Finds number of elements greater than k while (l <= r) { int m = l + (r - l) / 2; // If mid element is greater than // k update leftGreater and r if (arr[m] > k) { leftGreater = m; r = m - 1; } // If mid element is less than // or equal to k update l else l = m + 1; } // Return the count of elements greater than k return (n - leftGreater); } static ArrayList<Integer>printDivisors(int n) { // Note that this loop runs till square root ArrayList<Integer>list = new ArrayList<>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) list.add(i); else // Otherwise print both list.add(i); list.add(n/i); } } return list; } static boolean isPossible(String s, String str, int i, int j) { // System.out.println(i+" "+j); int x = i; int y = j; while(i >= 0 && j < str.length()) { if(s.charAt(i) != str.charAt(j)) { break; } i--; j++; } if(j == str.length()) { System.out.println(x+" "+y); return true; } return false; } static void leftRotate(int l, int r,int arr[], int d) { for (int i = 0; i < d; i++) leftRotatebyOne(l,r,arr); } static void leftRotatebyOne(int l, int r,int arr[]) { int i, temp; temp = arr[l]; for (i = l; i < r; i++) arr[i] = arr[i + 1]; arr[r] = temp; } static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long solvetheproblem(long n, long k) { long mod = 1000000007; long ansss = 0; while(n > 0) { // Nearest power of 2<=N long p = (long)(Math.log(n) / Math.log(2));; // Now insert k^p in the answer long temp = (long)power(k,p,mod); ansss += temp; ansss %= mod; // update n n %= (long)Math.pow(2, p); } // Print the ans in sorted order return ansss%mod ; } static boolean pos (int [][]mat, int r, int c, int n, boolean [][]vis) { if(r <= 0 || c <= 0 || r > 2 || c > n )return false; if(r == 2 && c == n)return true; if(vis[r][c])return false; vis[r][c] = true; if(mat[r][c] == 1)return false; boolean a = pos(mat,r+1,c,n,vis); boolean b = pos(mat,r,c+1,n,vis); boolean d = pos(mat,r+1,c+1,n,vis); boolean e = pos(mat,r-1,c+1,n,vis); return a || b || d || e; } static long sameremdiv(long x, long y) { if(x <= y) { y = x-1; } long sq = (long)Math.sqrt(x); if(y <= sq) { y--; long ans = (y*(y+1))/2; return ans; }else { long ans = 0; long dif = y-sq; sq -= 2; if(sq > 1) ans += (sq*(sq+1))/2; long d = x/y; sq+=2; for(int i = (int)sq; i <= y; i++) { if(i > 1 ) { long temp = x/i; if(x%i < temp)temp--; ans += temp; } } return ans; } } static int binary(long []ar, long element, int s, int e) { int mid = (s+e)/2; // System.out.println(mid); if(s > e)return mid; if(ar[mid] == element) { return mid; }else if(ar[mid] > element) { return binary(ar,element,s,mid-1); }else { return binary(ar,element,mid+1,e); } } static boolean isGibbrish(HashSet<String>set, String str, int j) { StringBuilder sb = new StringBuilder(); if(j >= str.length()) { return true; } for(int i = j; i < str.length(); i++) { sb.append(str.charAt(i)); String temp = sb.toString(); if(set.contains(temp)) { boolean test = isGibbrish(set,str,i+1); if(test)return true; } } return false; } static TreeNode buildTree(TreeNode root,int []ar, int l, int r){ if(l > r)return null; int len = l+r; if(len%2 != 0)len++; int mid = (len)/2; int v = ar[mid]; TreeNode temp = new TreeNode(v); root = temp; root.left = buildTree(root.left,ar,l,mid-1); root.right = buildTree(root.right,ar,mid+1,r); return root; } public static int getClosest(int val1, int val2, int target) { if (target - val1 >= val2 - target) return val2; else return val1; } public static int findClosest(int start, int end, int arr[], int target) { int n = arr.length; // Corner cases if (target <= arr[start]) return arr[start]; if (target >= arr[end]) return arr[end]; // Doing binary search int i = start, j = end+1, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid]; /* If target is less than array element, then search in left */ if (target < arr[mid]) { // If target is greater than previous // to mid, return closest of two if (mid > 0 && target > arr[mid - 1]) return getClosest(arr[mid - 1], arr[mid], target); /* Repeat for left half */ j = mid; } // If target is greater than mid else { if (mid < n-1 && target < arr[mid + 1]) return getClosest(arr[mid], arr[mid + 1], target); i = mid + 1; // update i } } // Only single element left after search return arr[mid]; } 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; } static List<List<Integer>>res = new ArrayList<>(); public static void lists(List<Integer>list, List<Integer>temp, int target, int j) { int sum = 0; for(int i = 0; i < temp.size(); i++) { sum += temp.get(i); } if(sum > target) { return; }else if(sum == target) { Collections.sort(temp); boolean exist = false; for(List<Integer>l : res) { if(l.size() == temp.size()) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == temp.get(i))c++; } if(c == l.size()) {exist = true; break;} } } if(!exist) { res.add(new ArrayList<>(temp)); } }else if(j == list.size())return; for(int i = j; i < list.size(); i++){ temp.add(list.get(i)); lists(list,temp,target,i+1); temp.remove(temp.size()-1); } return; } static ArrayList<Integer>dfs(int p, int c, ArrayList<Integer>l,HashMap<Integer,Integer>map, LinkedList<Integer>[]list, int v) { l.add(map.get(c)); System.out.println(c); if(c == v) { //System.out.println(c); return l; } for(int j : list[c]) { if(c == 1) { // System.out.println("Yes"+" "+j); // System.out.println("yes"+" "+list[c]); } if(j != p) { dfs(c,j,l,map,list,v); } } return new ArrayList<>(); } static boolean pos(char []a, int j) { char []ar = new char[a.length]; for(int i = 0; i < a.length; i++) { ar[i] = a[i]; } ar[j] = 'c'; ar[j-1] = 'a'; ar[j-2] = 'b'; ar[j-3] = 'a'; ar[j+1] = 'a'; ar[j+2] = 'b'; ar[j+3] = 'a'; int count = 0; for(int i = 3; i < ar.length-3; i++) { if(ar[i] == 'c' && ar[i-1] == 'a' && ar[i-2] == 'b' && ar[i-3] == 'a' && ar[i+1] == 'a' && ar[i+2] == 'b' && ar[i+3] == 'a') { count++; } } if(count == 1)return true; return false; } static void bruteforce(String s) { String ans = s.charAt(0)+""; ans += ans; StringBuilder sb = new StringBuilder(); sb.append(s.charAt(0)); for(int i = 1;i < s.length(); i++){ String d = sb.toString(); sb.reverse(); d += sb.toString(); boolean con = true; System.out.println(d+" "+s); for(int j = 0; j < Math.min(d.length(), s.length()); j++) { if(s.charAt(j) > d.charAt(j)) { con = false; break; } } sb.reverse(); sb.append(s.charAt(i)); if(con) { ans = d; break; } } System.out.println(ans+" "+"yes"); } static void permute(String s , String answer) { if (s.length() == 0) { System.out.print(answer + " "); return; } for(int i = 0 ;i < s.length(); i++) { char ch = s.charAt(i); String left_substr = s.substring(0, i); String right_substr = s.substring(i + 1); String rest = left_substr + right_substr; permute(rest, answer + ch); } } static List<List<Integer>>result = new ArrayList<>(); public static void comb(List<Integer>list, int n,int []ar, HashSet<Integer>set) { if(list.size() == n) { boolean exist = false; for(List<Integer>l : result) { int c = 0; for(int i = 0; i < l.size(); i++) { if(l.get(i) == list.get(i))c++; } if(c == n) { exist = true; break;} } if(!exist)result.add(new ArrayList<>(list)); } for(int j = 0; j < n; j++) { if(!set.contains(j)) { list.add(ar[j]); set.add(j); comb(list,n,ar,set); set.remove(j); list.remove(list.size()-1); } } return; } static void pinkSeat(int [][]mat, int i, int j, int n, int m, boolean vis[][], int [][]res) { if(i <= 0 || j <= 0 || i > n || j > m || vis[i][j])return; int a = Math.abs(i-1); int b = Math.abs(j-1); int c = Math.abs(i-1); int d = Math.abs(j-m); int e = Math.abs(i-n); int f = Math.abs(j-m); int x = Math.abs(i-n); int y = Math.abs(j-1); vis[i][j] = true; int max = Math.max(a+b,c+d); max = Math.max(max, e+f); max = Math.max(max, x+y); res[i][j] = max; pinkSeat(mat,i-1,j-1,n,m,vis,res); pinkSeat(mat,i+1,j-1,n,m,vis,res); pinkSeat(mat,i-1,j+1,n,m,vis,res); pinkSeat(mat,i+1,j+1,n,m,vis,res); pinkSeat(mat,i,j-1,n,m,vis,res); pinkSeat(mat,i,j+1,n,m,vis,res); pinkSeat(mat,i-1,j,n,m,vis,res); pinkSeat(mat,i+1,j,n,m,vis,res); } static boolean isPowerOfTwo(long 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 long ksearch(long s,long e, long []ar, long ans, long h, int n, long k) { if(s > e)return k; long mid = (s+e)/2; long count = 0; for(int i = 0; i < n; i++) { count += (ar[i])/mid; if(ar[i]%mid > 0)count++; } if(count <= h) { if(mid <= k) { ans = count; k = mid; } return ksearch(s, mid-1,ar,ans,h,n,k); }else { return ksearch(mid+1,e,ar,ans,h,n,k); } } static long mod = 1000000000; static long ans2 = 0; static void shirtCombo(long x, HashSet<Integer>set,int j, ArrayList<List<Integer>>list) { if(set.size() == list.size()) { ans2 = (ans2+x)%mod; } if(j == list.size()) { return; } int c = 0; List<Integer>temp = list.get(j); for(int a : temp ) { if(!set.contains(a)) { c++; set.add(a); shirtCombo(1,set,j+1,list); set.remove(a); } } x = (x*c)%mod; } public static class Pair { int x; int y; // Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } static class Compare1{ static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.x - p2.x; } }); } } static long gcd1(long a, long b) { if (a == 0) return b; return gcd1(b % a, a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a / gcd1(a, b)) * b; } static ArrayList<List<Integer>>res1 = new ArrayList<>(); static void permuteNumbers(LinkedHashSet<Integer>set, int n) { if(res1.size() == n)return; if(set.size() == n) { List<Integer>l = new ArrayList<>(); for(int a : set) { l.add(a); } boolean pos = true; for(int i = 2; i < l.size(); i++) { if(l.get(i-1) + l.get(i-2) == l.get(i)) { pos = false; break; } } if(pos) { res1.add(new ArrayList(l)); } return; } for(int i = 1; i <= n; i++) { if(!set.contains(i)) { set.add(i); permuteNumbers(set,n); set.remove(i); } } } static HashSet<String>set = new HashSet<>(); public static void main(String args[])throws IOException { BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(ob.readLine()); while(t --> 0) { /* StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(ob.readLine()); int []ar = new int[n]; int n = Integer.parseInt(ob.readLine()); long []ar = new long[n]; StringTokenizer st = new StringTokenizer(ob.readLine()); for(int i = 0; i < n; i++) { ar[i] = Long.parseLong(st.nextToken()); } */ StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); String s = ob.readLine(); int i = 0; int j = s.length()-1; while(i < j) { if(s.charAt(i) == s.charAt(j)) { i++; j--; }else { break; } } if(k == 0) { System.out.println(1); }else if(i == j || i > j) { System.out.println(1); }else { System.out.println(2); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
bcf3d156ab77a3a3090f66500aa55395
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; /** * @author chaofanjun * @Description * @createTime 2022/2/9 15:36 */ public class Main { static boolean judgeReverseSame(int l, String s) { for (int i = 0; i < l; i++) { if (s.charAt(i) != s.charAt(l - 1 - i)) { return false; } } return true; } static int solve(int l, int k, String s) { if (k == 0) { return 1; } boolean reverseSame = judgeReverseSame(l, s); return reverseSame ? 1 : 2; } public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int l = scanner.nextInt(); int k = scanner.nextInt(); String s = scanner.next(); int ans = solve(l, k, s); System.out.println(ans); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
989e41d2264dbfd7338edb80f5a20e55
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
// package code.forces.contest1634; import java.util.Scanner; /** * @author chaofanjun * @Description * @createTime 2022/2/9 15:36 */ public class Main { static boolean judgeReverseSame(int n, String s) { for (int i = 0; i < n; i++) { if (s.charAt(i) != s.charAt(n - 1 - i)) { return false; } } return true; } static int solve(int n, int k, String s) { if (k == 0) { return 1; } boolean reverseSame = judgeReverseSame(n, s); return reverseSame ? 1 : 2; } public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int n = scanner.nextInt(); int k = scanner.nextInt(); String s = scanner.next(); int ans = solve(n, k, s); System.out.println(ans); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
3b1b9ecca56361bbf7904eeb62894fd5
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; //code by tishrah_ public class _practise { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] ia(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=nextInt(); return a; } int[][] ia(int n , int m) { int a[][]=new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextInt(); return a; } long[][] la(int n , int m) { long a[][]=new long[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextLong(); return a; } char[][] ca(int n , int m) { char a[][]=new char[n][m]; for(int i=0;i<n;i++) { String x =next(); for(int j=0;j<m ;j++) a[i][j]=x.charAt(j); } return a; } long[] la(int n) { long a[]=new long[n]; for(int i=0;i<n;i++)a[i]=nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(long[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {long oi=r.nextInt(n), temp=a[i];a[i]=a[(int)oi];a[(int)oi]=temp;}Arrays.sort(a);} static void sort(int[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);} public static long sum(long a[]) {long sum=0; for(long i : a) sum+=i; return(sum);} public static long count(long a[] , long x) {long c=0; for(long i : a) if(i==x) c++; return(c);} public static int sum(int a[]) { int sum=0; for(int i : a) sum+=i; return(sum);} public static int count(int a[] ,int x) {int c=0; for(int i : a) if(i==x) c++; return(c);} public static int count(String s ,char ch) {int c=0; char x[] = s.toCharArray(); for(char i : x) if(ch==i) c++; return(c);} public static boolean prime(int n) {for(int i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;} public static boolean prime(long n) {for(long i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;} public static int gcd(int n1, int n2) { if (n2 != 0)return gcd(n2, n1 % n2); else return n1;} public static long gcd(long n1, long n2) { if (n2 != 0)return gcd(n2, n1 % n2); else return n1;} public static int[] freq(int a[], int n) { int f[]=new int[n+1]; for(int i:a) f[i]++; return f;} public static int[] pos(int a[], int n) { int f[]=new int[n+1]; for(int i=0; i<n ;i++) f[a[i]]=i; return f;} public static int[] rev(int a[]) { for(int i=0 ; i<(a.length+1)/2;i++) { int temp=a[i]; a[i]=a[a.length-1-i]; a[a.length-1-i]=temp; } return a; } public static boolean palin(String s) { StringBuilder sb = new StringBuilder(); sb.append(s); String str=String.valueOf(sb.reverse()); if(s.equals(str)) return true; else return false; } public static String rev(String s) { StringBuilder sb = new StringBuilder(); sb.append(s); return String.valueOf(sb.reverse()); } public static void main(String args[]) { FastReader in=new FastReader(); PrintWriter so = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); _practise ob = new _practise(); int T = in.nextInt(); // int T = 1; tc : while(T-->0) { int n = in.nextInt(); int k = in.nextInt(); String s = in.next(); if(k<=0 || palin(s)) so.println(1); else { char ch[] = s.toCharArray(); Arrays.sort(ch); if(ch[0]==ch[n-1]) so.println(1); else so.println(2); } } so.flush(); /*String s = in.next(); * Arrays.stream(f).min().getAsInt() * BigInteger f = new BigInteger("1"); 1 ke jagah koi bhi value ho skta jo aap * initial value banan chahte int a[] = new int[n]; Stack<Integer> stack = new Stack<Integer>(); Deque<Integer> q = new LinkedList<>(); or Deque<Integer> q = new ArrayDeque<Integer>(); PriorityQueue<Long> pq = new PriorityQueue<Long>(); ArrayList<Integer> al = new ArrayList<Integer>(); StringBuilder sb = new StringBuilder(); HashSet<Integer> st = new LinkedHashSet<Integer>(); Set<Integer> s = new HashSet<Integer>(); Map<Long,Integer> hm = new HashMap<Long, Integer>(); //<key,value> for(Map.Entry<Integer, Integer> i :hm.entrySet()) for(int i : hm.values()) for(int i : hm.keySet()) HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>(); so.println("HELLO"); Arrays.sort(a,Comparator.comparingDouble(o->o[0])) Arrays.sort(a, (aa, bb) -> Integer.compare(aa[1], bb[1])); Set<String> ts = new TreeSet<>();*/ } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
ce75a1b16cbf9c1c3b31377e97516c02
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class MyClass{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++){ int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String str = sc.nextLine(); if(k == 0){ System.out.println(1); continue; } if(str.equals(getRevertedString(str))){ System.out.println(1); }else { System.out.println(2); } } } public static String getRevertedString(String str){ StringBuilder builder = new StringBuilder(str); return builder.reverse().toString(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
eb13ff93998071d23077e6b74e331aa2
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; public class ReverseandConcatenate { static InputReader inputReader=new InputReader(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static void solve() throws IOException { int n=inputReader.nextInt(); int k=inputReader.nextInt(); String str=inputReader.readString(); if (k==0) { out.println(1); return; } else { if (ispalindrome(str)) { out.println(1); } else { out.println(2); } } } static boolean ispalindrome( String str) { int start=0; int end=str.length()-1; while (start<end) { if (str.charAt(start)!=str.charAt(end)) { return false; } start++; end--; } return true; } static PrintWriter out=new PrintWriter((System.out)); static void SortDec(int arr[]) { List<Integer>list=new ArrayList<>(); for(int ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } static void Sort(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while (t-->0) { solve(); } long s = System.currentTimeMillis(); // out.println(System.currentTimeMillis()-s+"ms"); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
29a7b7fa8b13837324f04beeb018470d
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
//import java.io.IOException; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; public class ReverseandConcatenate { static InputReader inputReader=new InputReader(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static void solve() throws IOException { int n=inputReader.nextInt(); int k=inputReader.nextInt(); String str=inputReader.readString(); if (k==0) { out.println(1); return; } else { if (ispalindrome(str)) { out.println(1); } else { out.println(2); } } } static boolean ispalindrome( String str) { int start=0; int end=str.length()-1; while (start<end) { if (str.charAt(start)!=str.charAt(end)) { return false; } start++; end--; } return true; } static PrintWriter out=new PrintWriter((System.out)); static void SortDec(int arr[]) { List<Integer>list=new ArrayList<>(); for(int ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } static void Sort(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while (t-->0) { solve(); } long s = System.currentTimeMillis(); // out.println(System.currentTimeMillis()-s+"ms"); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
1d29876e0c667923d10c2c7af6f87b87
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class AACFCC { public static long mod = (long) Math.pow(10, 9) + 7; public static long mod2 = 998244353; public static int oo = 0; public static ArrayList<Integer> prime; public static int zz = -1; public static int ttt = 0; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] str = br.readLine().split(" "); int n = (int) Long.parseLong(str[0]); int k = (int) Long.parseLong(str[1]); String s=br.readLine(); int p=1; if(k==0) { pw.println(1); continue; } for(int i=0;i<s.length()/2;i++) { if(s.charAt(i)!=s.charAt(n-1-i)) { p=0; break; } } if(p==0) { pw.println(2); }else { pw.println(1); } ////pw.println(); } pw.close(); } } // private static void putBit(int ind, int val, int[] bit) { // // TODO Auto-generated method stub // for (int i = ind; i < bit.length; i += (i & -i)) { // bit[i] += val; // } // } // // private static int getSum(int ind, int[] bit) { // // TODO Auto-generated method stub // int ans = 0; // for (int i = ind; i > 0; i -= (i & -i)) { // ans += bit[i]; // } // return ans; // } // private static void product(long[] bin, int ind, int currIt, long[] power) { // // TODO Auto-generated method stub // long pre = 1; // if (ind > 1) { // pre = power(power[ind - 1] - 1, mod2 - 1); // } // long cc = power[ind] - 1; // // System.out.println(pre + " " + cc); // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] = (bin[i] * pre) % mod2; // bin[i] = (bin[i] * cc) % mod2; // } // } // // private static void add(long[] bin, int ind, int val) { // // TODO Auto-generated method stub // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] += val; // } // } // // private static long sum(long[] bin, int ind) { // // TODO Auto-generated method stub // long ans = 0; // for (int i = ind; i > 0; i -= (i & (-i))) { // ans += bin[i]; // } // return ans; // } // // private static long power(long a, long p) { // TODO Auto-generated method stub // long res = 1;while(p>0) // { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // }return res; // }} // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // } // private static int kmp(String str) { // // TODO Auto-generated method stub // // System.out.println(str); // int[] pi = new int[str.length()]; // pi[0] = 0; // for (int i = 1; i < str.length(); i++) { // int j = pi[i - 1]; // while (j > 0 && str.charAt(i) != str.charAt(j)) { // j = pi[j - 1]; // } // if (str.charAt(j) == str.charAt(i)) { // j++; // } // pi[i] = j; // System.out.print(pi[i]); // } // System.out.println(); // return pi[str.length() - 1]; // } // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
f2f9319dc1dd74823bfa0e748deddb98
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
//package Practice; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class AACFCC { public static long mod = (long) Math.pow(10, 9) + 7; public static long mod2 = 998244353; public static int oo = 0; public static ArrayList<Integer> prime; public static int zz = -1; public static int ttt = 0; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] str = br.readLine().split(" "); int n = (int) Long.parseLong(str[0]); int k = (int) Long.parseLong(str[1]); String s=br.readLine(); int p=1; if(k==0) { pw.println(1); continue; } for(int i=0;i<s.length()/2;i++) { if(s.charAt(i)!=s.charAt(n-1-i)) { p=0; break; } } if(p==0) { pw.println(2); }else { pw.println(1); } //pw.println(); } pw.close(); } } // private static void putBit(int ind, int val, int[] bit) { // // TODO Auto-generated method stub // for (int i = ind; i < bit.length; i += (i & -i)) { // bit[i] += val; // } // } // // private static int getSum(int ind, int[] bit) { // // TODO Auto-generated method stub // int ans = 0; // for (int i = ind; i > 0; i -= (i & -i)) { // ans += bit[i]; // } // return ans; // } // private static void product(long[] bin, int ind, int currIt, long[] power) { // // TODO Auto-generated method stub // long pre = 1; // if (ind > 1) { // pre = power(power[ind - 1] - 1, mod2 - 1); // } // long cc = power[ind] - 1; // // System.out.println(pre + " " + cc); // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] = (bin[i] * pre) % mod2; // bin[i] = (bin[i] * cc) % mod2; // } // } // // private static void add(long[] bin, int ind, int val) { // // TODO Auto-generated method stub // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] += val; // } // } // // private static long sum(long[] bin, int ind) { // // TODO Auto-generated method stub // long ans = 0; // for (int i = ind; i > 0; i -= (i & (-i))) { // ans += bin[i]; // } // return ans; // } // // private static long power(long a, long p) { // TODO Auto-generated method stub // long res = 1;while(p>0) // { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // }return res; // }} // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // } // private static int kmp(String str) { // // TODO Auto-generated method stub // // System.out.println(str); // int[] pi = new int[str.length()]; // pi[0] = 0; // for (int i = 1; i < str.length(); i++) { // int j = pi[i - 1]; // while (j > 0 && str.charAt(i) != str.charAt(j)) { // j = pi[j - 1]; // } // if (str.charAt(j) == str.charAt(i)) { // j++; // } // pi[i] = j; // System.out.print(pi[i]); // } // System.out.println(); // return pi[str.length() - 1]; // } // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
cd689682f58b01529d97f2426704c694
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.io.*; 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(); } 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 class pair{ int x;int y; pair(int x,int y) { this.x=x; this.y=y; } } static int gcd(int a,int b) { if(a==0) return b; if(b==0) return a; return gcd(b%a,a); } static boolean isPrime(int x) { boolean bool[]=new boolean[x+1]; for(int i=0;i<bool.length;i++) bool[i]=true; for(int i=2;i<=Math.sqrt(x);i++) { if(bool[i]==true) { for(int j=i*i;j<=x;j+=i) bool[j]=false; } } for(int i=2;i<bool.length;i++) { if(bool[i]==true) { if(i==x) return true; } } return false; } static void swap(int a,int b) { int temp=a; a=b; b=temp; } static int max(int arr[]) { int max=Integer.MIN_VALUE; for(int i=0;i<arr.length;i++) { if(arr[i]>max) max=arr[i]; } return max; } static int min(int arr[]) { int min=Integer.MAX_VALUE; for(int i=0;i<arr.length;i++) { if(arr[i]<min) min=arr[i]; } return min; } static boolean isPalindrome(String s) { int i=0,j=s.length()-1; while(i<j) { if(s.charAt(i)!=s.charAt(j)) return false; i++;j--; } return true; } static String reverse(String s) { String ans=""; for(int i=0;i<s.length();i++) ans=s.charAt(i)+ans; return ans; } public static void main(String[] args) { try { FastReader sc=new FastReader(); FastWriter out = new FastWriter(); int testCases=sc.nextInt(); while(testCases-- > 0){ int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); String x=s;String y=""; if(isPalindrome(s)) { out.println(1); } else{ if(k==0) out.println(1); else out.println(2); } } out.close(); } catch (Exception e) { return; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
b943877c6602641edfe05168a58e90ac
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Practice1 { static long[] sort(long[] arr) { int n=arr.length; ArrayList<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static int[] sort(int[] arr) { int n=arr.length; ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long nCr(int n, int r) { // int x=1000000007; long dp[][]=new long[2][r+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=i&&j<=r;j++){ if(i==0||j==0||i==j){ dp[i%2][j]=1; }else { // dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1])%x; dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1]); } } } return dp[n%2][r]; } public static class UnionFind { private final int[] p; public UnionFind(int n) { p = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; } } public int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); } public void union(int x, int y) { x = find(x); y = find(y); if (x != y) { p[x] = y; } } } public static boolean ispalin(String str) { int n=str.length(); for(int i=0;i<n/2;i++) { if(str.charAt(i)!=str.charAt(n-i-1)) { return false; } } return true; } public static void main (String[] args) { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // out.print(); //out.println(); FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); String str=sc.nextLine(); if(ispalin(str)==true||k==0) { out.println(1); }else{ out.println(2); } } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
c78fca85bc6ba824682ee266a6583445
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(),k=sc.nextInt(); String s=sc.next(); StringBuilder sb=new StringBuilder(s); String p=sb.reverse().toString(); if(p.equals(s) || k==0){ System.out.println("1"); } else{ System.out.println("2"); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
fb0d38692d9124dab93dda6aa4cf374f
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.Scanner; public class Main { static Scanner input = new Scanner(System.in); public static void main(String[] args) { int k = input.nextInt(); while(k-- != 0){ method(); } } /* * 求最小变为回文的次数n,这可能就有2^n-1种 * 求变为回文的次数: * 要么已经是回文 * 若不是回文,变一次之后必是回文,如abcde -> abcdeedcba * * * */ static void method() { int n = input.nextInt(); int m = input.nextInt(); String str = input.next(); if (m == 0 || is_hui(str)) { System.out.println(1); } else { System.out.println(2); } } static boolean is_hui(String str) { int begin = 0; int end = str.length() - 1; char[] array = str.toCharArray(); while (begin <= end) { if (array[begin++] != array[end--]) return false; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
59a4d02a1252cc73bef4fa11e459f710
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class s { public static String rev(String s) { String r = ""; for (int i = 0 ; i<s.length() ; i++) r = s.charAt(i) + r; return r; } public static int[] StringtoNumber(String[] x) { int[] r = new int[x.length]; for (int i = 0 ; i<x.length ; i++) r[i] = Integer.parseInt(x[i]); return r; } public static boolean palindrome(String s) { String r = ""; for (int i = 0 ; i<s.length() ; i++) r = s.charAt(i) + r; return r.equals(s); } public static int number(int n , int k , String s , int count) { if (count == k) return 0; if (palindrome(s)) return 0; else return 2 + number(n , k , s + rev(s) , count+1) + number(n , k , rev(s) + s, count+1); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numberOfTestCases = sc.nextInt(); sc.nextLine(); for (int i = 0 ; i < numberOfTestCases ; i++) { String s = sc.nextLine(); String[] x = s.split(" "); String input = sc.nextLine(); int[] k = StringtoNumber(x); int count = 0; if (k[1] == 0) { System.out.println(1); } else if (palindrome(input)) { System.out.println(1); } else { int result = number(k[0] , k[1] , input , count); System.out.println(result); } } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
61054f33ad9b4980e3ff8af3e7d9dea8
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class cf770 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int l=sc.nextInt(); int op=sc.nextInt(); String s=sc.next(); if(op==0){ System.out.println(1); } else { if (palincheck(s)==true){ System.out.println(1); } else { System.out.println(2); } } } } static boolean palincheck(String s1) { String r=""; for(int i=s1.length()-1;i>=0;i--){ char ch=s1.charAt(i); r+=ch; } if(r.equals(s1)){ return true; } else{ return false; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
8c8503267b73d45a208f08cd4dc80021
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class A { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int K = Integer.parseInt(st.nextToken()); String s = infile.readLine(); StringBuilder sb1 = new StringBuilder(); sb1.append(s); sb1.reverse(); String str = sb1.toString(); // out.println(str); if(str.equals(s) || K == 0 ){ sb.append(1); sb.append('\n'); } else{ sb.append(2); sb.append('\n'); } } out.println(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for (int x : arr) out.print(x + " "); out.println(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
2199dee3711c55737706c382384d014d
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Reverse { 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) { String[] inp = br.readLine().split(" "); int n = Integer.parseInt(inp[0]); long k = Long.parseLong(inp[1]); String str = br.readLine(); if (k == 0 ){ System.out.println(1); continue; } boolean pal = true; int i = 0; int j = n - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) { pal = false; break; } i++; j--; } System.out.println(pal?1:2); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
63a94c6b7f6d327fd863317c8dd4604d
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class codeMaster { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = sc.nextInt(); while(tc-->0){ int n = sc.nextInt(), k = sc.nextInt(); String s = sc.next(); if(k == 0)pw.println(1); else{ boolean f = true; for(int i = 0, j = n - 1; i<j; i++, j--){ if(s.charAt(i) != s.charAt(j)){ f = false; break; } } if(f)pw.println(1); else pw.println(2); } } pw.flush(); } static class Pair implements Comparable<Pair>{ long x; long y; public Pair(long x, long y){ this.x = x; this.y = y; } public int compareTo(Pair p){ if(x > p.x)return 1; if(x < p.x)return -1; if(y > p.y) return -1; if(y < p.y) return 1; return 0; } public String toString(){ return "(" + x + "," + y + ")"; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
f255afc1f5915629d21e0c301b29f8a6
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
// Online IDE - Code Editor, Compiler, Interpreter import java.util.*; import java.util.Collections; public class cf1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int k=sc.nextInt(); sc.nextLine(); String str=sc.nextLine(); if(k>=1) { String rev=rev(str); String ans=str+rev; String ans1=rev+str; if(ans.equals(ans1)) { System.out.println("1"); } else { System.out.println("2"); } } else { System.out.println("1"); } } } static String rev(String str) { String s=""; for(int i=str.length()-1;i>=0;i--) { char ch=str.charAt(i); s+=ch; } return s; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
1e159d7eadd5735b13b537fa3fe79d51
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Lokeando20 { public static void main(String[] args){ Scanner read = new Scanner(System.in); int cases = read.nextInt(); read.nextLine(); ArrayList<Integer> list = new ArrayList<>(); String wordC="word"; int aux=0; for(int i=0;i<cases;i++){ int strL = read.nextInt(); int opr = read.nextInt(); read.nextLine(); wordC= read.nextLine(); if(opr==0){ list.add(1); continue; } char[]wordRever = new char[wordC.length()]; for(int j=(wordC.length()-1);j>=0;j--){ wordRever[aux] = wordC.charAt(j); aux++; } aux=0; String wordReversedString = String.valueOf(wordRever); if(wordReversedString.equals(wordC)){ list.add(1); }else list.add(2); } for(int print:list){ System.out.println(print); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
475d7824346ff68f4ef3f95eac80133a
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
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.StringTokenizer; public class A { static class Fast { BufferedReader br; StringTokenizer st; public Fast() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArray1(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* static long noOfDivisor(long a) { long count=0; long t=a; for(long i=1;i<=(int)Math.sqrt(a);i++) { if(a%i==0) count+=2; } if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a))) { count--; } return count; }*/ static boolean isPrime(long a) { for (long i = 2; i <= (long) Math.sqrt(a); i++) { if (a % i == 0) return false; } return true; } static void primeFact(int n) { int temp = n; HashMap<Integer, Integer> h = new HashMap<>(); for (int i = 2; i * i <= n; i++) { if (temp % i == 0) { int c = 0; while (temp % i == 0) { c++; temp /= i; } h.put(i, c); } } if (temp != 1) h.put(temp, 1); } static void reverseArray(int a[]) { int n = a.length; for (int i = 0; i < n / 2; i++) { a[i] = a[i] ^ a[n - i - 1]; a[n - i - 1] = a[i] ^ a[n - i - 1]; a[i] = a[i] ^ a[n - i - 1]; } } static boolean isPalindrome(char str[]) { char s[]=new char[str.length]; for(int i=0;i<str.length/2;i++) { if (str[str.length - i - 1] != str[i]) return false; } return true; } static boolean isPalindrome(String str) { // char s[]=new char[str.length]; for(int i=0;i<str.length()/2;i++) { if (str.charAt(str.length() - i - 1) != str.charAt(i)) return false; } return true; } static String reverse(String str) { String s1 = ""; for (int i = 0; i < str.length(); i++) s1 = str.charAt(i) + s1; return s1; } static int solve(String s,int k,int idx) { int l=0,r=0; if(idx>=k) return 0; if(isPalindrome(s)) return 1; // ab=ab+1; if(isPalindrome(s+reverse(s))&&idx+1<k) return 2; return (solve(s+reverse(s),k,idx+1) +solve(s+reverse(s),k,idx+1)); } static int ab=0; // static int idx=0; public static void main(String args[]) throws IOException { Fast sc = new Fast(); PrintWriter out = new PrintWriter(System.out); int t1 = sc.nextInt(); outer: while (t1-- > 0) { int n=sc.nextInt(); int k=sc.nextInt(); String s1=sc.nextLine(); if(k==0) { out.println(1); continue outer; } if(isPalindrome(s1)) out.println(1); else out.println(2); // long ans1=1<<ab; // out.println(ans1); // String s11="aab"; // out.println(isPalindrome(s11)); } out.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
ccae685122bd5b8e4586f4d67c548fa6
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(), k = sc.ni(); String str = sc.ns(); String rev = new StringBuilder(str).reverse().toString(); if(str.equals(rev) || k == 0) { w.p(1); return; } else { w.p(2); return; } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
a83d53ccceceef60bc64575b150e2345
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
// package Round770DIV2; import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); //int t = 1; int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); char [] a = sc.nextLine().toCharArray(); if(k==0||isPalindrome(a)){ pw.println(1); continue; } pw.println(2); } pw.close(); } static boolean isPalindrome(char []a){ int i =0; int j = a.length-1; while(i<=j){ if(a[i]!=a[j])return false; i++; j--; } return true; } // -------------------------------------------------------Basics---------------------------------------------------- static ArrayList<Integer> adj[]; static boolean vis[]; // binary search // first occur // last occur public static int binarySearch(long x, Long [] a){ int i =0; int j = a.length-1; int mid ; while(i<=j){ mid = (i+j)/2; if(a[mid]<=x){ i=mid+1; }else{ j=mid-1; } } return i; } public static void dfs(int u ){ vis[u]=true; for(int x: adj[u]){ if(!vis[x]) dfs(x); } } public static long gcd(long a , long b ){ if(b==0)return a; return gcd(b,a%b); } static class Pair implements Comparable<Pair>{ long x ; long y ; Pair(long x , long y){ this.x=x; this.y=y; } @Override public int compareTo(Pair o) { if(this.x==o.x)return 0; if(this.x>o.x)return 1; return -1; } @Override public String toString() { return x +" " + y ; } } static class Tuple implements Comparable<Tuple>{ int x; int y; int z; Tuple(int x, int y , int z){ this.x=x; this.y=y; this.z=z; } @Override public int compareTo(Tuple o) { if(this.x==o.x){ if(this.y==o.y)return this.z-o.z; return this.y-o.y; } return this.x-o.x; } @Override public String toString() { return x +" " + y + " " + z ; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
2f07fd9dc80f31133d6c4f1afc228ae0
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class Solution { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setOut(new PrintStream( new FileOutputStream("output.txt"))); sc = new Scanner(new File("input.txt")); } catch (Exception e) { } } solve(); } static void solve() { int t = sc.nextInt(); label: while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); String str = sc.next(); char arr[] = str.toCharArray(); if (k == 0) { System.out.println(1); continue label; } for (int i = 0; i < n; i++) { if (arr[i] != arr[n - i - 1]) { System.out.println(2); continue label; } } System.out.println(1); } } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
5eda7cb530c87f290e4f0ac09c099b1e
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { int n = sc.nextInt(); int k=sc.nextInt(); String s = sc.next(); if(k==0 || n==0) { System.out.println(1); continue; } int res=func(k,s); System.out.println(res); } } public static int func(int k,String s) { if(k==0 && rev(s).equals(s)) { return 1; } if(k==0 && !rev(s).equals(s)) { return 2; } if(k==1 && rev(s).equals(s)) { return 1; } if(k==1 && !rev(s).equals(s)) { return 2; } if(ispal(s)) { return 1; } return func(k-2,s+rev(s))+func(k-2,rev(s)+s); } public static String rev(String s) { char ch; String str=""; for (int i=0; i<s.length(); i++) { ch= s.charAt(i); str= ch+str; } return str; } public static boolean ispal(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
5b05a8d78bfe175d0d8ed48b6974670a
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static class scanner{ BufferedReader br; StringTokenizer st; scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } String next() throws IOException { while(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next()); } } public static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { scanner sc = new scanner(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); int i = 0; int j = s.length() -1 ; boolean flag = true; while(i<j){ if(s.charAt(i++) != s.charAt(j--)){ flag = false; break; } else flag = true; } if(flag){ pw.println(1); } else { if(k>=1){ pw.println(2); } else pw.println(1); } } pw.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
c6f824af8c5ce76b4b1435eaa0dad44e
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; import java.util.stream.Collectors; import java.io.*; import java.math.*; public class A13 { public static FastScanner sc; public static PrintWriter pw; public static StringBuilder sb; public static int MOD= 1000000007; 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()); } } public static void printList(List<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printList(Deque<Long> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printArr(long[] arr) { System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," ")); } public static long gcd(long a, long b) { if(b==0) return a; return gcd(b,a%b); } public static long lcm(long a, long b) { return((a*b)/gcd(a,b)); } public static void decreasingOrder(ArrayList<Long> al) { Comparator<Long> c = Collections.reverseOrder(); Collections.sort(al,c); } public static boolean[] sieve(int n) { boolean isPrime[] = new boolean[n+1]; Arrays.fill(isPrime, true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<n;i++) { for(int j=2*i;j<n;j+=i) { isPrime[j]=false; } } return isPrime; } public static long nCr(long N, long R) { double result=1; for(int i=1;i<=R;i++) result=((result*(N-R+i))/i); return (long) result; } public static long fastPow(long a, long b, int n) { long result=1; while(b>0) { if((b&1)==1) result=(result*a %n)%n; a=(a%n * a%n)%n; b>>=1; } return result; } public static int BinarySearch(long[] arr, long key) { int low=0; int high=arr.length-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==key) return mid; else if(arr[mid]>key) high=mid-1; else low=mid+1; } return low; //High=low-1 } public static int upperBound(int[] arr, int target, int l, int r) { while(l<=r) { int mid=(l+r)/2; if(arr[mid]<=target) l=mid+1; else r=mid-1; } return l; } public static int lowerBound(int[] arr, int target, int l, int r) { while(l<=r) { int mid=(l+r)/2; if(arr[mid]<target) l=mid+1; else r=mid-1; } return l; } public static boolean solve2(String s) { int i=0; int j=s.length()-1; while(i<=j) { if(s.charAt(i)==s.charAt(j)) { i++; j--; } else return false; } return true; } public static void solve(int t) { int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); if(solve2(s)==true) { System.out.println(1); return; } if(k==0) { System.out.println(1); return; } System.out.println(2); } public static void main(String[] args) { sc = new FastScanner(); pw = new PrintWriter(new OutputStreamWriter(System.out)); sb= new StringBuilder(""); int t=sc.nextInt(); for(int i=1;i<=t;i++) solve(i); System.out.println(sb); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
4c1c95b1bb04335ca3243b017b4285d8
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int ii=1;ii<=t;ii++) { int n = in.nextInt(),k = in.nextInt(); String str = in.next(); //String s = StringFormatter.reverseString(str); HashSet<String> set = new HashSet<>(); if(k==0) { System.out.println(1); } else { for(int i = 0;i<k;i++) { String s = StringFormatter.reverseString(str); set.add(s+str); set.add(str+s); s=s+str; } System.out.println(set.size()); } } } } class StringFormatter { public static String reverseString(String str){ StringBuilder sb=new StringBuilder(str); sb.reverse(); return sb.toString(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
d03ae6ae25880a4d6e40a712a01c167c
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.*; public class Main { public static Scanner in = new Scanner(System.in); public static void main(String[] args) { int t = in.nextInt(); while(t>0) { solve(); t--; } } public static void solve(){ int n= in.nextInt(); int k= in.nextInt(); String s = in.next(); String rev=""; for(int i=0;i<s.length();i++) { rev=s.charAt(i)+rev; } int ans=0; if(rev.equals(s)) { ans=1; } else{ if(k>0) ans=2; else ans=1; } if(k==0) ans=1; System.out.println(ans); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output
PASSED
8ce0b75b1c8dcd320b4dedfad3a190dd
train_107.jsonl
1644158100
Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.
256 megabytes
import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Solution{ static boolean isPalin(String s) { int n = s.length(); if(n==1) { return true; } int start = 0,end = n-1; while(start<end) { if(s.charAt(start)!=s.charAt(end)) { return false; } start++; end--; } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- >0) { int n,k; n = sc.nextInt(); k = sc.nextInt(); String s = sc.next(); if(k==0) { System.out.println(1); } else if(isPalin(s)) { System.out.println(1); } else{ System.out.println(2); } } sc.close(); } }
Java
["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"]
1 second
["2\n2\n1\n1"]
NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
Java 8
standard input
[ "greedy", "strings" ]
08cd22b8ee760a9d2dacb0d050dcf37a
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.
800
For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.
standard output