HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5908
Description
Let S be a number string, and occ(S,x) means the times that number x occurs in S.
i.e. S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1.
String u,w are matched if for each number i, occ(u,i)=occ(w,i) always holds.
i.e. (1,2,2,1,3)≈(1,3,2,1,2).
Let S be a string. An integer k is a full Abelian period of S if S can be partitioned into several continous substrings of length k, and all of these substrings are matched with each other.
Now given a string S, please find all of the numbers k that k is a full Abelian period of S.
Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, the first line of the input contains an integer n(n≤100000), denoting the length of the string.
The second line of the input contains n integers S1,S2,S3,...,Sn(1≤Si≤n), denoting the elements of the string.
Output
For each test case, print a line with several integers, denoting all of the number k. You should print them in increasing order.
Sample Input
2
6
5 4 4 4 5 4
8
6 5 6 5 6 5 5 6
Sample Output
3 6
2 4 8
题意:
给你n个数字,首先你可以以没k个为一段,要求n分成的各个段中数字的数量和个数都相同。
题解:
首先枚举每个可以被n整除的段,然后再判断是否可以即可,枚举sqrt(n)或者n/2暴力都可以。
判断的方法:首先我们先统计每个元素的个数,对于k个元素一组,因为是均分的,所以每组出现多少个就确定了。现在我们从开始往后面扫,首先没k个一组,多少组就确定了,那么我们现在看,在每一段该元素都有每个数字的出现上限。如果超过了那么必然是错误的k划分,同时我们记录下到达该数字上限的数的个数,和之前的统计如果不同返回false。最终返回true,这样就求出。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000+100;
int s[maxn+10];
int rec[maxn+10];
bool fuck[maxn+10];
int db[maxn+10];
int ans[maxn+10];
int n;
int tol = 0;
inline bool sol(int k)
{
int tt = n / k;
memset(db,0,sizeof db);
for (int i = 0; i < tt; i++){
int temp = 0;
for (int j = 1; j <= k; j++){
db[s[i*k+j]]++;
if (db[s[i*k+j]]*tt > rec[s[i*k+j]]*(i+1))
return false;
else if (db[s[i*k+j]]*tt == rec[s[i*k+j]]*(i+1))
temp++;
}
if (temp != tol)
return false;
}
return true;
}
int main()
{
int t;
scanf("%d",&t);
while (t--){
tol = 0;
int ta = 0;
memset(rec,0,sizeof rec) ;
scanf("%d",&n);
for (int i = 1; i <= n; i++){
scanf("%d",&s[i]);
rec[s[i]]++;
}
for (int i = 1; i <= maxn; i++)
if (rec[i] != 0)
tol++;
for (int i = 1; i*2 <= n; i++){
if (n%i == 0){
if (sol(i))
printf("%d ",i);
}
}
printf("%d\n",n);
}
return 0;
}
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)的更多相关文章
- HDU 5908 Abelian Period 暴力
Abelian Period 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5908 Description Let S be a number st ...
- HDU 5908 Abelian Period(暴力+想法题)
传送门 Description Let S be a number string, and occ(S,x) means the times that number x occurs in S. i. ...
- HDU 5908 Abelian Period 可以直接用multiset
http://acm.hdu.edu.cn/showproblem.php?pid=5908 要求把数组分成k组使得每组中的元素出现次数相同 就是分成k个集合,那么直接用multiset判定就可以 有 ...
- BestCoder Round #88
传送门:BestCoder Round #88 分析: A题统计字符串中连续字串全为q的个数,预处理以下或加个cnt就好了: 代码: #include <cstdio> #include ...
- hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Ot ...
- HDU - 5996 树上博弈 BestCoder Round #90
就是阶梯NIM博弈,那么看层数是不是奇数的异或就行了: #include<iostream> #include<cstdio> #include<algorithm> ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- Java笔记:与系统交互、系统相关的类,Object类
1.程序与用户交互 (1)运行一个Java程序的时候要给它提供一个main方法入口,这边分析一下这个main方法的签名public static void main(String[] args);pu ...
- C Socket初探
C Socket初探 前段时间写了个C# Socket初探,这次再写个C语言的Socket博文,运行效果如下: 实现步骤: 1. Server端 #include <stdio.h> // ...
- Broadcast Reveiver作用
Broadcast Reveiver作用以及为何要引入(用来接收系统以及自定义消息的) 在系统内通知和判定执行状态 1,系统执行状态,开机了,TF卡插拔,准备关机,电量低了, 2,自定义执行状态,发消 ...
- 子PID namespace中获取父namespace中pid的方法
在那篇< 使用独立PID namespace防止误杀进程>中的最后,我碰到了一个难题,那就是父PID namespace中的进程无法使用进入子PID namespace中通过echo $$ ...
- AS3中释放优化的几条常识
as3中垃圾和堆弃物如不及时清理,会造成进程的速度方面授予限制,下面讲几点关于释放优化的几条内容. 被删除对象在外部的所有引用一定要被删除干净才能被系统当成垃圾回收处理掉: 父对象内部的子对象被外部其 ...
- 命令版本git 分支篇-----不断更新中
最近应用开发的过程中出现了一个小问题,顺便记录一下原因和方法--命令版本 开发中想看看过去某个版本的代码,我们先查看log git log commit f224a720b8192165a4e70f2 ...
- 在code first结构下的生成的数据迁移文件,upadate-database失败
程序控制台出现 already exist Table "xxx",是由于项目中的Migrations(迁移文件)与连接的mysql数据库里迁移记录表里的数量及名称不一致.
- 结构-行为-样式-angularJs 指令解决IE下无PlaceHolder的问题
最近项目开发的时候遇到一个头疼的问题,在测试IE兼容性的时候,发现placeholder在IE下无效.查网上说也是有各种解决方案,但是都不是我想要的,于是决定自己写一个.思路:placeHolder是 ...
- Node.js中读取文件后用Json.parse方法报错
今天,在调试一个node项目时,发现了一个很大的坑,在此分享给大家! 大家都知道,Json.parse()方法对格式要求是很严格的,格式不对极其容易报错,但是有时候格式看似是正确的也会报错. 比如这一 ...
- HTML5的文档结构
HTML5的文档结构 HTML5简化了许多,它的设计遵循了3个原则:1.兼容性.2.实用性.3.通用访问性 1. header 元素 <header> 标签定义文档或者文档 ...
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5908
Description
Let S be a number string, and occ(S,x) means the times that number x occurs in S.
i.e. S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1.
String u,w are matched if for each number i, occ(u,i)=occ(w,i) always holds.
i.e. (1,2,2,1,3)≈(1,3,2,1,2).
Let S be a string. An integer k is a full Abelian period of S if S can be partitioned into several continous substrings of length k, and all of these substrings are matched with each other.
Now given a string S, please find all of the numbers k that k is a full Abelian period of S.
Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, the first line of the input contains an integer n(n≤100000), denoting the length of the string.
The second line of the input contains n integers S1,S2,S3,...,Sn(1≤Si≤n), denoting the elements of the string.
Output
For each test case, print a line with several integers, denoting all of the number k. You should print them in increasing order.
Sample Input
2
6
5 4 4 4 5 4
8
6 5 6 5 6 5 5 6
Sample Output
3 6
2 4 8
题意:
给你n个数字,首先你可以以没k个为一段,要求n分成的各个段中数字的数量和个数都相同。
题解:
首先枚举每个可以被n整除的段,然后再判断是否可以即可,枚举sqrt(n)或者n/2暴力都可以。
判断的方法:首先我们先统计每个元素的个数,对于k个元素一组,因为是均分的,所以每组出现多少个就确定了。现在我们从开始往后面扫,首先没k个一组,多少组就确定了,那么我们现在看,在每一段该元素都有每个数字的出现上限。如果超过了那么必然是错误的k划分,同时我们记录下到达该数字上限的数的个数,和之前的统计如果不同返回false。最终返回true,这样就求出。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000+100;
int s[maxn+10];
int rec[maxn+10];
bool fuck[maxn+10];
int db[maxn+10];
int ans[maxn+10];
int n;
int tol = 0;
inline bool sol(int k)
{
int tt = n / k;
memset(db,0,sizeof db);
for (int i = 0; i < tt; i++){
int temp = 0;
for (int j = 1; j <= k; j++){
db[s[i*k+j]]++;
if (db[s[i*k+j]]*tt > rec[s[i*k+j]]*(i+1))
return false;
else if (db[s[i*k+j]]*tt == rec[s[i*k+j]]*(i+1))
temp++;
}
if (temp != tol)
return false;
}
return true;
}
int main()
{
int t;
scanf("%d",&t);
while (t--){
tol = 0;
int ta = 0;
memset(rec,0,sizeof rec) ;
scanf("%d",&n);
for (int i = 1; i <= n; i++){
scanf("%d",&s[i]);
rec[s[i]]++;
}
for (int i = 1; i <= maxn; i++)
if (rec[i] != 0)
tol++;
for (int i = 1; i*2 <= n; i++){
if (n%i == 0){
if (sol(i))
printf("%d ",i);
}
}
printf("%d\n",n);
}
return 0;
}
Abelian Period 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5908 Description Let S be a number st ...
传送门 Description Let S be a number string, and occ(S,x) means the times that number x occurs in S. i. ...
http://acm.hdu.edu.cn/showproblem.php?pid=5908 要求把数组分成k组使得每组中的元素出现次数相同 就是分成k个集合,那么直接用multiset判定就可以 有 ...
传送门:BestCoder Round #88 分析: A题统计字符串中连续字串全为q的个数,预处理以下或加个cnt就好了: 代码: #include <cstdio> #include ...
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Ot ...
就是阶梯NIM博弈,那么看层数是不是奇数的异或就行了: #include<iostream> #include<cstdio> #include<algorithm> ...
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
1.程序与用户交互 (1)运行一个Java程序的时候要给它提供一个main方法入口,这边分析一下这个main方法的签名public static void main(String[] args);pu ...
C Socket初探 前段时间写了个C# Socket初探,这次再写个C语言的Socket博文,运行效果如下: 实现步骤: 1. Server端 #include <stdio.h> // ...
Broadcast Reveiver作用以及为何要引入(用来接收系统以及自定义消息的) 在系统内通知和判定执行状态 1,系统执行状态,开机了,TF卡插拔,准备关机,电量低了, 2,自定义执行状态,发消 ...
在那篇< 使用独立PID namespace防止误杀进程>中的最后,我碰到了一个难题,那就是父PID namespace中的进程无法使用进入子PID namespace中通过echo $$ ...
as3中垃圾和堆弃物如不及时清理,会造成进程的速度方面授予限制,下面讲几点关于释放优化的几条内容. 被删除对象在外部的所有引用一定要被删除干净才能被系统当成垃圾回收处理掉: 父对象内部的子对象被外部其 ...
最近应用开发的过程中出现了一个小问题,顺便记录一下原因和方法--命令版本 开发中想看看过去某个版本的代码,我们先查看log git log commit f224a720b8192165a4e70f2 ...
程序控制台出现 already exist Table "xxx",是由于项目中的Migrations(迁移文件)与连接的mysql数据库里迁移记录表里的数量及名称不一致.
最近项目开发的时候遇到一个头疼的问题,在测试IE兼容性的时候,发现placeholder在IE下无效.查网上说也是有各种解决方案,但是都不是我想要的,于是决定自己写一个.思路:placeHolder是 ...
今天,在调试一个node项目时,发现了一个很大的坑,在此分享给大家! 大家都知道,Json.parse()方法对格式要求是很严格的,格式不对极其容易报错,但是有时候格式看似是正确的也会报错. 比如这一 ...
HTML5的文档结构 HTML5简化了许多,它的设计遵循了3个原则:1.兼容性.2.实用性.3.通用访问性 1. header 元素 <header> 标签定义文档或者文档 ...