fork download
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5. static ArrayList<Combo> combos;
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. String line = sc.nextLine(),str;
  9. long previous,k,temp,number=0;
  10. int n =sc.nextInt(),index;
  11. long indices[]= new long[n];
  12. for(int i=0;i<n;i++){
  13. indices[i]=sc.nextLong();
  14. }
  15. sc.close();
  16. ArrayList<String> words=new ArrayList<>();
  17. ArrayList<Long> numbers = new ArrayList<>();
  18. char ch;
  19.  
  20. boolean prev_digit=false;
  21. ArrayList<Character> build_word = new ArrayList<>();
  22. for(int i=0;i<line.length();i++){
  23. ch=line.charAt(i);
  24. if(ch>='0' && ch<='9'){
  25. temp=ch-'0';
  26. if(prev_digit){
  27. number=number*10+temp;
  28. }
  29. else{
  30. number=temp;
  31. str="";
  32. for(Character c:build_word){
  33. str=str+c.charValue();
  34. }
  35. words.add(str);
  36. prev_digit=true;
  37. }
  38.  
  39.  
  40. }
  41. else{
  42. if(prev_digit){
  43. numbers.add(number);
  44. build_word=new ArrayList<>();
  45. build_word.add(ch);
  46. prev_digit=false;
  47. }
  48. else{
  49. build_word.add(ch);
  50.  
  51. }
  52.  
  53. }
  54. }
  55. if(prev_digit){
  56. numbers.add(number);
  57. }
  58. else{
  59. str="";
  60. for(Character c:build_word){
  61. str=str+c.charValue();
  62. }
  63. words.add(str);
  64. numbers.add(1l);
  65.  
  66. }
  67.  
  68.  
  69. combos = new ArrayList<>(numbers.size());
  70. Combo cb1;
  71. previous=0;
  72. for(int i=0;i<words.size();i++){
  73. number=numbers.get(i);
  74. str=words.get(i);
  75. cb1=new Combo(str,number,previous);
  76. previous=cb1.full;
  77. combos.add(cb1);
  78. }
  79.  
  80. for(int i=0;i<n;i++){
  81. if(search(indices[i])==-1){
  82. System.out.println("INVALID");
  83. }
  84. else{
  85. k=indices[i];
  86. while(true){
  87. index=search(k);
  88. cb1 = combos.get(index);
  89. if(cb1.finalrange(k)){
  90. System.out.println(cb1.str.charAt((int)(k-cb1.previous-1)));
  91. break;
  92. }
  93. else{
  94. k=k%cb1.limit;
  95. if(k==0){
  96. System.out.println(cb1.str.charAt(cb1.str_size-1));
  97. break;
  98. }
  99. }
  100.  
  101. }
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. }
  111. public static int search(long k){
  112. int start=0,mid;
  113. int end=combos.size()-1;
  114. if(k>combos.get(combos.size()-1).full || k<1){
  115. return -1;
  116. }
  117.  
  118. if(combos.get(start).inRange(k)){
  119. return start;
  120. }
  121. if(combos.get(end).inRange(k)){
  122. return end;
  123. }
  124. while(true){
  125. mid=(start+end)/2;
  126. if(combos.get(mid).inRange(k)){
  127. return mid;
  128. }
  129. if(k>combos.get(mid).full){
  130. start=mid;
  131. }
  132. else{
  133. end=mid;
  134. }
  135. }
  136. }
  137.  
  138.  
  139. }
  140. class Combo{
  141. String str;
  142. int str_size;
  143. long number;
  144. long previous;
  145. long full;
  146. long limit;
  147.  
  148. public Combo(String str,long number,long previous){
  149. this.str=str;
  150. this.str_size=this.str.length();
  151. this.number=number;
  152. this.previous=previous;
  153. limit=this.previous+this.str_size;
  154. full=limit*number;
  155. }
  156. public boolean inRange(long k){
  157. return (k>previous && k<=full);
  158. }
  159. public boolean finalrange(long k){
  160. return (k>previous && k<=limit);
  161. }
  162. }
Success #stdin #stdout 0.06s 711680KB
stdin
2ab3c
3
1 7 9
stdout
a
c
INVALID