Advertisement
Guest User

Untitled

a guest
Jun 27th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:16777216")
  2. #include <cctype>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <math.h>
  9. #include <time.h>
  10. #include <cmath>
  11. #include <vector>
  12. #include <queue>
  13. #include <deque>
  14. #include <map>
  15. #include <list>
  16. #include <set>
  17. #include <string>
  18. #include <iomanip>
  19. #include <iterator>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22.  
  23. #define forn(i, n) for (i64 i = 0; i < n; ++i)
  24. #define revn(i, n) for (i64 i = n-1; i>=0; --i)
  25. #define cyc(i, s, n) for (i64 i = s; i <= n; ++i)
  26. #define pb push_back
  27. #define mp(a,b) make_pair(a,b)
  28. #define all(x) x.begin(), x.end()
  29. #define F first
  30. #define S second
  31. #define eps 1e-7
  32. #define inf (1000*1000*1000+9)
  33. #define debug(x) cout << x << endl;
  34.  
  35.  
  36. using namespace std;
  37. typedef unsigned long long u64;
  38. typedef long long i64;
  39. typedef long double ld;
  40. typedef vector < i64 > vi64;
  41. typedef vector < u64 > vu64;
  42. typedef pair < i64, i64 > pi64;
  43. typedef vector < vi64 > graph;
  44. typedef int huint;
  45.  
  46. /////////////////////////////////////////////////////////////////////////////////////
  47. /////////////////////////////////////////////////////////////////////////////////////
  48.  
  49. inline i64 nextInt() {
  50.     int s = 1, x = 0, c = getc(stdin);
  51.     while (c <= 32)
  52.         c = getc(stdin);
  53.     if (c == '-')
  54.         s = -1, c = getc(stdin);
  55.     while ('0' <= c && c <= '9')
  56.         x = x * 10 + c - '0', c = getc(stdin);
  57.     return x * s;
  58. }
  59.  
  60. inline void printInt(i64 x) {
  61.     if (x < 0)
  62.         putc('-', stdout), x = -x;
  63.     char s[20];
  64.     i64 n = 0;
  65.     while (x || !n)
  66.         s[n++] = '0' + x % 10, x /= 10;
  67.     while (n--)
  68.         putc(s[n], stdout);
  69. }
  70.  
  71. inline void NextString( char *s ) {
  72.     int c = getc(stdin);
  73.     while (c <= 32)
  74.         c = getc(stdin);
  75.     while (c > 32)
  76.         *s++ = c, c = getc(stdin);
  77.     *s = 0;
  78. }
  79.  
  80. inline void writeWord( char *s ) {
  81.     while (*s)
  82.         putchar(*s++);
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////////////
  86. /////////////////////////////////////////////////////////////////////////////////////
  87.  
  88. vi64 p;
  89.  
  90. void sieve() {
  91.     i64 n = 10000;
  92.     vector<char> pr(n+1,true);
  93.     cyc(i, 2, n) {
  94.         if (pr[i])
  95.             for (i64 j = i*i; j<=n; j+=i)
  96.                 pr[j]=false;
  97.     }
  98.     cyc(i, 2, n)
  99.         if (pr[i])
  100.             p.pb(i);
  101. }
  102.  
  103. string alph = "qwertyuiopasdfghjklzxcvbnm", ralph;
  104. string s;
  105. set<char> u;
  106. vector<bool> can;
  107. i64 Z = 0, A = 0, n;
  108.  
  109.  
  110.  
  111. bool fix(i64 &k) {
  112.     if (!k) {
  113.         cout << s;
  114.         return false;
  115.     }
  116.     i64 bestZ=0, bestA=0;
  117.     forn(i, n)
  118.         if (u.count(alph[i]))
  119.             ++bestA;
  120.         else break;
  121.     forn(i, n)
  122.         if (u.count(ralph[i]))
  123.             ++bestZ;
  124.         else break;
  125.     if (!Z && !A) {
  126.         cout << -1;
  127.         return false;
  128.     } else if (!Z || !A) {
  129.         char f;
  130.         if (bestZ>bestA)
  131.             f=alph[bestA];
  132.         else f=ralph[bestZ];
  133.         bool flag = false;
  134.         forn(i, n) {
  135.             if (can[i]) {
  136.                 s[i]=f;
  137.                 u.insert(f);
  138.                 can[i]=false;
  139.                 flag = true;
  140.                 break;
  141.             }
  142.         }
  143.         if (flag) {
  144.             --k;
  145.             return true;
  146.         } else {
  147.             cout << -1;
  148.             return false;
  149.         }
  150.     }
  151.     if (bestZ>bestA) {
  152.         bool flag = false;
  153.         forn(i, n) {
  154.             if (can[i] && s[i]=='a') {
  155.                 u.insert(alph[bestA]);
  156.                 s[i]=alph[bestA];
  157.                 can[i]=false;
  158.                 flag = true;
  159.                 break;
  160.             }
  161.         }
  162.         if (!flag) {
  163.             cout << -1;
  164.             return false;
  165.         } else {
  166.             --k;
  167.             --A;
  168.             return true;
  169.         }
  170.     }
  171.     if (bestA>=bestZ) {
  172.         bool flag = false;
  173.         forn(i, n) {
  174.             if (can[i] && s[i]=='z') {
  175.                 u.insert(ralph[bestZ]);
  176.                 s[i]=ralph[bestZ];
  177.                 can[i]=false;
  178.                 flag = true;
  179.                 break;
  180.             }
  181.         }
  182.         if (!flag) {
  183.             cout << -1;
  184.             return false;
  185.         } else {
  186.             --k;
  187.             --Z;
  188.             return true;
  189.         }
  190.     }
  191.     return true;
  192. }
  193.  
  194. int main() {
  195.     sort(all(alph));
  196.     ralph = alph;
  197.     reverse(all(ralph));
  198.     cin >> s;
  199.     n = s.size();
  200.     i64 k = nextInt();
  201.     can.resize(n,false);
  202.     forn(i, n) {
  203.         if (s[i]=='?') {
  204.             if (i&1) {
  205.                 s[i]='a';
  206.                 ++A;
  207.             } else {
  208.                 s[i]='z';
  209.                 ++Z;
  210.             }
  211.             can[i]=true;
  212.         } else {
  213.             u.insert(s[i]);
  214.         }
  215.     }
  216.     k-=u.size();
  217.     k = (k<0)?0:k;
  218.     while (fix(k)){}
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement