• HDU 2609 How many


    How many

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


    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
    题目大意:一串由0和1组成的环形珠子,问共有多少串不同的珠子。
    解题方法:先用最小表示法求出每个字符串最小字典序的字符串,然后统计不同字符串的数量。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    char str[10001][105];
    
    int cmp(const void *p1, const void *p2)
    {
        return strcmp((char *)p1, (char *)p2);
    }
    
    int Getmin(char str[])
    {
        int nLen = strlen(str);
        int i = 0, j = 1, k;
        while (i < nLen && j < nLen)
        {
            for (k = 0; k < nLen; k++)
            {
                if (str[(i + k) % nLen] != str[(j + k) % nLen])
                {
                    break;
                }
            }
            if (str[(i + k) % nLen] > str[(j + k) % nLen])
            {
                i += k + 1;
            }
            else
            {
                j += k + 1;
            }
            if (i == j)
            {
                j++;
            }
        }
        return min(i, j);
    }
    
    int main()
    {
        int n;
        char strtemp[105];
        int ans = 1;
        while(scanf("%d", &n) != EOF)
        {
            ans = 1;
            for (int i = 0; i < n; i++)
            {
                scanf("%s", strtemp);
                int nLen = strlen(strtemp);
                int index = Getmin(strtemp);
                int nCount = 0;
                for (int j = index; j < nLen; j++)
                {
                    str[i][nCount++] = strtemp[j];
                }
                for (int j = 0; j < index; j++)
                {
                    str[i][nCount++] = strtemp[j];
                }
                str[i][nCount] = '';
                nCount = 0;
            }
            qsort(str, n, sizeof(str[0]), cmp);
            for (int i = 0; i < n - 1; i++)
            {
                if (strcmp(str[i], str[i + 1]) != 0)
                {
                    ans++;
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Java.Util.List(List接口)
    在VMware安装Centos7
    java中原生的发送http请求(无任何的jar包导入)
    二叉树算法的收集
    javascript将list转换成树状结构
    CSS实现鼠标悬浮无限向下级展示的简单代码
    Jquery的框架解析
    mybaits插入时的一些总结
    苹果手机在有滚动条的情况下,滑动不顺畅的原因
    tomcat下jndi的三种配置方式
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3162728.html
Copyright © 2020-2023  润新知