zoukankan      html  css  js  c++  java
  • P

    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. 

    InputThe 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'). 
    OutputFor 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
    #include<iostream>
    #include<set>
    #include<map>
    #include<vector>
    #include<string>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define MAXN 10002
    /*
    题目相当于求所有字符串中
    能通过相互循环位移得到的字符串数目
    对每个字符串求最小表示法,然后加入到set中
    */
    string str;
    set<string> S;
    int GetMin(string s,int len)
    {
        int i=0,j=1,k=0;
        while(i<len&&j<len&&k<len)
        {
            if(s[(i+k)%len]==s[(j+k)%len])
                k++;
            else if(s[(i+k)%len]>s[(j+k)%len])
            {
                i = i+k+1;
                k = 0;
            }
            else 
            {
                j = j+k+1;
                k = 0;
            }
            if(i==j)
                j++;
        }
        return min(i,j);
    }
    int main()
    {
        int n;
        string tmp;
        tmp.reserve(101);
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                tmp.clear();
                cin>>str;
                int pos = GetMin(str,str.size()),L = str.size();
                for(int j=pos,cnt=0;cnt<L;j=(j+1)%L,cnt++)
                {
                    tmp.push_back(str[j]);
                }
                S.insert(tmp);
            }
            cout<<S.size()<<endl;
            S.clear();
        }
    }
  • 相关阅读:
    简单工厂模式、工厂模式、抽象工厂模式
    直接插入排序
    简单选择排序的陷阱
    面试3 题目二,不修改数组找到重复的数字
    二进制中1的个数(读不懂题目怎么办)
    用两个栈实现队列
    斐波那契数列
    替换空格
    python 实现杨辉三角(依旧遗留问题)
    递归实现二分查找
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6686888.html
Copyright © 2011-2022 走看看