传送门
题目描述
给你一个字符串,你从中挑选一些让它们成为一个环,使得这个环朝一个方向转 k k k下,与原来看起来一样,找到你能够满足这个条件的可以挑选的最多的字符数量。
分析
我们去枚举 k k k的因子,那么我转过一个 k k k,等于转过若干个因子,所以这个相当于循环节的长度,有了循环节,我们只需要去枚举有多少个循环节即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){return (b>0)?gcd(b,a%b):a;}
char str[N];
int a[30];
int n,k;
int main(){
int T;
read(T);
while(T--){
memset(a,0,sizeof a);
read(n),read(k);
scanf("%s",str);
for(int i = 0;str[i];i++) a[str[i] - 'a']++;
int res = 0;
for(int i = 1;i <= k;i++){
if(k % i) continue;
for(int j = 1;j * i <= n;j++){
int sum = 0;
for(int k = 0;k < 26;k++) sum += a[k] / j;
if(sum >= i) res = max(res,i * j);
}
}
di(res);
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/