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): 2184    Accepted Submission(s): 904


    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
     
    用最小表示法返回字符串能旋转的最小的字典序。set记录一下数一数就好了。最大表示法和最小表示法基本上一样,就是判断的语句换了一下。
     
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <set>
    using namespace std;
    const int maxn = 1e4 + 10;
    char s[maxn][110];
    int len;
    set<string> st;
    
    int GetMin(int id) {
        int i = 0, j = 1, k = 0;
        while(i < len && j < len && k < len) {
            int t = s[id][(i + k) % len] - s[id][(j + k) % len];
            if(!t) k++;
            else {
                if(t < 0) j = j + k + 1;
                else i = i + k + 1;
                if(i == j) j++;
                k = 0;
            }
        }
        return min(i, j);
    }
    
    /*int getMax() {
        int i = 0, j = 1, k = 0;
        while(i < len && j < len && k < len) {
            int t = s[(i + k) % len] - s[(j + k) % len];
            if(!t) k++;
            else {
                if(t > 0) j = j + k + 1;
                else i = i + k + 1;
                if(i == j) j++;
                k = 0;
            }
        }
        return min(i, j);
    }*/
    
    int main() {
        int n;
        while(~scanf("%d", &n)) {
            st.clear();
            for(int i = 1; i <= n; i++) {
                scanf("%s", s[i]);
            }
            for(int i = 1; i <= n; i++) {
                string tmp;
                len = strlen(s[i]);
                int Min = GetMin(i);
                for(int j = 0; j < len; j++) {
                    tmp += s[i][(Min + j) % len];
                }
                //if(!st[tmp])
                    st.insert(tmp);
            }
            cout << st.size() << endl;
        }
    }
  • 相关阅读:
    Eclipse 控制台视图和服务器视图中停止Web服务器的差别
    JSP中forEach和forTokens循环的用法
    Java中的消息框
    JS弹出div简单样式
    Java中简单提示异常代码的行号,类名等
    Java简单的数据库连接
    Java简单方法批量修改Windows文件夹下的文件名(简单IO使用)
    Java中对文件的序列化和反序列化
    navicat 连接 mysql 出现Client does not support authentication protocol requested by server
    IoC是什么
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5648093.html
Copyright © 2011-2022 走看看