kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
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').
OutputFor each test case output a integer , how many different necklaces.Sample Input
- 4
- 0110
- 1100
- 1001
- 0011
- 4
- 1010
- 0101
- 1000
- 0001
Sample Output
- 1
- 2
- 本来是各种找特征,然后扩展kmp判,但是找不到,看了题解。学习了最小字符串
- s=“bbaa” 经过循环能够得到4个同构字符串, 其中最小的是 “aabb”
- 如何求最小字符串
- i=0, j=1, k=0
- 如果 s[i] < s[j] 很容易理解 j++;
如果 s[i] > s[j] 也很好理解 i=j;
如果 s[i] == s[j] ,
可以令 k=0, 在i和j之间 找到 第一个s[i+k] != s[j+k]的位置
如果 s[i+k] < s[j+k] 说明i~i+k 都符合,所以 j=j+k+1
如果 s[i+k] > s[j+k] 说明i-i+k 都不符合, 所以 i=i+k+1- 两个注意事项:
第一: i和j不能相等
第二: 每次s[i] != s[j] ,k=0
- #include<stdio.h>
- #include<string.h>
- #include<string>
- #include<set>
- #include<algorithm>
- using namespace std;
- set<string> ss;
- int n,len;
- char s[],t[];
- //最小字符串模板
- int minstring(char* s) {
- int i=,j=,k=;
- while(i<len&&j<len&&k<len) {
- int tmp=s[(i+k)%len]-s[(j+k)%len];
- if(!tmp) k++;
- else {
- if(tmp<) {
- j+=k+;
- } else {
- i+=k+;
- }
- if(i==j) j++;
- k=;
- }
- }
- return min(i,j);
- }
- void getstring(char* str) {//写法很厉害
- str[len/]='\0';
- ss.insert(str); //竟然还能这样
- }
- int main() {
- while(~scanf("%d",&n)) {
- for(int i=;i<n;i++) {
- scanf("%s",t);
- strcpy(s,t);
- strcat(s,t);
- len=strlen(s);
- int k=minstring(s);//得到最小字符串的起始位置
- getstring(s+k);
- }
- printf("%d\n",ss.size());
- ss.clear();
- }
- }
kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
- kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- 注解:@interface 自定义注解的语法
自定义注解: 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节.在定义注解时,不能继承其他的注解或接口 ...
- javascipt——对象的概念——数组
一.Array 特点: 数组的长度是可变的: 数组的索引可以是数字.字符串: 数组的内容可以是任意内容: 可以通过索引获取之前不存在的一个位置,其值为undefined: 1.构造函数: new Ar ...
- DAY10-MYSQL初识
一 数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件 ...
- 今天出现编码出现了No suitable driver found for jdbc
出现这样的情况,一般有四种原因: 一:连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql://local ...
- Ubuntu安装Chrome及hosts修改
Ubuntu16.04 1.chrome安装 获取安装包http://www.google.cn/chrome/browser/desktop/index.html 在安装包目录打开终端执行sudo ...
- Composite模式 组合模式
Android的ViewGroup 和 View 的关系,即是采用组合模式 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件 ...
- Codeforces 1110D Jongmah (DP)
题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 解析:首先我们容易发现,我们 ...
- [转]AJAX工作原理及其优缺点
1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术.它使用:使用XHTML ...
- interface 接口 和多态的含义
<?php //interface关键字用于定义接口 interface ICanEat{ //接口里面的方法不需要方法的实现 public function eat($food) ; } // ...
- C++输出斐波那契数列的几种方法
定义: 斐波那契数列指的是这样一个数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 这个数列从第三项开始,每一项都等于前两项之和. 以输出斐波那 ...