题目链接: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.对所有字符串的最小表示进行排序,然后统计。
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MAXN = 1e4+; char s[MAXN][], tmp[]; int cmp(const void *a,const void *b)
{
return (strcmp((char*)a,(char*)b));
} int getmin(char *s, int len) //返回最小表示法的始端
{
int i = , j = , k = ;
while(i<len && j<len && k<len)
{
int t = s[(i+k)%len]-s[(j+k)%len];
if (!t) k++;
else
{
if (t>) i += k+;
else j += k+;
if (i==j) j++;
k = ;
}
}
return i<j?i:j;
} int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
{
scanf("%s", tmp);
int len = strlen(tmp);
int k = getmin(tmp, len);
for(int j = ; j<len; j++)
s[i][j] = tmp[(k+j)%len];
s[i][len] = ; //!!
}
qsort(s+, n, sizeof(s[]), cmp); int ans = ;
for(int i = ; i<=n; i++)
if(i== || strcmp(s[i], s[i-]))
ans++; printf("%d\n", ans);
}
}

最新文章

  1. struts2笔记(2)
  2. 2015.4.23 贪吃蛇、canvas动画,各种上传工具,url信息匹配以及最全前端面试题等
  3. 2015年我国IT行业发展趋势分析(转)
  4. Google V8编程详解(三)Handle &amp; HandleScope
  5. 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
  6. SortedMap接口
  7. 直接拿来用!最火的Android开源项目(一) (转)
  8. Linux摄像头驱动学习之:(六)UVC-基本框架代码分析
  9. X5学习笔记—给单元格添加颜色
  10. SAP接口编程 之 JCo3.0系列(05) : Exception Handling
  11. 命令 crontab
  12. 三栏布局之 css3 calc和 flex
  13. 管理外部表(External Tables)
  14. luogu3645 [Apio2015]雅加达的摩天大楼 (分块+dijkstra)
  15. 完全卸载MySQL
  16. 解决libvlc_media_player_stop时死锁的方法
  17. PHP搜索中的sql注入
  18. LeeTCode题解之Remove Duplicates from Sorted List
  19. 学习7__STM32--SPI外设之双机通信---
  20. Push rejected: Push master to origin/master was rejected /failed to push some refs to /git did not exit cleanly

热门文章

  1. jenkins换端口号
  2. Day 13 Python 一之helloworld
  3. @OneToMany
  4. ThreadGroup
  5. 洛谷 P4318 完全平方数
  6. Angular2.X 笔记
  7. 使用Python写的第一个网络爬虫程序
  8. Dynamics CRM 2015中的SSRS Report集成配置
  9. npm的安装和更新
  10. 基于bootstrap_后台管理