please help me in finding difference between these two codes I've been to this problem and tried this code but it was going wrong when i upload this but normal in test cases of mine i used some big numbers too and tried changing the int and Integer.parseInt to a Long.parseLong and lot
Please Consider reading this (a long post though)
All edits to my JAVA code are happily welcomed
A string S (composed of only small case letters of the English alphabet) is shown on the screen to the user. Then two integers P and Q are shown to the users. The user has now to create a string R using S and P by concatenating P copies of S one after another.
Then R is transformed into its smallest lexicographic permutation i.e. all the "a"'s are brought to the front and placed there, then after that all the "b"'s are placed, then "c"'s and so on. This is the final R that the user needs to form. Then Q numbers appear on the screen one by one. For each number N that appears on the screen, the user has to type the character at the Nth location of R. If N is greater than the length of R, they have to type "-1" (without quotes). The first line consists of an integer T, the number of test cases followed by the test cases themselves.
Each test case begins with the string S in the first line, followed by two space-separated integers P and Q in the next line.
Then Q lines follow, each containing an integer N.
For each test case, for each integer N, print in a new line the character at the Nth location of R or print "-1" (without quotes) as described above.
1 ≤ P ≤ 107 1 ≤ N ≤ 109 1 ≤ Q ≤ 105 1 ≤ |S| ≤ 100, where |S| denotes the length of the string. 1 ≤ T ≤ 10 Input: 1 abcde 3 5 3 4 10 15 16
Output: a b d e -1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,i,j,p,q,n,l2,x;
string r="",s="";
cin>>t;
for(i=0;i<t;i++)
{
cin>>s>>p>>q;
for(j=0;j<p;j++)
r=r+s;
l2=r.length();
char p[l2];
for(j=0;j<l2;j++)
p[j]=r[j];
sort(p,p+l2);
for(j=0;j<q;j++)
{
cin>>x;
if(x>l2)
cout<<"-1"<<endl;
else
cout<<p[x-1]<<endl;
}
}
return 0;
}
and
import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
try{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
for(int i=0;i<test;i++){
String str = "";
str = br.readLine();
int p = Integer.parseInt(br.readLine());
int q = Integer.parseInt(br.readLine());
String R = "";
for(int cop=0;cop<p;cop++){
R=R.concat(str);
}
char tempArr[] = R.toCharArray();
Arrays.sort(tempArr);
R = new String(tempArr);
int len = R.length();
for(int j=0;j<q;j++){
int num = Integer.parseInt(br.readLine());
if(num<=len){
System.out.println(R.charAt(num-1));
}
else System.out.println("-1");
}
}
}
catch(Exception e){System.out.println("problem");}
}
}
Aucun commentaire:
Enregistrer un commentaire