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 ...
随机推荐
- Celery-4.1 用户指南: Canvas: Designing Work-flows(设计工作流程)
签名 2.0 版本新特性. 刚刚在calling 这一节中学习了使用 delay 方法调用任务,并且通常这就是你所需要的,但是有时候你可能想将一个任务调用的签名传递给另外一个进程或者作为另外一个函数的 ...
- oracle——pl/sql 查询中文乱码
1.查看服务器端编码select userenv('language') from dual;我实际查到的结果为:AMERICAN_AMERICA.AL32UTF82.执行语句 select * fr ...
- LAMP 2.8 php.ini配置文件详解
修改php配置文件,但有时候我们并不知道 php.ini 所在路径,这时候就需要通过命令来查一查在哪里. /usr/local/php/bin/php -i |head 看那一行 Loaded Con ...
- openGL一些概念02
着色器程序 着色器程序对象(Shader Program Object)是多个着色器合并之后并最终链接完成的版本. 如果要使用刚才编译的着色器我们必须把他们链接为一个着色器程序对象,然后在渲染对象的时 ...
- Python之条件语句以及循环
Python代码的缩进规则.具有相同缩进的代码被视为代码块 缩进请严格按照Python的习惯写法:4个空格,不要使用Tab,更不要混合Tab和空格,否则很容易造成因为缩进引起的语法错误. 注意: if ...
- <!doctype html>这个是干什么的???
html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5
- CORS实现跨域Ajax
客户端 #!/usr/bin/env python import tornado.ioloop import tornado.web class MainHandler(tornado.web.Req ...
- 如何设置Win10文件资源管理器默认打开“这台电脑”
摘录自:http://www.ithome.com/html/win10/126066.htm
- cocos2d-js 浏览器与JSB内存管理机制的不同
写这边文章的主要目的是为了理解使用cocos3d-js开发app时,浏览器调试与真机情况不一致的原因 一.浏览器中内存管理机制 HTML5版本运行时,整个游戏只存在JS脚本与一些必要的资源文件,这时候 ...
- SpringBoot10 整合JSP
1 整合JSP 1.1 导入相关依赖 JSP依赖.JSTL依赖 <?xml version="1.0" encoding="UTF-8"?> < ...