zoukankan      html  css  js  c++  java
  • hdu2609 最小表示法

    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
    题意:给一些长度相同的01数列,要求求出不相同的个数(经过循环相同的也算相同)
    题解:最小表示法(为啥分类到kmp里面?)直接水过了,还以为要kmp之类的呢
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    
    using namespace std;
    
    const int N=1000000+5,maxn=1000000+5,inf=1e9+5;
    
    int Next[N],slen;
    string str;
    
    void getnext()
    {
        Next[0]=-1;
        int k=-1;
        for(int i=1;i<slen;i++)
        {
            while(k>-1&&str[k+1]!=str[i])k=Next[k];
            if(str[k+1]==str[i])k++;
            Next[i]=k;
        }
    }
    int getmin()
    {
        int i=0,j=1,k=0;
        while(i<slen&&j<slen&&k<slen){
            int t=str[(i+k)%slen]-str[(j+k)%slen];
            if(!t)k++;
            else
            {
                t>0 ? i=i+k+1 : j=j+k+1;
                if(i==j)j++;
                k=0;
            }
        }
        return min(i,j);
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n;
        while(cin>>n){
            vector<string>v;
            for(int i=0;i<n;i++)
            {
                cin>>str;
                slen=str.size();
                str=str.substr(getmin(),slen)+str.substr(0,getmin());
                bool flag=1;
                for(int j=0;j<v.size();j++)
                    if(v[j]==str)
                    {
                        flag=0;
                        break;
                    }
                if(flag)v.push_back(str);
            }
            cout<<v.size()<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    5月14日 游戏闯关,
    无名管道练习小程序
    关于对进程、线程的返回状态的获取的理解
    C语言中内存分布及程序运行中(BSS段、数据段、代码段、堆栈)
    linux 与会话相关的一些概念、登录过程
    linux进程——fork、vfork 两函数的实现及两者区别
    关于 linux 中init 进程
    linux进程——fork()函数
    linux下 vim多屏幕操作
    linux下进程管理
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6826799.html
Copyright © 2011-2022 走看看