codeforces 1367 E (周期+思维)

没有想到若k为8,长度为6的字符串也可以满足,一直傻逼了以为是因数和倍数的关系

 

题意:给一个n长度的只包含小写英文字母的字符串,表示每个字符有多少个。再给一个k。让你用这些字符串组成一个任意长度的环字符串(长度小于等于n),使得其旋转任意k次得到的字符串不变。求这个字符串的最长长度。

思路:设周期为T,T为字符串长度len和k的最大公约数,len中包括len/T个相同的字符串,遍历26个英文字符,判断即可。

n为2e3,完全不会超时。

 

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector> 
// #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sp ' '
#define endl '\n'
#define inf  0x3f3f3f3f;
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
#define P pair<int, int>
#define fi first
#define se second
#define pb(x) push_back(x)
#define ppb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define sca2(a,b) scanf("%d %d",&(a),&(b))
#define sca(a) scanf("%d",&(a));
#define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define sca2ll(a,b) scanf("%lld %lld",&(a),&(b))
#define scall(a) scanf("%lld",&(a));


using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;}

const double Pi = acos(-1.0);
const double epsilon = Pi/180.0;
const int maxn = 2e5+10;
int cnt[30];
char c[2100];
int main()
{
    //freopen("input.txt", "r", stdin);
    int _;
    scanf("%d",&_);
    while(_--)
    {
        int n,k;
        cin>>n>>k;
        int ans = 0;
        cin>>c+1;
        int len = strlen(c+1);
        rep(i,1,len) {
            cnt[c[i]-'a']++;
        }
/*        rep(i,0,25){
            cout<<cnt[i]<<sp;
        }
        cout<<endl;*/
        rep(len,1,n){
            int sum = 0;
            int T = gcd(len,k);
            int nub = len/T;
            rep(i,0,25){
                int tmp = cnt[i]/nub;
                sum += tmp;
            }
        //    cout<<sum<<sp<<T<<endl;
            if(sum >= T){
                ans = len;
            }
        }
        cout<<ans<<endl;
        rep(i,0,n){
            c[i] = '\0';
        }
        rep(i,0,25){
            cnt[i] = 0;
        }
    }
}

 

posted @ 2020-06-30 09:12  阿斯水生产线  阅读(257)  评论(0编辑  收藏  举报