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 <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,f=1,k;
char s[1000100];
scanf("%d",&n);
scanf("%s",s);
i=0;
while(i<n&&(s[i+1]-'0')>=(s[i]-'0'))
{
i++;
}
if(i>=n)
{
printf("Yes\n");
}
else
{i++;
while(i<n&&f==1)
{
j=0;
while(s[i]==s[j])
{
if(i==n-1)
break;
i++;
j++;
}
if(i==n-1&&s[i]==s[j])
{
k=j+1;
while(s[k]==s[i]&&k<i){
k++;
}
if(s[i]<s[k])
{
f=0;
break;
}
}
if(s[i]<s[j]&&i<n)
{
f=0;
break;
}
i++;
}
if(f==0){
printf("No\n");
}
else
printf("Yes\n");
}
return 0;
}