Uva1584 字符串

逐个比较每一个字符位置开始的字符串。不断更新目前为止,字典序最小串在输入串中的起始位置,最终找到字典序最小串

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<memory>
#include<stdlib.h>
using namespace std;
char s[104];
int len;
int less1(char *s, int p, int q)
{
    for (int i = 0; i < len; i++)
    {
        if (s[(p + i) % len] != s[(q + i) % len])
            return s[(p + i) % len] < s[(q + i) % len];
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int ans = 0;
        scanf("%s", s);
        len = strlen(s);
        for (int i = 0; i < len; i++)
        {
            if (less1(s, i, ans))
            {
                ans = i;
            }
        }
        for (int i = 0; i < len; i++)
        {
            putchar(s[(ans + i) % len]);
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2016-10-02 14:37  web之路  阅读(139)  评论(0)    收藏  举报