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 ...
随机推荐
- 问题:Oracle to_date;结果:oracle常用的时间格式转换
oracle常用的时间格式转换 1:取得当前日期是本月的第几周 SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; T ...
- 配置php的curl模块问题
问题 checking for cURL in default path... not foundconfigure: error: Please reinstall the libcurl dist ...
- Python单例模式剖析
在聊这之前我们首先要明确的是,单例模式在实际中的意义以及在python中具有实现的价值? 当前,相信有很多人支持单例模式,也有不少人反对,尤其是在python中,目前依旧具有很大的争议性.我们要在评论 ...
- Tomcat服务器简介
- css中的hack
1.什么是CSS hack? CSS hack是通过在CSS样式中加入一些特殊的符号,让不同的浏览器识别不同的符号(什么样的浏览器识别什么样的符号是有标准的,CSS hack就是让你记住这个标准),以 ...
- Tensorflow手写数字识别(交叉熵)练习
# coding: utf-8import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #pr ...
- 914D Bash and a Tough Math Puzzle
传送门 分析 用线段树维护区间gcd,每次查询找到第一个不是x倍数的点,如果这之后还有gcd不能被x整除的区间则这个区间不合法 代码 #include<iostream> #include ...
- Luogu 2312 [NOIP2014] 解方程
感觉好无聊. 秦九昭算法:一般地,一元n次多项式的求值需要经过(n+1)*n/2次乘法和n次加法,而秦九韶算法只需要n次乘法和n次加法.在人工计算时,一次大大简化了运算过程.(百度百科) 具体来说怎么 ...
- 《Head First Servlets & JSP》-3-1st servlet MVC demo
项目结构 用户首页 form.html <html> <body> <h1 align='center'>Beer Selection Page</h1> ...
- 【实习项目记录】(四)Android 实现手机验证时,按钮倒计时60s
手机注册一般都会有一个按钮,默认显示获取验证码,点击之后变成xx秒之后重新获取验证码 在网上查到有两种方法可以实现这种功能,一种是自定义一个timeButton,另外一种是利用封装好的60秒获取验证码 ...