• Watto and Mechanism(hash字符串+set查找元素 ln的时间复杂度)


    对于hash,给这些东西赋值之后,就可以有具体的大小比较操作等等,sort排序的预处理啥的。

    总的长度很小,每一个位改变一下看行不行就可以拉。(set的 count 的利用)

    C. Watto and Mechanism
    time limit per test3 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position".
    
    Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
    
    Input
    The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively.
    
    Next follow n non-empty strings that are uploaded to the memory of the mechanism.
    
    Next follow m non-empty strings that are the queries to the mechanism.
    
    The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
    
    Output
    For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
    
    Examples
    inputCopy
    2 3
    aaaaa
    acacaca
    aabaa
    ccacacc
    caaac
    outputCopy
    YES
    NO
    NO
    View Problem
    #include <bits/stdc++.h>
    #define FF first
    #define SS second
    #define ll long long
    #define pb push_back
    #define pm make_pair
    using namespace std;
    typedef pair<int,int> PII;
    typedef unsigned ll ull;
    const int MAX = 6e5 + 5;
    const ll mod = 2000004199,mod2 = 1111111111111;
    const ll seed = 131,seed2 = 13331;
    int n,m;
    char s[MAX];
    ull P[MAX],P2[MAX];
    set<ull> ss,ss2;
    int main()
    {
        P[0]=P2[0]=1;
        for(int i = 1; i<MAX; i++) P[i] = P[i-1] * seed % mod,P2[i] = P2[i-1] * seed2 % mod2;
        cin>>n>>m;
        for(int i = 1; i<=n; i++) {
            scanf("%s",s+1);
            ull Hash = 0,Hash2 = 0;
            int len = strlen(s+1);
            for(int j = 1; j<=len; j++) Hash = (Hash*seed + s[j]-'a'+1)%mod,Hash2 = (Hash2*seed2+s[j]-'a'+1)%mod2;
            ss.insert(Hash);ss2.insert(Hash2);
        }
        for(int i = 1; i<=m; i++) {
            scanf("%s",s+1);
            ull Hash=0,Hash2 = 0;
            int len = strlen(s+1),flag = 0;
            for(int j = 1; j<=len; j++) Hash = (Hash*seed + s[j]-'a'+1)%mod,Hash2 = (Hash2*seed2+s[j]-'a'+1)%mod2;
            for(char ch = 'a'; ch<='c'; ch++) {
                for(int j = 1; j<=len; j++) {
                    if(s[j] == ch) continue;
                    if(ss.count((Hash+10*mod + (ch - s[j]) * P[len-j])%mod) && ss2.count((Hash2+10*mod2+(ch-s[j])*P2[len-j])%mod2)) {flag = 1;break;    }
                }
            }
            if(flag == 1) puts("YES");
            else puts("NO");
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    C# 谈谈代码重构
    收藏.NET 技术社区
    步步为营 .NET三层架构解析 四、Model设计(四种设计方式)
    C# 谈谈abstract用法 和一个简单abstract factory
    步步为营 .NET三层架构解析 一、什么是三层架构
    用户登陆的验证码的制作
    控制部分字体的颜色
    回发或回调参数无效。在配置中使用 <pages enableEventValidation= "true "/> 或在页面中使用 <%@ Page EnableEventValidation= "true " %> 启用了事件验证
    实习记2
    sniffer攻击
  • 原文地址:https://www.cnblogs.com/Lamboofhome/p/16060066.html
Copyright © 2020-2023  润新知