zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Linux 启动过程详解
    ASM实例原始磁盘搜索路径
    RMAN命令总结
    使用DUPLICATE 方式创建ORACLE 11G DG备库环境
    Dataguard中日志传输服务
    Material design之Material Theme
    创建一个Material Design应用过程
    android Material design是什么
    Android 最新L版本,都更新什么东西了
    AndroidHttpClient和HttpEntity详解
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3162728.html
Copyright © 2011-2022 走看看