HRZ Learn English

Title: Learn English in HRZ
Title: Insert picture description here
Input: Enter only one line, a string that matches the description of the title.

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

Sample:
Insert picture description here

Problem-solving ideas: Take a look at 1e6, okay, direct violence can be passed; search from the beginning, if there are 26 words that match the situation, judge the minimum character order output.

Code:

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
int a[26]={0};//a数组标记字母是否被选过
string y="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string sol(string m)
{
    for(int i=0;i<m.size();i++)
    {
        if(m[i]=='?')
        {
            for(int j=0;j<26;j++)
            {
                if(a[j]==0)//从头开始搜,看哪个没被标记
                {
                    m[i]=y[j];
                    a[j]=1;//标记一下
                    break;
                }
            }
        }
    }
    return m;
}
int main()
{
    string t;
    cin>>t;
    int q=0;
    for(int i=0;i<t.size();i++)//从头开始
    {
        memset(a,0,sizeof(a));
        int flag=0;
        string m="";
        for(int j=i;j<i+26;j++)//直接搜索
        {
            m+=t[j];
            if(t[j]!='?')//判断一下
            {
                if(a[int(t[j]-'A')]==0)//如果为0,通过
                {
                    a[int(t[j]-'A')]=1;
                }else//不然gg
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag==0)//通过的话,找最小字符序
        {
            q=1;
            string h=sol(m);
            cout<<h;
            break;
        }
        if(i+26==t.size())//上界
        {
            break;
        }
    }
    if(q==0)
    {
        cout<<-1;
    }
}
PGZ
Published 34 original articles · praised 0 · visits 863

Guess you like

Origin blog.csdn.net/qq_43653717/article/details/105396126