zoukankan      html  css  js  c++  java
  • Seek the Name, Seek the Fame && Hash

    求字符串首位一样的所有长度

    Solution

    对字符串建立hash, 枚举长度get_hash比较即可

    Code

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    #include<climits>
    #define LL long long
    #define REP(i, x, y) for(LL i = (x);i <= (y);i++)
    using namespace std;
    LL RD(){
        LL out = 0,flag = 1;char c = getchar();
        while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
        while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
        return flag * out;
        }
    const LL maxn = 400010, mod = 1e9 + 9, base = 233;
    char s[maxn];
    LL p[maxn], hash[maxn], len;
    void init(){
    	p[0] = 1;
    	REP(i, 1, maxn - 5)p[i] = p[i - 1] * base % mod;
    	}
    LL get_hash(LL l, LL r){
    	return ((hash[r] - hash[l - 1] * p[r - l + 1] % mod) % mod + mod) % mod;
    	}
    void work(){
    	len = strlen(s + 1);
    	hash[0] = 0;
    	REP(i, 1, len)hash[i] = (hash[i - 1] * base % mod + s[i] - 'a') % mod;
    	bool first = 1;
    	REP(i, 1, len){
    		if(get_hash(1, i) == get_hash(len - i + 1, len)){
    			if(first)printf("%d", i), first = 0;
    			else printf(" %d", i);
    			}
    		}
    	puts("");
    	}
    int main(){
    	init();
    	while(scanf("%s", s + 1) != EOF){
    		work();
    		}
    	return 0;
    	}
    
  • 相关阅读:
    [2020.12.5周六]Boruvka
    [2020.12.4周五] 圆上对称博弈
    [2020.12.3周四]最长上升子序列
    置顶~ 未来半年内训练计划
    cf1473d
    cf1474D
    寒假复健第一天 cf1475D
    来啦来啦,寒假复健第一题cf1475g
    12.1加训总结 2019南京
    12.7-12.13训练计划
  • 原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/14623741.html
Copyright © 2011-2022 走看看