码迷,mamicode.com
首页 > 其他好文 > 详细

[最小表示法] hdu 2609 How many

时间:2014-05-12 06:35:21      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

题目链接:

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,布布扣,bubuko.com

[最小表示法] hdu 2609 How many

标签:des   style   blog   class   code   java   

原文地址:http://blog.csdn.net/cc_again/article/details/25504369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!