Shape Number
Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 777 Accepted Submission(s): 372
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
Recommend
lcy
本题题目难以读懂,但解题思想还是比较简单的。
首先得根据题意转化所给的字符串,后一个的减前一个,注意最后一个应减开始时的第一个。
然后是求字符串的最小表示,有现成模板。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=300000+100;
//返回母串的最小子串的起始位置,从1开始
int minpresent(char *str)
{
int i,j,k,len=strlen(str);
i=0,j=1,k=0;
while(i<len&&j<len&&k<len)
{
if(str[(i+k)%len]==str[(j+k)%len])
k++;
else
{
if(str[(i+k)%len]>str[(j+k)%len])
i=i+k+1;
else
j=j+k+1;
if(i==j)j++;
k=0;
}
}
return ++i<++j?i:j;
}
char str[MAXN];
int main()
{
int i,j,len,tmp;
char ch;
while(~scanf("%s",str))
{
len=strlen(str);
ch=str[0];
for(i=0;i<len;i++)
{
if(i<len-1)tmp=str[i+1]-str[i];
else {
tmp=ch-str[i];
//printf("%c %c\n",str[0],str[i]);
}
str[i]='0'+(tmp<0?(tmp+8):tmp);
}
for(i=minpresent(str)-1,j=0;j<len;j++)
printf("%c",str[(i+j)%len]);
printf("\n");
}
return 0;
}
本文介绍了一种用于标准化轮廓表示的方法——形状编号。该方法通过计算链码的第一差分并找到循环移位后的字典序最小表示来实现旋转不变性和起点无关性。文章还提供了一个示例程序帮助理解。
3009

被折叠的 条评论
为什么被折叠?



