zoukankan      html  css  js  c++  java
  • LC 466. Count The Repetitions

    link

    class Solution {
    public:
        int getMaxRepetitions(string s1, int n1, string s2, int n2) {
            int len2=s2.size();
            int len1=s1.size();
            vector<int> next(len2);
            vector<int> cnt(len2);
            for(int i=0;i<len2;i++){
                int idx=i;
                for(int j=0;j<s1.size();j++){
                    if(s2[idx]==s1[j]){
                        idx++;
                        if(idx==len2){
                            cnt[i]++;
                            idx=0;
                        }
                    }
                }
                next[i]=idx;
            }
            vector<int> idxToRound(len2,-1);
            int idx=0;
            int totalcnt=0;
            for(int i=1;i<=n1;i++){
                if(idxToRound[idx]!=-1){
                    int patternlen=i-idxToRound[idx];
                    int precnt=0;
                    int firstidx=0;
                    while(firstidx!=idx){
                        precnt+=cnt[firstidx];
                        firstidx=next[firstidx];
                    }
                    int patterncnt=totalcnt-precnt;
                    int middlecnt=(n1-idxToRound[idx]+1)/patternlen*patterncnt;
                    int remain=(n1-idxToRound[idx]+1)%patternlen;
                    int suffix=0;
                    firstidx=idx;
                    for(int j=1;j<=remain;j++){
                        suffix+=cnt[firstidx];
                        firstidx=next[firstidx];
                    }
                    return (precnt+middlecnt+suffix)/n2;
                }
                idxToRound[idx]=i;
                totalcnt+=cnt[idx];
                idx=next[idx];
            }
            return totalcnt/n2;
        }
    };
  • 相关阅读:
    mybatis框架快速入门
    perl FileHandle 模块使用
    perl substr
    Browse Code Answers
    无题
    dlang 泛型
    dlang 读取gz压缩文件
    R包 tidyverse 分列
    推荐一个网站:用各种语言去做同一件事
    dlang ref的作用
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12734830.html
Copyright © 2011-2022 走看看