首页 > 代码库 > [最小表示法] hdu 2609 How many
[最小表示法] hdu 2609 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): 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.
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
Sample Output
1 2
Author
yifenfei
Source
奋斗的年代
Recommend
yifenfei | We have carefully selected several similar problems for you: 1131 2572 1251 2610 2603
题目意思:
给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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。