HDU 2609 How many(最小表示法+set判重两种写法)

How many

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1028    Accepted Submission(s): 403


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
 

                   题目大意:题目意思很好懂,给你n个串,这n个串都可以循环移位,很多都是同构的,求出有多少不同的,经过变换可以相等的不算。有点像化学里的异构。。。

            解题思路:开始的思路就是把每个串都复制两遍然后用自己的最小表示复制串不用%的最小表示不过这样在strcpy和strcat里面用的时间蛮多的,后来就直接用了%的最小表示,但是在sort排序的时候又纠结了很久。最后无奈把开始的char二维数组换成了结构体,26ms秒过。 在网上看了一下,set判重也可以做,也是26ms,详解见代码。

           题目地址:How many

AC代码1:
直接排序方法
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;
char a[10005][105];
char p[105];
//sort不能直接排二维的char?
int n;
struct node
{
     char str[105];
};
node b[10005]; //每一串都是最小表示

int cmp(node no1,node no2)
{
     if(strcmp(no1.str,no2.str)<0)
          return 1;
     return 0;
}

void getmin()
{
    int i,j,k,l;
    for(l=0;l<n;l++)
    {
         int len=strlen(a[l]);
         i=0,j=1,k=0;
         while(i<len&&j<len&&k<len)
         {
            int t=a[l][(i+k)%len]-a[l][(j+k)%len];
            if(t==0)
              k++;
            else if(t<0)
            {
               j=j+k+1;
               k=0;
            }
            else
            {
                 i=i+k+1;
                 k=0;
            }
            if(i==j)
               j++;
         }

         //i就是最小位置,然后再把最小表示给b就可以了
         int pos=-1;
         for(k=i;k<len;k++)
            b[l].str[++pos]=a[l][k];
         for(k=0;k<i;k++)
            b[l].str[++pos]=a[l][k];
         b[l].str[len]='\0';
         //cout<<b[l].str<<endl;
    }
}

int main()
{
     int i;
     while(~scanf("%d",&n))
     {
           int cnt=1;
           for(i=0;i<n;i++)
               scanf("%s",a[i]);
           getmin();  //得到每一串的最小表示
           sort(b,b+n,cmp);
           for(i=1;i<n;i++)
               if(strcmp(b[i].str,b[i-1].str)!=0)
                  cnt++;
           printf("%d\n",cnt);
     }
     return 0;
}


AC代码2:
最小表示+set
set的操作可以参考:STL set基本操作
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<set>
using namespace std;
char a[10005][105];
char str[105];
set <string> mq;
int cnt,n;

void getmin()
{
    mq.clear();
    cnt=0;
    int i,j,k,l;
    for(l=0;l<n;l++)
    {
         int len=strlen(a[l]);
         i=0,j=1,k=0;
         while(i<len&&j<len&&k<len)
         {
            int t=a[l][(i+k)%len]-a[l][(j+k)%len];
            if(t==0)
              k++;
            else if(t<0)
            {
               j=j+k+1;
               k=0;
            }
            else
            {
                 i=i+k+1;
                 k=0;
            }
            if(i==j)
               j++;
         }
         //i就是最小位置,然后再把最小表示给b就可以了
         int pos=-1;
         for(k=i;k<len;k++)
            str[++pos]=a[l][k];
         for(k=0;k<i;k++)
            str[++pos]=a[l][k];
         str[len]='\0';

         //下面就是set的用法了
         if(!mq.count(str))
         {
              cnt++;
              mq.insert(str);
         }
    }
}

int main()
{
     int i;
     while(~scanf("%d",&n))
     {
           for(i=0;i<n;i++)
               scanf("%s",a[i]);
           getmin();  //得到每一串的最小表示
           printf("%d\n",cnt);
     }
     return 0;
}

                  如果要单个单个复制,千万不能选择string。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值