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