zoukankan      html  css  js  c++  java
  • 【codeforces 514C】Watto and Mechanism(字符串hash)

    【题目链接】:http://codeforces.com/contest/514/problem/C

    【题意】

    给你n个字符串;
    然后给你m个询问;->m个字符串
    对于每一个询问字符串
    你需要在n个字符串里面找到和它的长度相同,且只有一个位置的字符不同的字符串;
    或者告知这是不存在的;

    【题解】

    字符串hash
    因为只有3个字符
    所以权值就为3^x;
    找个大质数取模就好;
    不够大就再大一点。。
    然后对于每一位,尝试换成其他两个字母,看看存不存在;

    【Number Of WA

    3

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define ps push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%lld",&x)
    #define ref(x) scanf("%lf",&x)
    #define ms(x,y) memset(x,y,sizeof x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 6e5+100;
    const LL MOD = 1e12+7;
    
    int n,m;
    LL p[N];
    string s;
    set<LL> myset;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        p[0] = 1;
        rep1(i,1,N-2)
            p[i] = (p[i-1]*3)%MOD;
        rei(n),rei(m);
        rep1(i,1,n)
        {
            cin >> s;
            LL temp = 0;
            int len = s.size();
            rep1(j,0,len-1)
                temp = (temp*3+s[j]-'0')%MOD;
            myset.insert(temp);
        }
        rep1(i,1,m)
        {
            cin >> s;
            bool ok = false;
            int len = s.size();
            LL temp = 0,ttemp;
            rep1(j,0,len-1)
                temp = (temp*3+s[j]-'0')%MOD;
            rep1(j,0,len-1)
            {
                char t = s[j];
                for (char d='a';d<='c';d++)
                if (t!=d)
                {
                    ttemp = (temp-(t-'0')*p[len-j-1])%MOD;
                    if (ttemp<0) ttemp=(ttemp+MOD)%MOD;
                    ttemp=(ttemp+(d-'0')*p[len-j-1])%MOD;
                    if (myset.count(ttemp))
                    {
                        ok = true;
                        break;
                    }
                }
                if (ok) break;
            }
            if (ok)
                puts("YES");
            else
                puts("NO");
        }
        //printf("
    %.2lf sec 
    ", (double)clock() / CLOCKS_PER_SEC);
        return 0;
    }
  • 相关阅读:
    Unity 保存游戏效果图片,并显示;
    Unity OnTriggerEnter问题
    Unity NGUI 批量点击跳转场景
    Unity调用手机摄像头进行摄像,并显示
    Unity3d NGUI 动态显示字体

    IDE的使用
    【树形Dp】【JSOI2008】【BZOJ1017魔兽地图DotR】
    【数学题】【Codeforces 164 Div2 E】【Playlist】
    【数学期望】【2012 ACM/ICPC 成都赛区现场赛】【B.Candy】
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626453.html
Copyright © 2011-2022 走看看