B - Ugly Number Gym - 101875B(最小表示法/最大表示法)

B - Ugly Number Gym - 101875B
A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k = 1 and 231 for k = 2.

Teo, a kid from Canada, learned from his Canadian friends this definition of ugly numbers: a number is ugly if, and only if every one of its k-cyclic shifts returns a number greater than or equal to it.

Given an n-digit decimal number x, can you answer if it is ugly or not?

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 5 × 105), indicating the number of digits of the number.

The next line contains an n-digit decimal number, indicating the value of x.

Output
Output “Yes” if x is an ugly number and “No” otherwise.

Examples
Input
3
123
Output
Yes
Input
4
2214
Output
No

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<string>
using namespace std;
int getmin(char *a,int n)
{
    int i=0,j=1,k=0;
    int t;
    //从i开始k长度和从j开始k长度的字符串作比较
    while(i<n&&j<n&&k<n)
    {
        t=a[(i+k)%n]-a[(j+k)%n];
        //t用来计算相应位置上那个字符字典序哪个大
        if(!t) //相等就下一个
        {
            k++;
        }
        else
        {
            if(t>0){i+=k+1;}
            //i位置大,最大表示法j+=k+1
            else{j+=k+1;}
            //j位置大,最大表示法i+=k+1
            if(i==j){j++;}
            k=0;
        }
    }
   if(i<j){return i;}
   else {return j;}
}
int main()
{
   int n;
   char a[500005];
   scanf("%d",&n);
   scanf("%s",a);
   int k=getmin(a,n);
   if(k==0)
   {
       printf("Yes\n");
   }
   else
   {
       printf("No\n");
   }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值