Abelian Period
Abelian Period
设SSS是一个数字串,定义函数occ(S,x)occ(S,x)occ(S,x)表示SSS中数字xxx的出现次数。 例如:S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1。 如果对于任意的iii,都有occ(u,i)=occ(w,i)occ(u,i)=occ(w,i)occ(u,i)=occ(w,i),那么我们认为数字串uuu和www匹配。 例如:(1,2,2,1,3)≈(1,3,2,1,2)(1,2,2,1,3)\approx(1,3,2,1,2)(1,2,2,1,3)≈(1,3,2,1,2)。 对于一个数字串SSS和一个正整数kkk,如果SSS可以分成若干个长度为kkk的连续子串,且这些子串两两匹配,那么我们称kkk是串SSS的一个完全阿贝尔周期。 给定一个数字串SSS,请找出它所有的完全阿贝尔周期。
输入的第一行包含一个正整数T(1≤T≤10)T(1\leq T\leq10)T(1≤T≤10),表示测试数据的组数。 对于每组数据,第一行包含一个正整数n(n≤100000)n(n\leq 100000)n(n≤100000),表示数字串的长度。 第二行包含nnn个正整数S1,S2,S3,...,Sn(1≤Si≤n)S_1,S_2,S_3,...,S_n(1\leq S_i\leq n)S1,S2,S3,...,Sn(1≤Si≤n),表示这个数字串。
对于每组数据,输出一行若干个整数,从小到大输出所有合法的kkk。
2
6
5 4 4 4 5 4
8
6 5 6 5 6 5 5 6
3 6
2 4 8
思路:枚举n的因子,然后我们去检验这个是否符合就可以了;
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<set>
7 #include<math.h>
8 #include<map>
9 using namespace std;
10 typedef long long LL;
11 int ans[100005];
12 int cnt[100005];
13 int cnt2[100005];
14 int tx[100005];
15 int ask[100005];
16 int main(void)
17 {
18 int T;
19 scanf("%d",&T);
20 while(T--)
21 {
22 int n;
23 scanf("%d",&n);
24 int i,j;
25 for(i = 1; i <= n; i++)
26 {
27 scanf("%d",&ans[i]);
28 }
29 int cn = 0;
30 for(i = 1; i <= sqrt(1.0*n); i++)
31 {
32 int v = 0;
33 if(n%i==0)
34 {
35 int k = n/i;
36 for(j = 1; j <= i; j++)
37 {
38 if(!cnt[ans[j]])
39 {
40 tx[v++] = ans[j];
41 }
42 cnt[ans[j]]++;
43 }
44 bool flag = false ;
45 int x = i+1;
46 while(true)
47 {
48 for(j = x; j <= i+x-1&& j<=n; j++)
49 {
50 if(!cnt[ans[j]])
51 {
52 flag = true;
53 break;
54 }
55 cnt2[ans[j]]++;
56 }
57 x = j;
58 for(j = 0; j < v; j++)
59 {
60 if(cnt[tx[j]]!=cnt2[tx[j]])
61 {
62 flag = true;
63 }
64 cnt2[tx[j]] = 0;
65 }
66 if(flag || x == n+1)
67 {
68 break;
69 }
70 }
71 for(j = 0; j < v; j++)
72 {
73 cnt[tx[j]] = 0;
74 }
75 if(!flag)ask[cn++] = i;
76 if(i!=n/i)
77 {
78 v = 0;
79 for(j = 1; j <= k; j++)
80 {
81 if(!cnt[ans[j]])
82 {
83 tx[v++] = ans[j];
84 }
85 cnt[ans[j]]++;
86 }
87 bool flag = false ;
88 int x = k+1;
89 while(true)
90 {
91 for(j = x; j <= k+x-1&& j<=n; j++)
92 {
93 if(!cnt[ans[j]])
94 {
95 flag = true;
96 break;
97 }
98 cnt2[ans[j]]++;
99 }
100 x = j;
101 for(j = 0; j < v; j++)
102 {
103 if(cnt[tx[j]]!=cnt2[tx[j]])
104 {
105 flag = true;
106 }
107 cnt2[tx[j]] = 0;
108 }
109 if(flag || x == n+1)
110 {
111 break;
112 }
113 }
114 for(j = 0; j < v; j++)
115 {
116 cnt[tx[j]] = 0;
117 }
118 if(!flag)ask[cn++] = k;
119 }
120 }
121 }
122 ask[cn++] = n;
123 sort(ask,ask+cn);
124 printf("%d",ask[0]);
125 for(i = 1; i < cn; i++)
126 {
127 printf(" %d",ask[i]);
128 }
129 printf("\n");
130 }
131 return 0;
132 }
Abelian Period的更多相关文章
- 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 (BestCoder Round #88 模拟+暴力)
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...
- HDU 5908 Abelian Period 暴力
Abelian Period 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5908 Description Let S be a number st ...
- HDU5908 Abelian Period 暴力
题目大意:将一个数组分成长度为k的几个连续区间,如果每个区间内各个元素出现的次数相同,则称k为一个阿贝尔周期,从小到大打印所有阿贝尔周期,数据间加空格. 题目思路:map+暴力 #include< ...
- HDU 5908 Abelian Period 可以直接用multiset
http://acm.hdu.edu.cn/showproblem.php?pid=5908 要求把数组分成k组使得每组中的元素出现次数相同 就是分成k个集合,那么直接用multiset判定就可以 有 ...
- 【29.27%】【hdu 5908】Abelian Period
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) 问题描述 设SS是一个数字串,定义 ...
- BestCoder #88(1001 1002)
Find Q Accepts: 392 Submissions: 780 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131 ...
- TCP Provider The semaphore timeout period has expired
我们一数据库服务器上有个作业最近几天偶尔会遇到下面错误(敏感信息已做处理),主要是报"TCP Provider: The semaphore timeout period has expir ...
- SSRS 2008 R2 错误:Timeout expired. The timeout period
今天遇到了Reporting Services(SQL SERVER 2008 R2)的报表执行异常情况,报表加载数据很长时间都没有响应,最后报"An error occurred with ...
随机推荐
- 生成随机数的N种方式
首先需要说明的是,计算机中生成的随机数严格来说都是伪随机,即非真正的随机数,真正随机数的随机样本不可重现.那么我们来看看代码中有哪些方式可以生成随机数. rand rand函数声明如下: #inclu ...
- liveBOS环境搭建
环境搭建:1.准备jdk1.6及以上版本oracle11gplsqlsql脚本(oracle_init.sql,oracle_insert.sql)livebos_tomcatlivebos的授权文件 ...
- LetNet、Alex、VggNet分析及其pytorch实现
简单分析一下主流的几种神经网络 LeNet LetNet作为卷积神经网络中的HelloWorld,它的结构及其的简单,1998年由LeCun提出 基本过程: 可以看到LeNet-5跟现有的conv-& ...
- Centos7服务器上RabbitMQ单机安装
一.背景 最近项目中用到了RabbitMQ,但是发现自己本地没有安装,此文记录一下本地RabbitMQ的安装过程.注意不同的系统安装方式略有不同,此处我们记录的是Centos7的安装方式. 二.安装方 ...
- 从源码看Thread&ThreadLocal&ThreadLocalMap的关系与原理
1.三者的之间的关系 ThreadLocalMap是Thread类的成员变量threadLocals,一个线程拥有一个ThreadLocalMap,一个ThreadLocalMap可以有多个Threa ...
- Shell学习(五)—— awk命令详解
一.awk简介 awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报 ...
- ListView的item不能点击(焦点冲突问题)
一般这种问题就是item里面有checkbox或button之类抢占焦点的控件,解决方案有2种: 第一种:就是在checkbox或button添加android:focusable="fal ...
- 链式栈——Java实现
1 package struct; 2 3 //接口 4 interface ILinkStack{ 5 //栈中元素个数(栈大小) 6 int size(); 7 //取栈顶元素 8 Object ...
- LeetCode 36. Valid Sudoku (Medium)
题目 Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according ...
- Linux编译安装、压缩打包、定时任务管理
编译安装 压缩打包 定时任务管理 一.编译安装 使用源代码,编译打包软件 1.特点 1.可以定制软件 2.按需构建软件 2.编译安装 1.下载源代码包 wget https://nginx.org/d ...