随手练——HUD 2609 How many

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609

题目没看懂,就想百度找下,结果 多数人就写个 最小表示法,就po代码了,看了这个博主才明白题目是啥意思:https://blog.csdn.net/piaocoder/article/details/48447193 ,每个01序列都作为一串 “项链”,能够通过循环转换而成的算同一个,比如:0110和0011算同一个,就要把所有序列转换为相同的标准:循环的同构字符串S’中字典序最小的一个。

其实只要能把 循环的同构字符串 都表示一样就可以,不一定是最小字典序。

#include <iostream>
#include <string>
#include <set>
using namespace std;

int findMin(string s) {
    int i = 0, j = 1,k = 0;
    while (i < s.length() && j < s.length() && k < s.length()) {
        int t = s[(i + k) % s.length()] - s[(j + k) % s.length()];
        if (t == 0) k++;
        else {
            if (t > 0) 
                i += k + 1;
            else 
                j += k + 1;
            //碰到i,j重合时,不处理后面两个指针就一直在一起了
            if (i == j)
                j++;
            k = 0;
        }
    }
    //谁在前面,谁就是小的那个
    return i > j ? j : i;
}

int main() {
    set<string>set;
    string s;
    int n;
    while (cin >> n) {
        cin.ignore();
        while (n--) {
            getline(cin, s);
            if (s.empty()) continue;
            int min = findMin(s);
            string s1;
            s1 = s.substr(min);
            s1.append(s.substr(0, min));
            set.insert(s1);
        }
        cout << set.size() << endl;
        set.clear();
    }
}

猜你喜欢

转载自www.cnblogs.com/czc1999/p/10356165.html