zoukankan      html  css  js  c++  java
  • CH1807 Necklace (字符串Hash + 最小表示法)

    把第一个字符串拼接后就是hash + 最小表示法板子了。

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int N=4000010,P=131;
    string s, t, ss;
    typedef unsigned long long ULL;
    ULL h1[N], p1[N], h2[N], p2[N];
    int lens, lent;
    ULL get1(int l,int r)
    {
        return h1[r] - h1[l-1] * p1[r-l+1];
    }
    ULL get2(int l,int r)
    {
        return h2[r] - h2[l-1] * p2[r-l+1];
    }
    long long ans = 0;
    int main()
    {
        //freopen("data.txt", "r", stdin);
        cin >> s;
        cin >> t;
        ss = s;
        s = s + ss;
        lens = s.size(), lent = t.size();
        s = " " + s;
        t = " " + t;
        p1[0] = 1;
        p2[0] = 1;
        for(int i = 1; i <= lens; i++)
        {
            p1[i] = p1[i-1] * P;
            h1[i] = h1[i-1] * P +s[i];
        }
        for(int i = 1;i <= lent; i++)
        {
            p2[i] = p2[i-1] * P;
            h2[i] = h2[i-1] * P +t[i];
        }
        bool flag = 0;
        for(int i = 1; i + lent <= lens; i++)
        {
            if(get1(i, i + lent - 1) == get2(1, lent))
            {
                flag = 1;
                break;
            }
        }
        if(flag) cout << "Yes" << endl;
        else {
            cout << "No" << endl; 
            return 0;
        }
    
    
        int i = 1, j = 2, k;
        while(i <= lent && j <= lent) {
            for(k = 0; k < lent && s[i + k] == s[j + k]; k++); 
            if(k == lent) break;
            if(s[i + k] > s[j + k]) {
                i = i + k + 1;
                if(i == j) i++;
            } else {
                j = j + k + 1;
                if(i == j) j++;
            }
        }
        int ans = min(i, j);
        for(int i = ans; i <= ans + lent - 1; i++) {
            cout << s[i];
        }
        return 0;
    }
    
  • 相关阅读:
    【字符串题目】poj 3096 Surprising Strings
    【转载】:【博弈论】博弈的学习和总结
    【博弈论】hihocoder
    转载:SPFA算法学习
    马克思所言:
    【NOIP2013】火柴排队
    【NOIP2013】【P1441】花匠
    【JZOI2002】【BZOJ1477】【P1371】青蛙的约会
    【P1373】奶牛的卧室
    2016.9.16 の 測試
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/14484257.html
Copyright © 2011-2022 走看看