hdu2609 How many

地址: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): 2625    Accepted Submission(s): 1135


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
 
思路:最大最小表示法+set
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <set>
 5 #include <string>
 6 using namespace std;
 7 
 8 #define MP make_pair
 9 #define PB push_back
10 typedef long long LL;
11 const double eps=1e-8;
12 const int K=1e6+7;
13 const int mod=1e9+7;
14 
15 char sb[205];
16 //ff为真表示最小,为假表示最大
17 //S串应该为原串复制两次后的字符串
18 int mx_mi_express(char *S,bool ff,int len)
19 {
20     int i=0,j=1,k;
21     while(i<len&&j<len)
22     {
23         k=0;
24         while(k<len&&S[i+k]==S[j+k]) k++;
25         if(k==len)  return i<=j?i:j;
26         if((ff&&S[i+k]>S[j+k]) || (!ff&&S[i+k]<S[j+k]))
27         {
28             if(i+k+1>j) i=i+k+1;
29             else    i=j+1;
30         }
31         else if((ff&&S[i+k]<S[j+k]) || (!ff&&S[i+k]>S[j+k]))
32         {
33             if(j+k+1>i) j=j+k+1;
34             else    j=i+1;
35         }
36     }
37     return i<=j?i:j;
38 }
39 string tmp;
40 set<string>st;
41 int main(void)
42 {
43     int t,n,len;
44     while(scanf("%d",&n)==1&&n)
45     {
46         st.clear(),tmp.clear(),len=0;
47         for(int i=1,be;i<=n;i++)
48         {
49             scanf("%s",sb);
50             if(!len)
51             {
52                 len=strlen(sb);
53                 for(int j=0;j<len;j++)
54                     tmp+='0';
55             }
56             for(int j=0;j<len;j++)
57                 sb[j+len]=sb[j];
58             be=mx_mi_express(sb,1,len);
59             for(int j=0;j<len;j++)
60                 tmp[j]=sb[j+be];
61             st.insert(tmp);
62         }
63         printf("%d\n",st.size());
64     }
65     return 0;
66 }

 

posted @ 2017-04-05 19:42  weeping  阅读(208)  评论(0编辑  收藏  举报