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. MFC-[转]基于MFC的ActiveX控件开发

    作者:lidan | 出处:博客园 | 2012/3/13 16:10:34 | 阅读22次 ActiveX 控件是基于组件对象模型 (COM) 的可重用软件组件,广泛应用于桌面及Web应用中.在VC ...

  2. 题解 CF20A 【BerOS file system】

    对于此题,我的心近乎崩溃 这道题,注意点没有什么,相信大佬们是可以自己写出来的 我是蒟蒻,那我是怎么写出来的啊 好了,废话少说,开始进入正题 这道题,首先我想到的是字符串的 erase 函数,一边运行 ...

  3. 自动生成 WebApi 在线说明文档。

    1.使用Swashbuckle实现 Swashbuckle 是.NET类库,可以将WebAPI所有开放的控制器方法生成对应SwaggerUI的JSON配置.再通过SwaggerUI 显示出来.类库中已 ...

  4. Oracle下如何收集 Systemstate dump

    2: dump (不包括lock element) 10: dump 11: dump + global cache of RAC 256: short stack (函数堆栈) 258: 256+2 ...

  5. iOS多播Delegate类——GCDMulticastDelegate用法小结

    iOS中通常的delegate模式只能有一个被委托的对象,这样当需要有多个被委托的对象时,实现起来就略为麻烦,在开源库XMPPFramework中提供了一个GCDMulticastDelegate类, ...

  6. Lepus天兔数据库监控系统安装配置

    [root@redis01 ~]# cd /opt/[root@redis01 opt]# wget https://www.apachefriends.org/xampp-files/5.6.36/ ...

  7. node的webserver模板

    const express = require('express'); const swig =require('swig'); const fs = require('fs'); //创建服务器 c ...

  8. formpanel布局的学习

    FormPanel有两种布局:form和column,form是纵向布局,column为横向布局.默认为后者.使用layout属性定义布局类型.对于一个复杂的布局表单,最重要的是正确分割,分割结果直接 ...

  9. 【PHP】根据两地经纬度计算距离

    最近做一个H5活动的项目,有个要求是必须现场玩家才能参与,所以就需要计算玩家位置和活动地点的距离来判断是否在活动现场. 以下是写的一个根据经纬度计算两地距离的方法 1 function getDist ...

  10. Terrorist’s destroy HDU - 4679

    Terrorist’s destroy HDU - 4679 There is a city which is built like a tree.A terrorist wants to destr ...