8.5-Day1T1--Asm.Def 谈笑风生

题目大意

m个操作,

1:添加一个字符串

2:查询字符串s是否被添加过(中至多包含一个通配符“*”)

题解

trie树可以得部分分

用map映射

'*'就枚举26个英文字母来判断就可以了

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<map>
#include<cstring>
#include<string>
using namespace std;

inline int read()
{
    int sum = 0,p = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
            p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        (sum *= 10) += ch - '0';
        ch = getchar();
    }
    return sum * p;
}

map<string,int> q;
string s;
int n,opt;
int main()
{
    n = read();    
    for(int i = 1;i <= n;i++)
    {
        opt = read();
        cin>>s;
        if(opt == 1)
            q[s] = 1;
        if(opt == 2)
        {
            int len = s.length();
            int o = 30;
            for(int i = 0;i < len;i++)
                if(s[i] == '*')
                {
                    o = i;
                    break;
                }
            if(o == 30)
            {
                if(q.count(s))
                    printf("YES
");
                else
                    printf("NO
");
            }
            else
            {
                bool flag = 0;
                for(int i = 0;i < 26;i++)
                {
                    char v = 'a' + i;
                    s[o] = v;
                    if(q.count(s))
                    {
                        flag = 1;
                        printf("YES
");
                        break;
                    }
                }
                if(!flag)
                    printf("NO
");
            }
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/darlingroot/p/11303211.html