HDU——2609How many(字符串的最小表示法+substr)
How many
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1852 Accepted Submission(s): 763
How many kinds of necklaces total have.(if two necklaces can equal by rotating ,we say the two necklaces are some).
For example 0110 express a necklace, you can rotate it. 0110 -> 1100 -> 1001 -> 0011->0110.
Each test case include: first one integers n. (2<=n<=10000)
Next n lines follow. Each line has a equal length character string. (string only include '0','1').
0110
1100
1001
0011
4
1010
0101
1000
0001
2
题意:输入n串二进制的字符串,将其看成项链,问有多少种项链,第一次WA了发现并不是直接map就可以做的,还要将字符串先转换成一种统一的,比如abcde(为了方便表示就用字母了)与cdeab是一样的,相当于把这串字符串头尾连起来,从不同的视角看过去就显示不一样了,那重点就是把这些不同角度的项链统一成一个角度的。按照最小字典序进行统一。最小表示法的模版是网上找的,但是这个模版只能返回一个开始位置整数,拿来修改了一下就可以了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<set>
#include<map>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
string zxc(string s,const int &l)
{
int i = 0, j = 1, k = 0, t;
while(i < l && j < l && k < l)
{
t = s[(i + k) >= l ? i + k - l : i + k] - s[(j + k) >= l ? j + k - l : j + k];
if(!t) k++;
else
{
if(t > 0) i = i + k + 1;
else j = j + k + 1;
if(i == j) ++ j;
k = 0;
}
}
int q=min(i,j);//这里本要返回q(最小表示的起始位置),这里先利用一下
s+=s;//首先要把字符串自增一下
s=s.substr(q,l);//(起始位置,原来长度)
return s;//返回最小表示的字符串
}
int main (void)
{
string s;
int t,n;
while (cin>>n)
{
map<string,int>list;//无脑map开始了......
for (int i=0; i<n; i++)
{
cin>>s;
list[zxc(s,s.size())]=0;
}
cout<<list.size()<<endl;
}
return 0;
}
HDU——2609How many(字符串的最小表示法+substr)的更多相关文章
- HDU 3374 exkmp+字符串最大最小表示法
题意 找到一个字符串中最先出现的最小(大)表示位置,和最小(大)表示串出现次数 分析 用最小(大)表示法求出最先出现的最小(大)表示位置,然后将串长扩两倍用exkmp找出现次数. Code #incl ...
- HDU 4162 Shape Number(字符串,最小表示法)
HDU 4162 题意: 给一个数字串(length <= 300,000),数字由0~7构成,求出一阶差分码,然后输出与该差分码循环同构的最小字典序差分码. 思路: 第一步是将差分码求出:s[ ...
- hdu 3374 String Problem(kmp+最小表示法)
Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...
- HDU 4162 Shape Number (最小表示法)
题意:给你一串n个数,求出循环来看一阶差的最小字典序:数字串看成一个顺时针的环,从某一点开始顺时针循环整个环,保证字典序最小就是答案 例如给你 2 1 3 就会得到(1-2+8 注意题意负数需要加8) ...
- HDU3374 字符串最大最小表示法模板
一开始没太看懂什么意思,拿笔反复推了一遍才大概知道最大最小表示法是怎么求的,感觉太神奇了... #include <iostream> #include <cstdio> #i ...
- hdu 3374 String Problem(最小表示法+最大表示法+kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...
- hdu 2609 How many(最小表示法)
Problem Description Give you n ( n < 10000) necklaces ,the length of necklace will not large than ...
- bzoj5130 字符串的周期(kmp,最小表示法)
bzoj5130 字符串的周期(kmp,最小表示法) bzoj 题解时间 m很大,n很小. 周期很容易求,就是kmp之后n-fail[n]. 之后对于枚举所有的字符串用最小表示法,暴力搜索. 能过就完 ...
- hdu3374 kmp+最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
随机推荐
- MovieReview—Ghost in the Shell 2: Innocence(攻壳机动队2:无罪)
Doll killing event The movie was developed around a series of doll murders. Barthes and o ...
- ansible 通过堡垒机/跳板机 访问目标机器需求实战(ssh agent forward)
一. 需求背景: 在我们使用ansible的过程中经常会遇到这样的情况,我们要管理的机器都在内网中,这些内网机器的登录都是通过跳板机或者堡垒机登录.我们的ansible机器不能直接管理到这些后端的机器 ...
- Harvest of Apples
问题 B: Harvest of Apples 时间限制: 1 Sec 内存限制: 128 MB提交: 18 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...
- python_89_configparser模块
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为ConfigPsresr 来看一个好多软件的常见文档格式如下 [ ...
- python之道09
整理函数相关知识点,写博客. 看代码写结果 1. def func(): for i in range(3): print(i) return 666 print(func()) # 0 1 2 66 ...
- 201621123080《java程序设计》第14周实验总结
201621123080<java程序设计>第14周实验总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2. ...
- CentOS 系统下Gitlab搭建与基本配置 以及代码备份迁移过程
GitLab 是一个开源的版本管理系统,提供了类似于 GitHub 的源代码浏览,管理缺陷和注释等功能,你可以将代码免费托管到 GitLab.com,而且不限项目数量和成员数.最吸引人的一点是,可以在 ...
- Anaconda安装和环境的搭建
Anaconda安装 在官网上下载最新的Anaconda https://www.anaconda.com/distribution/ 我使用的是2018.12,Python 3.7这个版本的. 安装 ...
- python的标准模块
本文用于记录python中的标准模块,随时更新. decimal模块(解决小数循环问题): >>> import decimal >>> a = decimal.D ...
- Applied Nonparametric Statistics-lec8
Ref:https://onlinecourses.science.psu.edu/stat464/print/book/export/html/11 additive model value = t ...