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 ...
随机推荐
- cocos2d(x) HTML label ;CCHTML CCHTMLLabel
这几天由于特殊需要,写了一个HTMLLabel.可以直接支持HTML的几种格式,<font> <a href> color size 等等. 参考object C的一个ios开 ...
- 学习ORM框架—hibernate(三):跟踪持久化对象状态,掌握对象持久化
准备工作 在上篇博客中学习ORM框架—hibernate(一):初识hibernate,通过简单的实例说明O和R的映射过程.本篇博客将要介绍hibernate中持久化对象的状态,并使用hibernat ...
- .NET基础——基本概念
1. .NET.C#(sharp)和JAVA .net是一种多语言的平台,开发.net可以用多达几十种语言进行开发. C#(sharp)是一种编程语言,可开发基于.net平台的应用. Java既是 ...
- 结构-行为-样式-Js排序算法之 快速排序
快速排序算法,是我的算法系列博客中的第二个Js实现的算法,主要思路: 在一个数组中随机取一个数(一般都取第一个或者最后一个),使这个数与数组中其他数进行比较,如果比它大就放到它的右边,比它小就放 ...
- 结构-行为-样式-AngularJs--指令实现 手动触发响应到页面指定位置
最近工作需要增强用户体验,项目经理说下拉框架用户体验太差,于是乎我开始想了如下指令.这个指令可以让用户点击的时候把下拉或者其他的响应显示的东西不会因为屏幕的滚动而看不见,也就是让用户看见他想看见的. ...
- C++ std::stack
std::stack template <class T, class Container = deque<T> > class stack; LIFO stack Stack ...
- SAP CRM 高效调试方法
调试,是程序开发中的基本技巧.快速定位错误消息在源代码中的位置,对发现和解决程序中的问题有着重要的意义.在SAP CRM中,错误消息通常在前台的Web Client页面中展示,应该怎样定位相关代码的位 ...
- SQL如何获取时间的方法?
getdate():当前系统日期与时间 DATEADD(DAY,5,GETDATE()):当前日期的基础上加上x天 DATEDIFF(DAY,'2017-01-02','2017-01-13'):返回 ...
- Perception(1.2)
4.1.2 Definition of Coordinate Systems The global coordinate system is described by its origin lying ...
- 第一百三十五节,JavaScript,封装库--拖拽
JavaScript,封装库--拖拽 封装库新增1个拖拽方法 /** tuo_zhuai()方法,将一个弹窗元素实现拖拽功能 * 注意:一般需要在css文件将元素里的某一个区块光标设置成提示可以拖拽, ...
题目链接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 ...
这几天由于特殊需要,写了一个HTMLLabel.可以直接支持HTML的几种格式,<font> <a href> color size 等等. 参考object C的一个ios开 ...
准备工作 在上篇博客中学习ORM框架—hibernate(一):初识hibernate,通过简单的实例说明O和R的映射过程.本篇博客将要介绍hibernate中持久化对象的状态,并使用hibernat ...
1. .NET.C#(sharp)和JAVA .net是一种多语言的平台,开发.net可以用多达几十种语言进行开发. C#(sharp)是一种编程语言,可开发基于.net平台的应用. Java既是 ...
快速排序算法,是我的算法系列博客中的第二个Js实现的算法,主要思路: 在一个数组中随机取一个数(一般都取第一个或者最后一个),使这个数与数组中其他数进行比较,如果比它大就放到它的右边,比它小就放 ...
最近工作需要增强用户体验,项目经理说下拉框架用户体验太差,于是乎我开始想了如下指令.这个指令可以让用户点击的时候把下拉或者其他的响应显示的东西不会因为屏幕的滚动而看不见,也就是让用户看见他想看见的. ...
std::stack template <class T, class Container = deque<T> > class stack; LIFO stack Stack ...
调试,是程序开发中的基本技巧.快速定位错误消息在源代码中的位置,对发现和解决程序中的问题有着重要的意义.在SAP CRM中,错误消息通常在前台的Web Client页面中展示,应该怎样定位相关代码的位 ...
getdate():当前系统日期与时间 DATEADD(DAY,5,GETDATE()):当前日期的基础上加上x天 DATEDIFF(DAY,'2017-01-02','2017-01-13'):返回 ...
4.1.2 Definition of Coordinate Systems The global coordinate system is described by its origin lying ...
JavaScript,封装库--拖拽 封装库新增1个拖拽方法 /** tuo_zhuai()方法,将一个弹窗元素实现拖拽功能 * 注意:一般需要在css文件将元素里的某一个区块光标设置成提示可以拖拽, ...