Week8 CSP mock T2 HRZ learn English

Title description

Ruishen is a junior this year. He learned 26 letters in English during the winter vacation, so he is very excited! So he asked his friend TT to test him, TT thought of a good question of Kaori God: given a string, find 26 consecutive capital letters from it and output! But thinking about it, this is too cheap Raytheon, so he increased the difficulty: now given a string, the string includes 26 uppercase letters and special characters'? ', Special characters'? 'Can represent any capital letter. Now TT asks you if there is a substring consisting of 26 capital letters in a continuous position. In this substring, each letter appears and appears only once. If it exists, please output the first occurrence from the left. The substring that meets the requirements, and requires that if there are multiple sets of solutions that meet the leftmost position at the same time, the solution with the smallest lexicographic order is output! If it does not exist, output -1! Now the HRZ is circled. He has just learned 26 letters, which is too difficult for him, so he comes to you for help, please help him solve this problem, and the payment can help you play Overwatch. Note: The lexicographical order is first arranged according to the first letter, in the order of A, B, C ... Z; if the first letter is the same, then compare the second, third and even the following letters. If it is not the same as the last two words (for example, SIGH and SIGHT), then put the shorter one first. E.g

AB??EFGHIJKLMNOPQRSTUVWXYZ
输出:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABDCEFGHIJKLMNOPQRSTUVWXYZ

For example, the above two filling methods can make up 26 letters, but we require the smallest lexicographic order, only the former. Note that the title requires the first occurrence, with the smallest lexicographic order!

Input format

Enter only one line, a string that matches the description of the title.

Output format

The output is only one line, if there is such a substring, please output, otherwise output -1

Sample input 1

ABC??FGHIJK???OPQR?TUVWXY?

Sample output 1

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample input 2

AABCDEFGHIJKLMNOPQRSTUVW??M

Sample output 2

1

Ideas

For this question, I followed a sliding window-like idea, first enqueue the first 26 elements of the string in the queue, and then record '? 'The number of occurrences cw ++, and the number of occurrences of each character vis [s [i] -65] ++, and then traverse the vis array, and record the number of times cz in which cz, when cz == cw means that it is consistent at this time Conditional string section, it is specially judged whether it is the first time to join the queue to meet the conditions. If it does not work, continue to traverse backwards to determine whether the first element of the team is a question mark, then cw-, otherwise vis [qe.front ()-65] , Determine whether vis [qe.front ()-65] is 0 at this time, and then cz ++; when entering the team is similar to leaving the team, determine whether the new entry element is a question mark, cw ++, or vis [s [i] -65] + +, When vis [s [i] -65] is 1, you need cz–, you must judge cw == cz every time you enter or leave the team, and then give it in lexicographic order? Replace elements in the output queue

Code


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
queue<char>qe;
int t,n,cnt;
int vis[26]={0};bool flag=1;
string sre;
void show()
{
        for(int i=0;i<26;i++)
        {
            if(qe.front()=='?')
            {
                for(int i=0;i<26;i++)
                {
                    if(vis[i]==0)
                    {
                        vis[i]=1; 
                        printf("%c",i+65); 
                        break;
                    }
                }
            }
            else
            {
                cout<<qe.front();
            }
            qe.pop();
        }   
}
void f2()
{
    int cw=0,cz=0;//记录问号数,未到数
    for(int i=0;i<26;i++)
    {
        qe.push(sre[i]);
        if(sre[i]!='?')
        {
            vis[sre[i]-65]++; 
        }
        else cw++;
    }
    for(int i=0;i<26;i++)
    if(vis[i]==0)cz++;
    if(cz==cw)
    {//提前结束
        show();
        return;   
    }
    //初始化结束
    for(int i=26;i<n;i++)
    {
        if(qe.front()!='?')
        {
            vis[qe.front()-65]--;
            if(vis[qe.front()-65]==0)cz++;
        }
        else cw--;
        qe.pop();
        qe.push(sre[i]);
        if(sre[i]!='?')
        {
            vis[sre[i]-65]++;
            if(vis[sre[i]-65]==1)cz--;
        }
        else cw++;
        if(cz==cw)
        {
            show();
            return ;
        }
    }
    if(cz!=cw)
    cout<<"-1"<<endl;
    else show();
}
int main()
{
    memset(vis,0,sizeof(vis));
    cin>>sre;
    n=sre.size();
   // cout<<n<<endl; 
        f2();
    
    //system("pause");
    return 0;
}
Published 20 original articles · Likes3 · Visits 450

Guess you like

Origin blog.csdn.net/qq_44893580/article/details/105392518