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 ...
随机推荐
- 第六章 Java性能调优工具(待续)
Java性能调优工具 Windows工具 JDK命令行工具 JConsole工具 Visual VM多合一工具 Visual VM对QQL的支持 MAT内存分析工具 MAT对QQL的支持 JProfi ...
- SQLServer SDK
https://www.cnblogs.com/Leo_wl/p/3451865.html 回到目录 SQLSERVER一些公用DLL的作用解释 如果你的SQLSERVER安装在C盘的话,下面的路径就 ...
- LAMP 2.4 Apache访问控制
通过查看日志发现有个IP 恶意攻击你的网址,可以控制这个IP的访问. 打开主配置文件复制模板. vim /usr/local/apache2/conf/httpd.conf 搜索 /Order 复制 ...
- mysql工具Navicat批量执行SQL语句
例如:我现在要同时执行这么多语句 update community set xqmc=replace(xqmc,' ',''); update community set xqbm=replace(x ...
- JVM知识点总览
jvm 总体梳理 jvm体系总体分四大块: 类的加载机制 jvm内存结构 GC算法 垃圾回收 GC分析 命令调优 当然这些知识点在之前的文章中都有详细的介绍,这里只做主干的梳理 这里画了一个思维导图, ...
- 向linux内核增加一个系统调用-1
验证编辑编译内核的流程,并增加新的系统调用 注意:需要/目录至少10GB空间,/boot目录500MB空间 下载内核并解压 kernel下载 百度云搬运 密码: qc8b 进入 /usr/src目录 ...
- JAVA基础知识总结13(同步)
好处:解决了线程安全问题. 弊端:相对降低性能,因为判断锁需要消耗资源,还容易产生了死锁. 定义同步是有前提的: 1,必须要有两个或者两个以上的线程,才需要同步. 2,多个线程必须保证使用的是同一个锁 ...
- vue-resource基础介绍
1.vue-resource 的请求api是按照rest风格设计的,它提供了7种请求api get(url, [data], [options]); head(url,[data],[options] ...
- Cocos2d-js 热更新学习笔记
转载至: http://blog.csdn.net/pt_xxj/article/details/68927705 为什么还要再写一篇关于cocos2d js热更新的笔记,最单纯的想法就是记录心得,另 ...
- C++面向对象类的实例题目九
题目描述: 编写一个学生和老师数据输入和显示程序,学生数据有编号.姓名.班号和成绩,教师数据有编号.姓名.职称和部门. 要求将编号.姓名.输入和显示设计成一个类person,并作为学生数据操作类stu ...