Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me
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.

InputThe input contains multiple test cases.

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

  1. 4
  2. 0110
  3. 1100
  4. 1001
  5. 0011
  6. 4
  7. 1010
  8. 0101
  9. 1000
  10. 0001

Sample Output

  1. 1
  2. 2
  3.  
  4. 本来是各种找特征,然后扩展kmp判,但是找不到,看了题解。学习了最小字符串
  5.  
  6. s=“bbaa 经过循环能够得到4个同构字符串, 其中最小的是 aabb
  7.  
  8. 如何求最小字符串
  9.  
  10. i=0, j=1, k=0
  11.  
  12. 如果 s[i] < s[j] 很容易理解 j++;
    如果 s[i] > s[j] 也很好理解 i=j
    如果 s[i] == s[j]
    可以令 k=0, ij之间 找到 第一个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
  13.  
  14. 两个注意事项:
    第一: ij不能相等
    第二: 每次s[i] != s[j] k=0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<string>
  4. #include<set>
  5. #include<algorithm>
  6. using namespace std;
  7. set<string> ss;
  8. int n,len;
  9. char s[],t[];
  10.  
  11. //最小字符串模板
  12. int minstring(char* s) {
  13. int i=,j=,k=;
  14. while(i<len&&j<len&&k<len) {
  15. int tmp=s[(i+k)%len]-s[(j+k)%len];
  16. if(!tmp) k++;
  17. else {
  18. if(tmp<) {
  19. j+=k+;
  20. } else {
  21. i+=k+;
  22. }
  23. if(i==j) j++;
  24. k=;
  25. }
  26. }
  27. return min(i,j);
  28. }
  29.  
  30. void getstring(char* str) {//写法很厉害
  31. str[len/]='\0';
  32. ss.insert(str); //竟然还能这样
  33. }
  34.  
  35. int main() {
  36. while(~scanf("%d",&n)) {
  37. for(int i=;i<n;i++) {
  38. scanf("%s",t);
  39. strcpy(s,t);
  40. strcat(s,t);
  41. len=strlen(s);
  42. int k=minstring(s);//得到最小字符串的起始位置
  43. getstring(s+k);
  44. }
  45. printf("%d\n",ss.size());
  46. ss.clear();
  47. }
  48. }

kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)的更多相关文章

  1. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  3. 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 ...

  4. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  5. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 注解:@interface 自定义注解的语法

      自定义注解: 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节.在定义注解时,不能继承其他的注解或接口 ...

  2. javascipt——对象的概念——数组

    一.Array 特点: 数组的长度是可变的: 数组的索引可以是数字.字符串: 数组的内容可以是任意内容: 可以通过索引获取之前不存在的一个位置,其值为undefined: 1.构造函数: new Ar ...

  3. DAY10-MYSQL初识

    一 数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件 ...

  4. 今天出现编码出现了No suitable driver found for jdbc

    出现这样的情况,一般有四种原因: 一:连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql://local ...

  5. Ubuntu安装Chrome及hosts修改

    Ubuntu16.04 1.chrome安装 获取安装包http://www.google.cn/chrome/browser/desktop/index.html 在安装包目录打开终端执行sudo  ...

  6. Composite模式 组合模式

    Android的ViewGroup 和 View 的关系,即是采用组合模式 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件 ...

  7. Codeforces 1110D Jongmah (DP)

    题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 解析:首先我们容易发现,我们 ...

  8. [转]AJAX工作原理及其优缺点

    1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术.它使用:使用XHTML ...

  9. interface 接口 和多态的含义

    <?php //interface关键字用于定义接口 interface ICanEat{ //接口里面的方法不需要方法的实现 public function eat($food) ; } // ...

  10. C++输出斐波那契数列的几种方法

    定义: 斐波那契数列指的是这样一个数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 这个数列从第三项开始,每一项都等于前两项之和. 以输出斐波那 ...