Beads

题目描述

Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子串),每块有k(k>0)个珠子,如果这条珠子的长度不是k的倍数,最后一块小于k的就不要拉(nc真浪费),保证珠子的长度为正整数。

Zxl喜欢多样的项链,为她应该怎样选择数字k来尽可能得到更多的不同的子串感到好奇,子串都是可以反转的,换句话说,子串(1,2,3)和 (3,2,1)是一样的。

写一个程序,为Zxl决定最适合的k从而获得最多不同的子串。

例如:

这一串珠子是: (1,1,1,2,2,2,3,3,3,1,2,3,3,1,2,2,1,3,3,2,1)

k=1的时候,我们得到3个不同的子串: (1),(2),(3)

k=2的时候,我们得到6个不同的子串: (1,1),(1,2),(2,2),(3,3),(3,1),(2,3) k=3的时候,我们得到5个不同的子串:

(1,1,1),(2,2,2),(3,3,3),(1,2,3),(3,1,2)

k=4的时候,我们得到5个不同的子串:

(1,1,1,2),(2,2,3,3),(3,1,2,3),(3,1,2,2),(1,3,3,2)

输入

共有两行,第一行一个整数n代表珠子的长度,第二行是由空格分开的颜色ai(1<=ai<=n,n<=200005)。

输出

也有两行 第一行两个整数,第一个整数代表能获得的最大不同的子串个数,第二个整数代表能获得最大值的k的个数, 第二行输出所有的k(中间有空格)。

来源

[Poi2010]


好像没有hash的题解,放一篇

暴力枚举k+正反hash

hash的数不能是11,差评。。。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define maxn 200010
#define p 1000000007
using namespace std;
int n,s[maxn],h1[maxn],h2[maxn],c[maxn];
int ans,outp[maxn],tot,t1,t2,sum;
map<int,int>f;
void get(int l,int r)
{
t1=h1[r]-h1[l-1]*c[r-l+1];
t2=h2[l]-h2[r+1]*c[r-l+1];
//cout<<t1<<' '<<t2<<' '<<l<<' '<<r<<endl;
if(!f[t1]&&!f[t2]){
f[t1]=f[t2]=1;
sum++;
}
}
int main(){
cin>>n;
c[0]=1;
for(int i=1;i<=n;i++){
scanf("%d",&s[i]);
h1[i]=h1[i-1]*p+s[i];c[i]=c[i-1]*p;
//cout<<h1[i]<<' '<<c[i]<<endl;
}
for(int i=n;i>=1;i--)h2[i]=h2[i+1]*p+s[i];
for(int k=1;k<=n;k++){
f.clear();sum=0;
for(int i=1;i+k-1<=n;i+=k)get(i,i+k-1);
if(sum>ans){
ans=sum,tot=1,outp[1]=k;
}
else if(sum==ans)outp[++tot]=k;
}
printf("%d %d\n%d",ans,tot,outp[1]);
for(int i=2;i<=tot;i++)printf(" %d",outp[i]);
cout<<endl;
return 0;
}

Beads的更多相关文章

  1. HDU 1817Necklace of Beads(置换+Polya计数)

    Necklace of Beads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  2. 【USACO】beads

    题目: You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, othe ...

  3. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  4. 数学计数原理(Pólya):POJ 1286 Necklace of Beads

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7763   Accepted: 3247 ...

  5. poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)

      Description Beads of red, blue or green colors are connected together into a circular necklace of ...

  6. POJ 1286 Necklace of Beads(项链的珠子)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7874   Accepted: 3290 ...

  7. Necklace of Beads(polya计数)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 3102 ...

  8. POJ1509 Glass Beads

    Glass Beads Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 4314   Accepted: 2448 Descr ...

  9. POJ1509 Glass Beads(最小表示法 后缀自动机)

    Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 4901   Accepted: 2765 Description Once ...

  10. Necklace of Beads (polya定理的引用)

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...

随机推荐

  1. windows下sorl安装

    1. JDK要求 Solr 4.10 要求JDK版本必须是1.7或更高. 2. 下载 下载地址: http://www.apache.org/dyn/closer.cgi/lucene/solr/ 下 ...

  2. React Native 初探

    推荐文章 React Native 简介:用 JavaScript 搭建 iOS 应用 (1) React Native 简介:用 JavaScript 搭建 iOS 应用 (2) React Nat ...

  3. ABAP读取长文本的方法

    SAP中所有的项目文本都存在以下两张数据表中: 1. STXH  抬头项目文本 透明表 2. STXL  明细项目文本   透明表 长文本读取方法 首先在STXH和STXL中根据OBJECT NAME ...

  4. ELFhash - 优秀的字符串哈希算法

    ELFhash - 优秀的字符串哈希算法 2016年10月29日 22:12:37 阅读数:6440更多 个人分类: 算法杂论算法精讲数据结构 所属专栏: 算法与数据结构   版权声明:本文为博主原创 ...

  5. jenkins+svn+pipeline+kubernetes部署java应用(三)

    将jar包.Dockerfile.kubernetes部署yaml文件上传至svn自定义目录 一.生成流水线脚本 二.配置jenkins pipeline构建语句 三.点击构建java工程

  6. day09-函数讲解

    1.如何定义一个函数 s = '华为加油a' def s_len(): i = 0 for k in s: i += 1 print(i) s_len() 这个函数的功能就是输出字符串的长度.但是他只 ...

  7. Volatile小结

    1)Java 中能创建 Volatile 数组吗? 能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引用指向的数组,将会受到 vo ...

  8. 服务器常说的U是什么意思?

    U是英文单词:unit 所说的1U和2U,是服务器的厚度,1U大概是相当于机柜的两个小格子,2U是四个格子.1U大概是4.45厘米(1U=1.75英寸,1英寸=2.54CM).以下这个是图片:

  9. visual studio cl -d1reportSingleClassLayout查看内存f分布

    C:\Users\Administrator\Desktop\cppsrc>cl -d1reportSingleClassLayoutTeacher virtual.cpp 用于 x86 的 M ...

  10. A1075 PAT Judge (25)(25 分)

    A1075 PAT Judge (25)(25 分) The ranklist of PAT is generated from the status list, which shows the sc ...