题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2609
How many
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1225 Accepted Submission(s): 476
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 | We have carefully selected several similar problems for you: 1131 2572 1251 2610 2603
Statistic | Submit | Discuss | Note
题目意思:
给n个有0,1组成的串,求可以经过循环旋转,最后不同的串有多少个。
解题思路:
最小表示法。
先求出每个串的字典序最小的串,存入map里,最后输出map大小即可,也可以存入字典树,最后输出叶子个数。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 220 map<string,int>myp; char save[Maxn]; int n; string cal() { int len=strlen(save); int i=0,j=1; while(i<len&&j<len) { int k=0; while(k<len&&save[(i+k)%len]==save[(j+k)%len]) k++; if(k>=len) break; if(save[(i+k)%len]>save[(j+k)%len]) { i=i+k+1; if(i==j) i++; } else { j=j+k+1; if(i==j) j++; } //printf("i:%d j:%d k:%d\n",i,j,k); //system("pause"); } int tt=min(i,j); int cnt=0; string res; while(cnt<len) { res+=save[(tt+cnt)%len]; cnt++; } return res; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%d",&n)) { myp.clear(); while(n--) { scanf("%s",save); string a=cal(); //cout<<": "<<a<<endl; //system("pause"); myp[a]=1; } printf("%d\n",myp.size()); } return 0; }
[最小表示法] hdu 2609 How many