Gym - 101889J - Jumping Frog

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/monochrome00/article/details/82995579

题目链接<http://codeforces.com/gym/229829/attachments>


题意:

给出一个循环字符串,R代表可以走,P代表不可以走。问有多少个步长,使得从某一点出发能回到原点。


题解:

因为是回到原点,所以可以只考虑每个因子。如果一个因子确定是可以的,那么它的倍数也就都可以了。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+7;
char s[N];
int stp[N],top,n,m;
bool vis[N];
bool jg(int x,int len){
    int now=(x+len)%n;
    while(1){
        if(x==now) return true;
        if(s[now]=='P') return false;
        now=(now+len)%n;
    }
}
int main()
{
    scanf("%s",s);
    n=strlen(s);
    for(int i=1;i<n;i++){
        if(n%i==0) stp[++top]=i;
    }
    int ans=0;
    for(int i=1;i<=top;i++){
        int len=stp[i];
        for(int j=0;j<len;j++){
            if(s[j]=='P') continue;
            if(jg(j,len)){
                for(int k=1;k*len<n;k++){
                    if(vis[k*len]) continue;
                    ans++;
                    vis[k*len]=true;
                }
                break;
            }
        }
    }
    printf("%d\n",ans);
}

猜你喜欢

转载自blog.csdn.net/monochrome00/article/details/82995579