Title description:
Input and output scale and agreement:
Ideas:
If the length does not exceed 10 ^ 6, direct violence O (26 * N) is enough.
One point worth noting is, if it meets the requirements, how to output in lexicographic order?
My method is to count the number of occurrences of cnt [i] for 26 characters first.
Then traverse cnt from 1 to 26 and find it is 0, then put it in the queue.
When it is taken out of the queue, it can be guaranteed to be lexicographical. Because the queue is first in first out.
Of course, it is also possible to output the data in reverse order.
to sum up:
When the state is transferred, the queue and cnt array are not cleared! !
I didn't know what I was thinking at that time! ! (It may be that the relatives are eating too loudly in the living room ...)
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <map> using namespace std; const int MAXN = 1e6 + 5; char s [MAXN]; queue <int> q; / / Store missing characters, first-in first-out int cnt [200]; map <char, int> mp; int main () { for (int i = 0; i <26; i ++) mp ['A' + i] = i + 1; // 26 letters correspond to 1-26 mp ['?'] = 30; scanf ("% s", s + 1); int len = strlen (s + 1); int l = 1, r = 26; while (r <= len) { int i; // Characters in statistical substring for (i = l; i <= r; i ++) cnt [mp [s [i]]] ++; for ( i = 1; i <= 26;i++) { if(cnt[i]==0) q.push(i); if(cnt[i]>1) break; } // If the letter is 0 or 1, you can if (i == 27) // After searching the entire 26, there are no more characters { // output for ( int j = l; j <= r; j ++) { if (s [j]! = '?') cout << s [j]; else printf ("% c", q.front () + 'A' -1), q.pop (); } cout << endl; return 0; } else { l ++, r ++; memset (cnt, 0, sizeof (cnt)); while (! Q.empty ()) q.pop (); } } cout <<-1 << endl; return 0; }