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. HP刀片服务器安装系统步骤

    原文链接 HP刀片服务器安装系统步骤: HP刀片使用一个叫OA(Onboard Administrator)的管理平台统一进行管理,具有很高的集成性,同时使用起来也很简单方便,通过IE浏览器输入IP地 ...

  2. "segmentation fault " when "import tensorflow as tf"

    https://github.com/tensorflow/tensorflow/issues/2034

  3. 标准对象 -------JavaScript

    本文摘要:http://www.liaoxuefeng.com/ 在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对 ...

  4. mbstring未安装

    yum install php55w-mbstring.x86_64 把php版本换成自己的就ok

  5. C# 界面跳转-登陆之后跳转至主窗口

    在登陆按钮验证成功之后可以将会话结果改为OK //验证通过之后将对话结果设置为OK(之后会载入主界面) this.DialogResult = DialogResult.OK; this.Dispos ...

  6. vue 自定义动态弹框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Pots POJ - 3414 (搜索+记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special J ...

  8. ES6箭头函数基本用法

    ES6箭头函数基本用法 ``` window.onload = function(){ alert(abc); } //箭头函数 window.onload = ()=>{ alert(&quo ...

  9. tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加

    tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加 大家都知道,在使用tp5的paginate获取分页数据之后,得到的是一个数据对象,但有时会碰到要对数据对象进行二次加工的情况, ...

  10. 二叉树(dfs)

    样例输入: 5        //下面n行每行有两个数 2 3    //第i行的两个数,代表编号为i的节点所连接的两个左右儿子的编号. 4 5 0 0    // 0 表示无 0 0 0 0   样 ...