cf1367E 思维+贪心

1367E 1900的题
题意:给你n个字母,其中仅由a-z26个小写构成,选其中ans个字母围成一个圈使得这个圈顺时针旋转k个位置还是和一开始一样,求你能得到的最大的ans
思路:从大到小枚举最终字符串的长度,对当前长度和k取最大公因数即为字符串单节的长度len,然后求要经过多少次的循环即为cnt=i/len,然后对字符串进行贪心即可,具体实现可看到代码
代码如下:

#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define rep(i, a, n) for(int i = a; i <= n; i++)
#define per(i, a, n) for(int i = n; i >= a; i--)
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define fopen freopen("file.in","r",stdin);freopen("file.out","w",stdout);
#define fclose fclose(stdin);fclose(stdout);
const int inf = 1e9;
const ll onf = 1e18;
const int maxn = 1e5+10;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
	return x*f;
}
int gcd(int a, int b){
	return b==0 ? a : gcd(b, a%b);
}
inline void cf(){
	int t = read();
	while(t--){
		int n = read(), k = read();
		char str[n+5];
		scanf("%s", str+1);
		int m[30];
		rep(i,0,26) m[i] = 0; 
		rep(i,1,n){
			m[str[i]-'a']++;
		}
		per(i,1,n){		//枚举长度
			int len = gcd(i, k);	//单节长度
			int cnt = i/len;	//循环次数
			rep(j,0,25) len -= m[j]/cnt;
			if(len<=0){
				printf("%d\n", i);break;
			}
		}
	}
	return ;
}
signed main(){
	cf();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值