zoukankan      html  css  js  c++  java
  • HDU2609 How many —— 最小表示法

    题目链接:https://vjudge.net/problem/HDU-2609

    How many

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


    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
     
    Author
    yifenfei
     
    Source
     
    Recommend
    yifenfei
     
     
     
    题解:
    1.求出每个字符串的最小表示法。
    2.对所有字符串的最小表示进行排序,然后统计。
     
     
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 typedef long long LL;
    14 const double eps = 1e-6;
    15 const int INF = 2e9;
    16 const LL LNF = 9e18;
    17 const int MAXN = 1e4+10;
    18 
    19 char s[MAXN][110], tmp[110];
    20 
    21 int cmp(const void *a,const void *b)
    22 {
    23      return (strcmp((char*)a,(char*)b));
    24 }
    25 
    26 int getmin(char *s, int len) //返回最小表示法的始端
    27 {
    28     int i = 0, j = 1, k = 0;
    29     while(i<len && j<len && k<len)
    30     {
    31         int t = s[(i+k)%len]-s[(j+k)%len];
    32         if (!t) k++;
    33         else
    34         {
    35             if (t>0) i += k+1;
    36             else j += k+1;
    37             if (i==j) j++;
    38             k = 0;
    39         }
    40     }
    41     return i<j?i:j;
    42 }
    43 
    44 int main()
    45 {
    46     int n;
    47     while(scanf("%d", &n)!=EOF)
    48     {
    49         for(int i = 1; i<=n; i++)
    50         {
    51             scanf("%s", tmp);
    52             int len = strlen(tmp);
    53             int k = getmin(tmp, len);
    54             for(int j = 0; j<len; j++)
    55                 s[i][j] = tmp[(k+j)%len];
    56             s[i][len] = 0; //!!
    57         }
    58         qsort(s+1, n, sizeof(s[1]), cmp);
    59 
    60         int ans = 0;
    61         for(int i = 1; i<=n; i++)
    62             if(i==1 || strcmp(s[i], s[i-1]))
    63                 ans++;
    64 
    65         printf("%d
    ", ans);
    66     }
    67 }
    View Code
  • 相关阅读:
    Vijos Oct.28 NOIP2012模拟赛
    QBXT day3 圆盘自动机 游戏 方块
    QBXT day2 最近点对 最长路径 山峰
    1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
    1617: [Usaco2008 Mar]River Crossing渡河问题
    委托的Lambda表达式
    值转换器IValueConverter
    Silverlight中遇到的一些问题
    匿名方法
    Silverlight中的Binding
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7894889.html
Copyright © 2011-2022 走看看