ZOJ2006 字符串最小表示

http://blog.csdn.net/zy691357966/article/details/39854359

字符串最小表示 相關證明與模板


字符串最小表示入門模板題


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;

int Min(const char *str, int len){
    int i, j, k;
    i = k = 0;
    j = 1;
    while(i < len && j < len){
        k = 0;
        while(k < len && str[i+k] == str[j+k]) k++;
        if (k == len) return i;
        if (str[i+k] > str[j+k])
            i = i+k+1;
        else
            j = j+k+1;
        if (i == j) j++;
    }
    return min(i, j);
}

char str[maxn*2], tmp[maxn];

int main(){
    std::ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--){
        cin >> str;
        int len = strlen(str);
        strncpy(tmp, str, len);
        strcat(str, tmp);
        cout << Min(str, len)+1 << endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章