`
king_tt
  • 浏览: 2110521 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

CF 182D Common Divisors(KMP最短循环节,循环周期)

 
阅读更多

链接:

http://codeforces.com/problemset/problem/182/D


题目大意:

假设字符串也有因数,一个字符串S的因数是a,当且仅当S是由k个a连续组成的。 例如S="abababab", 那么"ab"和"abab"都是S的因子。

给两个字符串,求它们的公因数有多少个。


分析总结:

先看看一个数的因子有什么性质。

如果知道了一个最小因子,那么就可以利用这个最小因子求出一个字符串的所有因子。

例如,假设S="abababab", len = |S| = 8, 最小因子为A="ab", minlen = |A| = 2, 除了这个最小因子之外,可以发现S的所有因子长度x必须满足len%x==0,并且,还要再满足x%minlen==0。 “abab”就是S的另外一个因子,它满足这个要求。


利用这个性质,就可以解决这道题了。

首先,分别利用kmp的next数组可以求出两个字符串的最小因数。然后用其中一个去匹配另一个,每次可以得到一个当前已匹配长度j,根据这个j,利用上述性质,如果同时满足两个字符串的因子要求,那么就是他们的一个公因子了。具体看代码。



代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

typedef long long int64;
const int MAXN = 100005;
char S[MAXN], T[MAXN];
int  f1[MAXN],f2[MAXN];
bool vis[MAXN];

void getNext(char* P, int* f){
    int n=strlen(P);
    f[0]=f[1]=0;
    for(int i=1; i<n; ++i){
        int j=f[i];
        while(j && P[i]!=P[j]) j=f[j];
        f[i+1] = P[i]==P[j]?1+j:0;
    }
}

int64 find(char* S,char* P,int* f1,int* f2){
    getNext(P,f1);
    getNext(S,f2);
    memset(vis, 0, sizeof(vis));

    int m=strlen(P);
    int n=strlen(S);

    int mp=m%(m-f1[m])?m:(m-f1[m]);
    int ms=n%(n-f2[n])?n:(n-f2[n]);

    int64 cnt=0;
    int j=0;
    for(int i=0; i<n; ++i){
        while(j && S[i]!=P[j]) j=f1[j];
        if(S[i] == P[j]) ++j;
        if(j && !vis[j]){
            if(m%j==0 && n%j==0 && (i+1)%ms==0 && j%mp==0 && j%ms==0){ // 是否满足公因子性质
                ++cnt;
                vis[j]=true;
            }
        }
    }
    return cnt;
}

int main(){
    scanf("%s %s",S,T);
    cout << find(S,T,f1,f2) << endl;
    return 0;
}


—— 生命的意义,在于赋予它意义士。

原创http://blog.csdn.net/shuangde800By D_Double (转载请标明)








分享到:
评论

相关推荐

    odd-divisors:求给定奇数除数的整数个数的应用程序

    奇数除数 一个 Java 应用程序,用于查找具有给定的奇数除数的整数的数量。 问题 给定整数 1 &lt; A &lt; B 和 K 正数和奇数,A 和 B(含)之间有多少个整数正好有 K 个除数? 用法 编译并在命令行输入java ...

    Modern computer arithmetic

    This is a book about algorithms for performing arithmetic, and their ...as modular arithmetic, greatest common divisors, the fast Fourier transform (FFT), and the computation of special functions.

    leetcode2sumc-interview:面试

    https://www.geeksforgeeks.org/common-divisors-of-n-numbers/ 3. https://www.geeksforgeeks.org/common-divisors-of-two-numbers/ 2. Leetcode 1. https://leetcode.com/problems/intersection-of-two-arrays-ii/...

    Diviseurs / Divisors-开源

    它是一种软件,可以查找数字范围为1至99999999999999999(17位)的所有除数。 它还会告诉您它是否是质数及其除数。

    oeis 整数数列离线查询 按数列序号排列 数列名称

    name: A063534 C(n) = H(n) + d(n), where C(n) is Chowla's function A048050, H(n) is the half-totient function A023022 and d(n) is the number of divisors function A000005. stripped: A063534 ,6,8,15,21,...

    Mathematics in Computing

    7.3.1 Greatest Common Divisors (GCD) 7.3.2 Least Common Multiple (LCM) 7.3.3 Euclid’s Algorithm 7.3.4 Distribution of Primes 7.4 Theory of Congruences 7.5 Review Questions 7.6 Summary 8 Cryptography ...

    project-euler:EgisonLib - libmathproject-euler.egi

    解决欧拉项目的实用函数用法 % egison -l lib/math/project-euler.egi&gt; (num-to-digits 12345){1 2 3 4 5}&gt; (digits-to-num {1 2 3 4 5})12345功能列表sum-of-positive-divisors &gt; (sum-of-positive-divisors 3)4 ;...

    divisors-generator-python:想找到一个奇数的所有除数吗? 你来对地方了

    除数发生器python 想找到所有奇数的除数吗? 您来对地方了,我编写了这个简单的程序,以便能够计算地球上最奇数的所有可能的除数。 在线上有很多站点,但是没有一个站点只关注除数,所以请尽情享受!...

    Algebraic Functions

    The distinguishing feature of the book is its third chapter, on rational functions, which gives an extremely brief and clear account of the theory of divisors. Here the integrands of the three ...

    最大公共字符串leetcode-LeetCode:力码

    divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only. 我的答案:数学 class Solution { public int sumFourDivisors(int[] nums) { int sum = ...

    欧拉公式求圆周率的matlab代码-eulerlib:受欧拉计划启发的休闲数学和数论相关功能库

    欧拉公式求长期率的matlab代码欧拉·利伯 是受启发的娱乐数学和数论相关功能...Divisors类实现与素因数分解,sigma函数等相关的功能。 &gt;&gt;&gt; from eulerlib import Divisors &gt;&gt;&gt; mydiv = Divisors(10000) &gt;&gt;&gt; div84 = myd

    HigherArithmetics:数论与整数数学

    离散数学库 该.Net例程使用System.Numerics.BigInteger 例子: using HigherArithmetics ; using HigherArithmetics .... using HigherArithmetics ....using System .... Join ( " , " , Divisors . A

    algorithm-practice:算法问题解决

    最大化Nice Divisors.py的数量 ['。','leetcode']767。重新组织String.py ['。','leetcode'] 5714.评估String.py的括号对 ['。','leetcode'] 621. Task Scheduler.py ['。','leetcode'] 5715.重新初始化...

    多米诺骨牌算法leetcode-DSA-CP:DSA-CP

    chef_and_divisors.py │  ├── codechef_college_contest │  │  ├── ball_game.cpp │  │  ├── bucket.cpp │  │  └── ravi1234.py │  ├── DEC20B-codechef │  │  ├── catch_...

    Advanced Algebra

    2. Divisors 531 3. Genus 534 4. Riemann–Roch Theorem 540 5. Applications of the Riemann–Roch Theorem 552 6. Problems 554 X. METHODSOF ALGEBRAICGEOMETRY 558 1. Affine Algebraic Setsand Affine ...

    [离散数学及其应用(英文第六版)].Discrete.Mathematics.and.its.Applications.djvu

    200 3.5 Primes and Greatest Common Divisors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 3.6 Integers and Algorithms. . . . . . . . . . . . . . . . . . . . . ....

    Channel Coding in Communication Networks

    Homage to Alain Glavieux. . . . . . . . . ....Chapter 1....1.1. Introduction: the Shannon paradigm ....1.2. Principal coding functions ....1.2.1. Source coding ....1.2.2. Channel coding ....1.2.3. Cryptography ....

Global site tag (gtag.js) - Google Analytics