Codeforces Round #650 (Div. 3) E. Necklace Assembly (暴力)

题意:有一个字符串,要求使用其中字符构造一个环(不必全部都用),定义一个环是k美的,如果它转\(k\)次仍是原样,现在给你\(k\),要求最长的k美环的长度.
题解:我们首先看\(k\),如果一个环转\(k\)的因子次是美的,那么\(k\)次也一定是美的,然后再看环,假如一个环最少转\(d\)次是美的,那么这个环的长度\(n\)一定能被\(d\)整除.也就是这个环有\(d\)种不同的数,每种数有\(n/d\)个.其实也就可以把看作是一个循环节构成的,画一画也就知道了.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL; int t;
int n,k;
string s;
map<char,int> mp;
vector<int> v; int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>t;
while(t--){
mp.clear();
v.clear();
cin>>n>>k;
cin>>s;
for(int i=0;i<n;++i){
mp[s[i]]++;
}
for(int i=1;i<=k;++i){
if(k%i==0){
v.pb(i);
}
}
int ans=0;
for(int i=1;i<=n;++i){
for(auto w:v){
if(w%i==0){
ans=max(ans,i); //worst situation
}
if(i%w==0){
int d=i/w;
int cnt=0;
for(char j='a';j<='z';++j){
cnt+=mp[j]/d;
}
if(cnt>=w){
ans=max(ans,i);
}
}
}
}
cout<<ans<<endl; } return 0;
}
Codeforces Round #650 (Div. 3) E. Necklace Assembly (暴力)的更多相关文章
- Codeforces Round #339 (Div. 1) C. Necklace 构造题
C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...
- Codeforces Round #650 (Div. 3) C. Social Distance
题目链接:https://codeforces.com/contest/1367/problem/C 题意 给出一个长为 $n$ 的 $01$字符串,两个相邻 $1$ 间距应大于 $k$,初始序列合法 ...
- Codeforces Round #650 (Div. 3) B. Even Array
题目链接:https://codeforces.com/contest/1367/problem/B 题意 有一大小为 $n$ 的数组 $a$,问能否经过交换使所有元素与下标奇偶性相同(0 - ind ...
- Codeforces Round #650 (Div. 3) A. Short Substrings
题目链接:https://codeforces.com/contest/1367/problem/A 题意 给出一个字符串 $t$,找出原字符串 $s$,$t$ 由 $s$ 从左至右的所有长为 $2$ ...
- Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
- Codeforces Round #650 (Div. 3) D. Task On The Board (构造,贪心)
题意:有一个字符串和一组数,可以对字符串删去任意字符后为数组的长度,且可以随意排序,要求修改后的字符串的每个位置上的字符满足:其余大于它的字符的位置减去当前位置绝对值之和等于对应序列位置上的数. 题解 ...
- Codeforces Round #650 (Div. 3) C. Social Distance (前缀和)
题意:有一排座位,要求每人之间隔\(k\)个座位坐,\(1\)代表已做,\(0\)代表空座,问最多能坐几人. 题解:我们分别从前和从后跑个前缀和,将已经有人坐的周围的位置标记,然后遍历求每一段连续的\ ...
- Codeforces Round #352 (Div. 2) C. Recycling Bottles 暴力+贪心
题目链接: http://codeforces.com/contest/672/problem/C 题意: 公园里有两个人一个垃圾桶和n个瓶子,现在这两个人需要把所有的瓶子扔进垃圾桶,给出人,垃圾桶, ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
随机推荐
- python 2.7.11 环境安装
0 安装依赖: yum install zlib zlib-devel readline-devel sqlite-devel bzip2-devel openssl-devel gdbm-deve ...
- Assuming that agent dropped connection because of access permission
Assuming that agent dropped connection because of access permission
- WIN7系统没有USB驱动和以太网驱动如何操作
| 欢迎关注个人公众号 zclinux_note 第一时间获取关于linux使用的技巧.探索Linux的奥秘 | 今天在单位安装了一台win7纯净版,但是安装完成后发现usb没有反应,插上网 ...
- 电子邮箱、邮件地址、网站地址正则表达式!几个有用的RE、regex、regexp!
几个常用的正则表达式! r"\w[-\w\.]*@\w[-\w]*(\.\w[-\w]*)+" 这个是电子邮件地址的. r"<TAG\b[^>]*<(. ...
- apscheduler(定时任务) 基于redis持久化配置操作
apscheduler(定时任务) 基于redis持久化配置操作 安装模块 pip install apscheduler 导入模块配置 ## 配置redis模块 from apscheduler.j ...
- 在vSphere中为不同服务器配置IPMI功能
在vSphere HA中如果要配置并启用DPM功能,需要记录服务器远程管理接口的IP地址(不是ESXi的IP地址,而是另一个独立的IP地址,是与ESXi服务器同一网段的另一个IP地址)与MAC地址.远 ...
- Ubuntu14.04系统安装
1. 使用U盘或光盘进行引导进入系统安装向导. 2. 安装类型选择,选择中文(简体).然后点安装ubuntu. 3. 安装ubuntu电脑必须接入外网(外网的方式有自动获取或手动编辑IP地址). 网络 ...
- jmeter跳过验证码登录配置:通过手动添加 Cookie 跳过带验证码的登录接口
目录 一.基本配置 二.HTTP请求默认值 三.HTTP信息头管理器 四.HTTP Cookie管理器 五.线程组下接口设置 一.基本配置 二.HTTP请求默认值 (1)jmeter的设置: (2)设 ...
- okhttp踩坑
ResponseBody.string() 平常我们用的客户端http调用工具一般是RestTemplate,HttpClient,okhttp,以前使用过前两者,没咋接触过okhttp,我看公司 ...
- 项目管理/Bug管理/问题管理—Phabricator
项目管理/Bug管理/问题管理-Phabricator 1.项目管理/Bug管理/问题管理工具 2.Phabricator 3.Docker 方式安装Phabricator 3.1Docker方式安装 ...