# [HDU 4162] Shape Number【最小表示法】

• 2018-01-06
• 0
• 0

## Problem:

Time Limit: 24000/12000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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

Recommend

lcy   |   We have carefully selected several similar problems for you:  4161 4163 4164 4165 4168

## Solution:

## 今天 POJ 被一个程序 (Run ID = 18020626) 卡着编译了 4 个多钟头！所以找了道 HDU 的 ##

• dif[i] = (chain[i + 1] - chain[i]) % 8

【字符串的最小表示法】

1. 若 s[i + k] == s[j + k]，则将 k++。
2. 若 s[i + k] > s[j + k]，则将 i += k + 1，并将 k 重置为 0。
3. 若 s[i + k] < s[j + k]，则将 j += k + 1，并将 k 重置为 0。

【最小表示法线性算法的正确性证明】

【华丽丽的 Q.E.D.】

## Code: O(Tn) [2084K, 577MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

char cod[300005], dif[300005];
int len;

#define cyc(x) ((x) < len ? (x) : (x) - len)
inline int minRepresentation(char *str, int len){
int i = 0, j = 1, k = 0, diff;
while(i < len && j < len && k < len){
diff = str[cyc(i + k)] - str[cyc(j + k)];
if(!diff) k++;
else{
if(diff > 0) i += k + 1;
else j += k + 1;
if(i == j) j++;
k = 0;
}
}
return min(i, j);
}

int main(){
while(scanf("%s", cod) != EOF){
len = strlen(cod), cod[len] = cod[0];  // No need to worry '\0' covered
for(register int i = 0; i < len; i++){
dif[i] = cod[i + 1] - cod[i] + '0';
if(dif[i] < '0') dif[i] += 8;
}
int startid = minRepresentation(dif, len);
for(register int i = startid; i < len; i++) putchar(dif[i]);
for(register int i = 0; i < startid; i++) putchar(dif[i]);
puts("");
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai