hdu 2609 How many (minimum notation)

The meaning of problems

For \ (n-\) cycles strings, essentially required number of different strings

Portal

Thinking

Seeking subscript represents the smallest, from the smallest index of the string as a starting point, a new weight to the string into the map, the final number of elements in the map is the final answer.

Min / notation

Code

#include <cstdio>
#include <cstring>
#include <map>
#include <string>

using namespace std;

const int maxn = 1e6+10;

int n, l;
char str[maxn<<1];

int solve(int op) {  // 0: 最小表示法, 1:最大表示法
    strncpy(str+l, str, l);
    int i = 0, j = 1;
    while(i < l && j < l) {
        int k = 0;
        while(k < l && str[i+k] == str[j+k]) ++k;
        if(k >= l) break;
        if((str[i+k] > str[j+k]) ^ op) i += k+1;
        else j += k+1;
        if(i == j) ++j;
    }
    return i < j ? i : j;
}

int main() {
    while(scanf("%d", &n)==1) {
        map<string, bool> mp;
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            scanf("%s", str);
            l = strlen(str);
            int mi = solve(0);
            string tmp = "";
            for (int i = 0; i < l; ++i)
                tmp += str[(mi+i)%l];
            if(!mp.count(tmp)) {
                ++ans;
                mp[tmp] = 1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/acerkoo/p/11516869.html