您的位置:首页 > 其它

hdu 4162 Shape Number【循环字符串最小表示法】模板学习

2016-10-15 17:04 489 查看

Shape Number

Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1416    Accepted Submission(s): 696


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).

Two chain codes may represent the same shape if the shape has been rotated, or if a different starting point is chosen for the contour. To normalize the code for rotation, we can compute the first difference of the chain code instead. The first difference is
obtained by counting the number of direction changes in counterclockwise direction between consecutive elements in the chain code (the last element is consecutive with the first one). In the above code, the first difference is

00110026202011676122

Finally, to normalize for the starting point, we consider all cyclic rotations of the first difference and choose among them the lexicographically smallest such code. The resulting code is called the shape number.

00110026202011676122

01100262020116761220

11002620201167612200

...

20011002620201167612

In this case, 00110026202011676122 is the shape number of the shape above.

 

 

Input

The input consists of a number of cases. The input of each case is given in one line, consisting of a chain code of a shape. The length of the chain code is at most 300,000, and all digits in the code are between 0 and 7 inclusive. The contour may intersect
itself and needs not trace back to the starting point.

 

 

Output

For each case, print the resulting shape number after the normalizations discussed above are performed.

Sample Input

22234446466001207560

12075602223444646600

Sample Output

00110026202011676122

00110026202011676122

Source

The 2011 Rocky Mountain Regional Contest

题目大意:

给你一个原串,让你求出其对应第一个图片中,原串中a【i】想要变成a【i+1】需要在图片中逆时针方向挪动多少次。得到一个新的串b,并且表示串b是一个循环字符串,让你找出最小字典序的输出方式。

思路:

1、O(n)暴力出b串,然后跑一遍循环字符串最小表示法即可,得到位子tmp,然后从tmp位子开始输出即可。

Ac代码:

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