zoukankan      html  css  js  c++  java
  • (字典树)How many--hdu--2609

    http://acm.hdu.edu.cn/showproblem.php?pid=2609

    How many

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


    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
     
     
    用到了上一题的查找最小字典串的方法
    一看题没思路了,看看学长是用字典树写的,还从没有用字典树写过题,勉强写了,不过这次可以好好体会体会字典树,但既然是在KMP专题里出现的,想必应该也可以用KMP来写吧,再搜搜

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    const int maxn = 205;
    
    char s1[maxn], s[maxn], a[maxn];
    
    struct node
    {
        struct node *next[2];
    };
    
    int GetMin(char S[], int N)
    {
        int i=0, j=1;
    
        while(i<N && j<N)
        {
            int k = 0;
    
            while(S[i+k]==S[j+k] && k<N)
                k++;
    
            if(k==N)
                break;
    
            if(S[i+k]<S[j+k])
            {
                if(j+k>i)
                    j += k+1;
                else
                    j = i+1;
            }
            else
            {
                if(i+k>j)
                    i += k+1;
                else
                    i = j+1;
            }
        }
    
        return min(i, j);
    }
    
    int FindTree(node *head, char s[])
    {
         node *p=head;
         int i, flag = 0;
    
         for(i=0; s[i]; i++)
         {
            int k = s[i]-'0';
    
            if(p->next[k]==NULL)
            {
                flag = 1;
                p->next[k] = new node();
            }
    
            p = p->next[k];
         }
    
         return flag;
    }
    
    void Free(node *head)
    {
        node *p = head;
        for(int i=0; i<2; i++)
            if(p->next[i]!=NULL)
            Free(p->next[i]);
        free(p);
    }
    
    
    int main()
    {
        int n;
    
        while(scanf("%d", &n)!=EOF)
        {
            int sum = 0;
            node *head = new node();
    
            while(n--)
            {
                scanf("%s", s1);
                int len = strlen(s1);
    
                strcpy(a, s1);
                strcat(a, s1);
    
                int index = GetMin(a, len);
                strncpy(s, a+index, len);
    
                int ans = FindTree(head, s);
    
                if(ans)  sum ++;
            }
    
            printf("%d
    ", sum);
    
            Free(head);
        }
        return 0;
    }
    View Code
    勿忘初心
  • 相关阅读:
    maven打包不执行测试用例
    maven不打包子模块资源文件
    mvn打包时添加version和profile
    eclipse控制台中文乱码解决方法
    eclipse常用插件
    Spring Boot系列之-logging
    Spring Boot系列之-profile
    Spring Boot系列之-helloword入门
    sqlite入门
    解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element
  • 原文地址:https://www.cnblogs.com/YY56/p/4846735.html
Copyright © 2011-2022 走看看