您的位置:首页 > 其它

HDU 4162 Shape Number【字符串最小表示】

2012-03-14 18:27 344 查看
Problem Description

In computer vision, a chain code is a sequence of numbers representing directions when following the contour of an object. For example, the following figure shows the contour represented by the chain code 22234446466001207560 (starting at the upper-left corner).
View Code

#include<stdio.h>
#include<string.h>
#define min(a,b)((a)<(b))?(a):(b)
int Min(char s[],int len)
{
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len)
{
if(s[(i+k)%len]==s[(j+k)%len])
k++;
else
{
if(s[(i+k)%len]>s[(j+k)%len])
i+=k+1;
else
j+=k+1;
if(i==j)
j++;
k=0;
}
}
return min(i,j);
}
char s[300001];
int main()
{
int i,len,k;
char tmp;
while(scanf("%s",s)!=EOF)
{
len=strlen(s);
tmp=s[0];
for(i=0;i<len-1;i++)
s[i]=(s[i+1]+8-s[i])%8+'0';
s[i]=(tmp+8-s[i])%8+'0';
k=Min(s,len);
for(i=k;i<len;i++)
printf("%c",s[i]);
for(i=0;i<k;i++)
printf("%c",s[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: