HDU4162(最小循环表示)
Shape Number
Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1503 Accepted Submission(s): 743
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.
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
链码逐项求差得到形状码
求形状码的最小循环表示
1 //2017-08-31 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int N = 300010; 10 char str[N<<1], str2[N]; 11 12 //最小循环表示 13 //input: str[2*len] 原串扩展了一倍的串。如原串为“abc”, str为“abcabc”。 14 // len 原串的长度。 15 //output:ptr 最小循环表示法的起始下标。 16 int min_representation(int len){ 17 int i = 0, j = 1, k = 0; 18 while(i < len && j < len && k < len){ 19 if(str[i+k] == str[j+k])k++; 20 else{ 21 if(str[i+k] < str[j+k]) j += k+1; 22 else i += k+1; 23 k = 0; 24 if(i == j)j++; 25 } 26 } 27 return min(i, j); 28 } 29 30 int main() 31 { 32 while(scanf("%s", str2) != EOF){ 33 int len = strlen(str2); 34 for(int i = 0; i < len-1; i++){ 35 str[i] = (str2[i+1] - str2[i] + 8) % 8 + '0'; 36 str[i+len] = str[i]; 37 } 38 str[len-1] = (str2[0] - str2[len-1] + 8) % 8 + '0'; 39 str[len-1+len] = str[len-1]; 40 int ptr = min_representation(len); 41 str[ptr+len] = '\0'; 42 printf("%s\n", str+ptr); 43 } 44 45 return 0; 46 }