fork(1) download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. /**
  6.  * created by asheshvidyut on 04/05/18
  7.  **/
  8. class Candies {
  9. public static void main(String args[]) {
  10. try {
  11. InputReader in = new InputReader(System.in);
  12. char s[] = in.readLine().trim().toCharArray();
  13. int freq[][] = new int[s.length][26];
  14. for (int i = 0; i < s.length; i++) {
  15. if (i - 1 >= 0) {
  16. for (int j = 0; j < 26; j++) {
  17. freq[i][j] += freq[i - 1][j];
  18. }
  19. }
  20. freq[i][s[i] - 'a']++;
  21. }
  22. int tc = in.readInt();
  23. for (int t = 0; t < tc; t++) {
  24. int n = in.readInt();
  25. int ans = Integer.MAX_VALUE;
  26. int low = n;
  27. int high = s.length;
  28. while(low <= high) {
  29. int mid = (low + high) >> 1;
  30. for (int i = 0; i < s.length; i++) {
  31. int next = i + mid - 1;
  32. if (next < s.length && next >= 0) {
  33. int f[] = new int[26];
  34. for (int j = 0; j < 26; j++) {
  35. f[j] = freq[next][j];
  36. if (i - 1 >= 0) {
  37. f[j] -= freq[i - 1][j];
  38. }
  39. }
  40. int countLetters = 0;
  41. Vector<Integer> odd = new Vector<>();
  42. for (int j = 0; j < 26; j++) {
  43. if (f[j] % 2 != 0){
  44. odd.add(f[j]);
  45. }
  46. else {
  47. countLetters += f[j];
  48. }
  49. }
  50. if (odd.size() == 0 && countLetters >= n) {
  51. ans = Math.min(mid, ans);
  52. }
  53. else {
  54. Collections.sort(odd);
  55. for (int j = 0; j < odd.size() - 1; j++) {
  56. countLetters += odd.get(j) - 1;
  57. }
  58. countLetters += odd.get(odd.size() - 1);
  59. if (countLetters >= n) {
  60. ans = Math.min(mid, ans);
  61. }
  62. }
  63. }
  64. }
  65. if (ans == mid) {
  66. high = mid - 1;
  67. }
  68. else {
  69. low = mid + 1;
  70. }
  71. if (ans == n) {
  72. break;
  73. }
  74. }
  75. if (ans == Integer.MAX_VALUE)
  76. ans = -1;
  77. out.write(Integer.toString(ans));
  78. out.newLine();
  79. }
  80. out.close();
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86.  
  87.  
  88. class InputReader {
  89. private boolean finished = false;
  90.  
  91. private InputStream stream;
  92. private byte[] buf = new byte[1024];
  93. private int curChar;
  94. private int numChars;
  95.  
  96. public InputReader(InputStream stream) {
  97. this.stream = stream;
  98. }
  99.  
  100. public int read() {
  101. if (numChars == -1)
  102. throw new InputMismatchException();
  103. if (curChar >= numChars) {
  104. curChar = 0;
  105. try {
  106. numChars = stream.read(buf);
  107. } catch (IOException e) {
  108. throw new InputMismatchException();
  109. }
  110. if (numChars <= 0)
  111. return -1;
  112. }
  113. return buf[curChar++];
  114. }
  115.  
  116. public int peek() {
  117. if (numChars == -1)
  118. return -1;
  119. if (curChar >= numChars) {
  120. curChar = 0;
  121. try {
  122. numChars = stream.read(buf);
  123. } catch (IOException e) {
  124. return -1;
  125. }
  126. if (numChars <= 0)
  127. return -1;
  128. }
  129. return buf[curChar];
  130. }
  131.  
  132. public int readInt() {
  133. int c = read();
  134. while (isSpaceChar(c))
  135. c = read();
  136. int sgn = 1;
  137. if (c == '-') {
  138. sgn = -1;
  139. c = read();
  140. }
  141. int res = 0;
  142. do {
  143. if (c < '0' || c > '9')
  144. throw new InputMismatchException();
  145. res *= 10;
  146. res += c - '0';
  147. c = read();
  148. } while (!isSpaceChar(c));
  149. return res * sgn;
  150. }
  151.  
  152. public long readLong() {
  153. int c = read();
  154. while (isSpaceChar(c))
  155. c = read();
  156. int sgn = 1;
  157. if (c == '-') {
  158. sgn = -1;
  159. c = read();
  160. }
  161. long res = 0;
  162. do {
  163. if (c < '0' || c > '9')
  164. throw new InputMismatchException();
  165. res *= 10;
  166. res += c - '0';
  167. c = read();
  168. } while (!isSpaceChar(c));
  169. return res * sgn;
  170. }
  171.  
  172. public String readString() {
  173. int length = readInt();
  174. if (length < 0)
  175. return null;
  176. byte[] bytes = new byte[length];
  177. for (int i = 0; i < length; i++)
  178. bytes[i] = (byte) read();
  179. try {
  180. return new String(bytes, "UTF-8");
  181. return new String(bytes);
  182. }
  183. }
  184.  
  185. public static boolean isSpaceChar(int c) {
  186. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  187. }
  188.  
  189. private String readLine0() {
  190. int c = read();
  191. while (c != '\n' && c != -1) {
  192. if (c != '\r')
  193. buf.appendCodePoint(c);
  194. c = read();
  195. }
  196. return buf.toString();
  197. }
  198.  
  199. public String readLine() {
  200. String s = readLine0();
  201. while (s.trim().length() == 0)
  202. s = readLine0();
  203. return s;
  204. }
  205.  
  206. public String readLine(boolean ignoreEmptyLines) {
  207. if (ignoreEmptyLines)
  208. return readLine();
  209. else
  210. return readLine0();
  211. }
  212.  
  213. public BigInteger readBigInteger() {
  214. try {
  215. return new BigInteger(readString());
  216. } catch (NumberFormatException e) {
  217. throw new InputMismatchException();
  218. }
  219. }
  220.  
  221. public char readCharacter() {
  222. int c = read();
  223. while (isSpaceChar(c))
  224. c = read();
  225. return (char) c;
  226. }
  227.  
  228. public double readDouble() {
  229. int c = read();
  230. while (isSpaceChar(c))
  231. c = read();
  232. int sgn = 1;
  233. if (c == '-') {
  234. sgn = -1;
  235. c = read();
  236. }
  237. double res = 0;
  238. while (!isSpaceChar(c) && c != '.') {
  239. if (c == 'e' || c == 'E')
  240. return res * Math.pow(10, readInt());
  241. if (c < '0' || c > '9')
  242. throw new InputMismatchException();
  243. res *= 10;
  244. res += c - '0';
  245. c = read();
  246. }
  247. if (c == '.') {
  248. c = read();
  249. double m = 1;
  250. while (!isSpaceChar(c)) {
  251. if (c == 'e' || c == 'E')
  252. return res * Math.pow(10, readInt());
  253. if (c < '0' || c > '9')
  254. throw new InputMismatchException();
  255. m /= 10;
  256. res += (c - '0') * m;
  257. c = read();
  258. }
  259. }
  260. return res * sgn;
  261. }
  262.  
  263. public boolean isExhausted() {
  264. int value;
  265. while (isSpaceChar(value = peek()) && value != -1)
  266. read();
  267. return value == -1;
  268. }
  269.  
  270. public String next() {
  271. return readString();
  272. }
  273.  
  274. public boolean readBoolean() {
  275. return readInt() == 1;
  276. }
  277. }
Success #stdin #stdout #stderr 0.09s 27828KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
java.util.InputMismatchException
	at InputReader.read(Main.java:103)
	at InputReader.readLine0(Main.java:193)
	at InputReader.readLine(Main.java:205)
	at Candies.main(Main.java:13)