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;
    }
  • 相关阅读:
    ~随笔A007~html中input输入框的字数限制、同步input的输入内容至div中
    ~随笔A006~微信扫码的授权、用户绑定、关注公众号、消息反馈
    【CV】实验二:特征检测与匹配
    【笔记】DLX算法及常见应用
    【笔记】和算法无关的那些东东
    【笔记】康拓展开&逆康拓展开
    【笔记】离散对数
    【笔记】Shift-And算法&Shift-OR算法
    【模板】中缀表达式求值
    【笔记】数据库系统
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3162728.html
Copyright © 2011-2022 走看看