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;
    }
  • 相关阅读:
    sshkey批量分发,管理方案
    sersync 配合rsync实时同步备份
    全网实时热备inotify+rsync
    rsync定时同步配置
    全网NFS存储架构详解与分析
    linux新装系统初始化
    Cannot create a secure XMLInputFactory --CXF调用出错
    POJ 2187 /// 凸包入门 旋转卡壳
    POJ 1127 /// 判断线段与线段是否相交
    POJ 2398 map /// 判断点与直线的位置关系
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3162728.html
Copyright © 2011-2022 走看看