UVA 1584 Circular Sequence

标签:字符串
题目在这

本来我是想着遍历所有可能的情况,但是操作起来太麻烦,果断放弃了,之后就学习作者的做法,觉得真是跪服作者。
然后,就写一下自己的理解吧。
首先,根据圆的特性,是循环的,所以找出起点就好。
第二,找起点的方法:我们是要寻找最小的字典序,要好好分析Less函数,当从i开始的字典序小于从ans开始的字典序,才进行ans更新。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <vector>
#include <queue>
#include <ctime>
#define ll long long
#define inf 0x3f3f3f3f
//#define local
using namespace std;
const int N = 110;
int t;
char s[N]={'\0'};
bool Less(const char *s,int a,int b)
{
    int n=strlen(s);
    for(int i=0;i<n;i++)
    {
        if(s[(a+i)%n]!=s[(b+i)%n])
            return s[(a+i)%n]<s[(b+i)%n];
    }
    return false;
}
int main()
{
#ifdef local
    freopen("input.txt","r",stdin);
#endif // local
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        int len=strlen(s);
        int ans=0;
        for(int i=1;i<len;i++)
        {
            if(Less(s,i,ans))
                ans=i;
        }
        for(int i=0;i<len;i++)
        {
            printf("%c",s[(ans+i)%len]);
        }
        printf("\n");

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值