题目链接:https://vjudge.net/problem/HDU-2609

How many

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3272    Accepted Submission(s): 1457

Problem Description
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.
 
Input
The 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').
 
Output
For 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
 
Author
yifenfei
 
Source
 
Recommend
yifenfei
 
 
 
题解:
1.求出每个字符串的最小表示法。
2.对所有字符串的最小表示进行排序,然后统计。
 
 
代码如下:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <sstream>
  11. #include <algorithm>
  12. using namespace std;
  13. typedef long long LL;
  14. const double eps = 1e-;
  15. const int INF = 2e9;
  16. const LL LNF = 9e18;
  17. const int MAXN = 1e4+;
  18.  
  19. char s[MAXN][], tmp[];
  20.  
  21. int cmp(const void *a,const void *b)
  22. {
  23. return (strcmp((char*)a,(char*)b));
  24. }
  25.  
  26. int getmin(char *s, int len) //返回最小表示法的始端
  27. {
  28. int i = , j = , k = ;
  29. while(i<len && j<len && k<len)
  30. {
  31. int t = s[(i+k)%len]-s[(j+k)%len];
  32. if (!t) k++;
  33. else
  34. {
  35. if (t>) i += k+;
  36. else j += k+;
  37. if (i==j) j++;
  38. k = ;
  39. }
  40. }
  41. return i<j?i:j;
  42. }
  43.  
  44. int main()
  45. {
  46. int n;
  47. while(scanf("%d", &n)!=EOF)
  48. {
  49. for(int i = ; i<=n; i++)
  50. {
  51. scanf("%s", tmp);
  52. int len = strlen(tmp);
  53. int k = getmin(tmp, len);
  54. for(int j = ; j<len; j++)
  55. s[i][j] = tmp[(k+j)%len];
  56. s[i][len] = ; //!!
  57. }
  58. qsort(s+, n, sizeof(s[]), cmp);
  59.  
  60. int ans = ;
  61. for(int i = ; i<=n; i++)
  62. if(i== || strcmp(s[i], s[i-]))
  63. ans++;
  64.  
  65. printf("%d\n", ans);
  66. }
  67. }

HDU2609 How many —— 最小表示法的更多相关文章

  1. hdu2609 How many【最小表示法】【Hash】

    How many Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. hdu2609 How many 字典树+最小表示法

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell meHow many ...

  3. hdu2609 最小表示法

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  4. HDU 4162 Shape Number (最小表示法)

    题意:给你一串n个数,求出循环来看一阶差的最小字典序:数字串看成一个顺时针的环,从某一点开始顺时针循环整个环,保证字典序最小就是答案 例如给你 2 1 3 就会得到(1-2+8 注意题意负数需要加8) ...

  5. POJ 1635 树的最小表示法/HASH

    题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...

  6. HDU 2609 最小表示法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609 题意:给定n个循环链[串],问有多少个本质不同的链[串](如果一个循环链可以通过找一个起点使得和 ...

  7. HDU 4162 最小表示法

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4162 题意:给定一个只有0-7数字组成的串.现在要由原串构造出一个新串,新串的构造方法:相邻2个位置的数字 ...

  8. POJ 1509 最小表示法

    题目链接:http://poj.org/problem?id=1509 题意:给定一个字符串,求一个起点使字符串从该起点起的字符串字典序最小[题目的字符串起点从1开始] 思路:最小表示法模板题 #de ...

  9. UVA 1314 最小表示法

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36117 题意:给定长度为n的字符串,求一个起点使字符串从该起点起的 ...

随机推荐

  1. 大数(bzoj 4542)

    /* 想了半天莫队,不知道咋转移,需要动下脑子. 有一个很显然的结论是如果(l,r)是P的倍数,那么s[l...n]%P=s[r+1...n]%P. 根据这个东西,我们预处理出所有的后缀%P的余数,接 ...

  2. php——配合QQ邮箱发送邮件

    最近做一个域名管理系统的项目,实现在域名还有三十天的时候系统发送邮件到QQ邮箱从而提醒续费: 这里运用到了phpmailer; 1:getAll函数: function getAll($con,$sq ...

  3. AC日记——草地排水 codevs 1993

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在农夫约翰的农场上,每 ...

  4. Java 新手的通病

    一:对算法和数据结构不熟悉 为什么我先拿“数据结构和算法”说事捏?这玩意是写程序最最基本的东东.不管你使用 Java 还是其它的什么语言,都离不开它.而且这玩意是跨语言的,学好之后不管在哪门语言中都能 ...

  5. T1079 回家 codevs

    http://codevs.cn/problem/1079/ 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver~死坑 题目描述 Description 现在是晚 ...

  6. STM32F10x_StdPeriph_Driver_3.5.0(中文版).chm的使用

    以熟悉的固件库函数说明中函数GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_IintTypeDef *GPIO_InitStructure)为例 GPIOA...G       ...

  7. SPOJ 26108 TRENDGCD - Trending GCD

    Discription Problem statement is simple. Given A and B you need to calculate S(A,B) . Here, f(n)=n, ...

  8. Python资料大全

    说明:以下文章为转载,有英文原文和中文整理翻译,对原作者和译者的工作表示极大感谢!!! 英文原文:https://github.com/vinta/awesome-python 中文译文:https: ...

  9. 洛谷 P1034 矩形覆盖

    P1034 矩形覆盖 题目描述 在平面上有nn个点(n \le 50n≤50),每个点用一对整数坐标表示.例如:当 n=4n=4 时,44个点的坐标分另为:p_1p1​(1,11,1),p_2p2​( ...

  10. Spring拦截器从Request中获取Json格式的数据

    7 package com.newpp.core.interceptor; 8 9 import java.io.BufferedReader; 10 import java.io.ByteArray ...