Codeforces 182D - Common Divisors

本文介绍了一种使用KMP算法求字符串最小循环节的方法,并通过两个实例演示了如何计算不同字符串的最小循环周期。此外,还展示了如何比较两个字符串的循环节是否相同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

KMP求最小循环节

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h> 
char s1[100005],s2[100005];
int next[100005],flag;
void getnext(char * s){
    int k=1,j=0,len=strlen(&s[1]);
    next[k]= 0;
    while(k<=len ){
        if( j== 0|| s[k]== s[j] ){
            ++j, ++k;
            next[k]= j;
        }
        else
            j= next[j];
    }
}
int min(int a,int b){
    return a>b?b:a;
}
int main()
{
    int a,b,i,j;
    scanf("%s",&s1[1]); 
    getnext(s1);
    int len1= strlen(&s1[1]);//注意s[len+1]='\0'
    a= len1- next[len1+1]+1;
	if(len1%a==0)
	    len1/=a;
	else
		a=len1;
    scanf("%s",&s2[1]); 
    memset(next,0,sizeof(next));
    getnext(s2);
    int len2= strlen(&s2[1]);//注意s[len+1]='\0'
    b= len2- next[len2+1]+1;
    if(len2%b==0)
	    len2/=b;
	else
		b=len2;
    
    if(a!=b){
        printf("0\n");
    }
    else{
        for(i=1;i<=a;i++){
            if(s1[i]!=s2[i]){
                printf("0\n");
                break;
            }
        }
        if(i==a+1){
            int tem=min(len1,len2),sum=0;
            for(j=1;j<=tem;j++){
                if(len1%j==0 && len2%j==0)
                    sum++;
            }
            printf("%d\n",sum);
        }
    }
}


 

针对Codeforces平台上的Common Generator相关题目或讨论的信息,在提供的参考资料中并未涉及相关内容[^1]。然而,可以提供一般性的指导来帮助定位这类资源。 对于寻找特定类型的编程竞赛题目或是有关于某些算法组件如Common Generator的讨论,通常的做法是在Codeforces网站上利用其内置搜索功能输入关键词“Common Generator”。此外,参与社区论坛中的板块,例如比赛后的问答环节或者是专门的技术话题讨论区,也是获取相关信息的有效途径之一。 如果目标是了解Common Generator的具体实现细节以及应用场景,建议关注那些由经验丰富的参赛者撰写的博客文章和技术分享帖。这些资料往往包含了更深入的理解和实用技巧。 为了进一步探索这个主题,下面给出一段Python代码片段作为示例,展示如何通过网络爬虫自动收集Codeforces上与指定关键字匹配的内容链接: ```python import requests from bs4 import BeautifulSoup def search_codeforces(keyword): base_url = "https://codeforces.com" search_path = "/search?query=" response = requests.get(base_url + search_path + keyword) soup = BeautifulSoup(response.text, 'html.parser') results = [] for link in soup.find_all('a'): href = link.get('href') text = link.string if href and (keyword.lower() in str(text).lower()): full_link = base_url + href results.append(full_link) return results[:5] links = search_codeforces("Common Generator") for l in links: print(l) ``` 此脚本会返回前五个含有“Common Generator”的页面链接,供有兴趣的研究人员查阅更多信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值