Пример #1
0
 void solve() {
   int k = nextInt();
   char[] c = nextToken().toCharArray();
   for (int i = 0, j = c.length - 1; i < j; i++, j--) {
     if (c[i] != '?' && c[j] != '?' && c[i] != c[j]) {
       out.println("IMPOSSIBLE");
       return;
     }
   }
   int q = 0;
   TreeSet<Character> ts = new TreeSet<Character>();
   for (int i = 0; i < k; i++) {
     ts.add((char) ('a' + i));
   }
   for (int i = 0, j = c.length - 1; i <= j; i++, j--) {
     if (c[i] == '?' && c[j] == '?') {
       q++;
     }
     if (c[i] != '?') {
       ts.remove(c[i]);
     }
     if (c[j] != '?') {
       ts.remove(c[j]);
     }
   }
   for (int i = (c.length - 1) / 2, j = c.length - i - 1; i >= 0; i--, j++) {
     if (c[i] == '?' && c[j] == '?') {
       if (!ts.isEmpty()) {
         c[i] = c[j] = ts.pollLast();
       } else {
         c[i] = c[j] = 'a';
       }
     }
   }
   if (!ts.isEmpty()) {
     out.println("IMPOSSIBLE");
     return;
   }
   for (int i = 0, j = c.length - 1; i < j; i++, j--) {
     if (c[i] == '?') {
       c[i] = c[j];
     } else if (c[j] == '?') {
       c[j] = c[i];
     }
   }
   out.println(new String(c));
 }