zoukankan      html  css  js  c++  java
  • nowcoder 白兔的字符串

    题意:输入一个字符串,接下来n个字符,问每个字符串有多少个子串是和原串循环同构的

    题解:可以用hash水过...把原串所有循环同构的hash值放入set,然后对每个字符串可以找到所有的子串,这里的set用unordered_set就能过

    #include <bits/stdc++.h>
    #define maxn 1000010
    #define INF 0x3f3f3f3f
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    char s[maxn];
    const ull B = 1e9+7;
    ull fa;
    unordered_set<ull >mp;
    int ans, n, l;
    void init(int len) {
        ull ah = 0;
        for(int i=0; i<len; i++)
            ah = ah*B+s[i];
        mp.insert(ah);
        fa = 1;
        for(int i=1; i<len; i++) fa *= B;
        for(int i=0; i<len; i++) {
            mp.insert(ah);
            ah = (ah-s[i]*fa)*B+s[i];
        }
    }
    int main() {
        scanf("%s", s);
        int len = strlen(s);
        init(len);
        scanf("%d", &n);
        for(int i=0; i<n; i++) {
            scanf("%s", s);
            l = strlen(s);
            ans = 0;
            if(l >= len) {
                ull ah = 0;
                for(int j = 0; j < len; j++)
                    ah = ah*B+s[j];
                if(mp.count(ah)) ans++;
                for(int j=len; j<l; j++) {
                    ah = (ah-s[j-len]*fa)*B+s[j];
                    if(mp.count(ah)) ans++;
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    毕业设计进度16
    毕业设计进度15
    毕业设计进度14
    毕业设计进度13
    毕业设计进度12
    毕业设计进度11
    JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    Media Queries详解
    css 选择器
    css内容样式属性
  • 原文地址:https://www.cnblogs.com/Noevon/p/8541983.html
Copyright © 2011-2022 走看看