HDU NO.4162 Shape Number(最小表示法循环输出字符串)
来源:互联网 发布:远距离网络对讲系统 编辑:程序博客网 时间:2024/05/18 03:54
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
2223444646600120756012075602223444646600
Sample Output
0011002620201167612200110026202011676122
题意&思路:
给出一个字符串(设想连成一个环)先按照相邻两个元素相减构成一个新的字符串,
然后利用最小表示法函数,找到循环输出的位置继而循环输出即可。
代码:
#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<list>#include<iostream>#include<map>#include<queue>#include<set>#include<stack>#include<vector>#define a first#define b secondusing namespace std;const int MAX = 300000 + 5;char s[MAX];int func(char *s, int l){ int i,j,k; i=0; j=1; k=0; while(i<l&&j<l){ k=0; while(s[i+k]==s[j+k]&&k<l) k++; if(k==l) return i; if(s[i+k]>s[j+k]) if(i+k+1>j) i=i+k+1; else i=j+1; else if(j+k+1>i) j=j+k+1; else j=i+1; } if(i<l) return i; else return j;}int main(){ while(~scanf("%s", s)){ int len = strlen(s); char temp = s[0]; char s1[MAX]; for(int i = 0; i < len - 1; i++){ s1[i] = ((s[i+1] - s[i] + 8) % 8) + '0'; } s1[len-1] = ((temp - s[len-1] + 8) % 8) + '0'; s1[len] = '\0'; int p = func(s1, len); for(int i = 0, j = p; i < len; i++, j = (j + 1) % len){ printf("%c", s1[j]); } printf("\n"); } return 0;}
0 0
- HDU NO.4162 Shape Number(最小表示法循环输出字符串)
- HDU 4162 Shape Number(字符串,最小表示法)
- hdu 4162 Shape Number【循环字符串最小表示法】模板学习
- 【最小表示法】HDU-4162-Shape Number
- HDU 4162 Shape Number 最小表示法
- HDU 4162 Shape Number(最小表示法)
- hdu 4162 Shape Number(最小表示法)
- HDU——4162Shape Number(字符串的最小表示)
- TOJ 3771 HDU 4162 Shape Number / 最小表示法
- 【HDU 4162】Shape Number(一阶差分链码+最小表示法)
- HDU4162 Shape Number(最小表示法)
- HDOJ 4162 Shape Number(最小表示法 )
- HDU4162 Shape Number 最小表示法
- hdu 2609(字符串最小表示法)
- 字符串循环同构的最小表示法(转)
- 【字符串循环同构的最小表示法】
- 循环字符串的最小表示法
- 最小表示法 字符串循环同构问题
- SSIS 学习之旅 FTP文件传输-脚本任务
- 如何下载YouTube网站的视频
- 69道Spring面试题和答案
- MySQL存储过程+游标+触发器
- android 性能优化
- HDU NO.4162 Shape Number(最小表示法循环输出字符串)
- 2016 UESTC Training for Dynamic Programming B - 柱爷与最大区间和 最大区间和推广、前缀和、枚举间隔点
- gh0st的IOCP模型分析
- 1002 Problem B
- MySQL服务启动提示window无法启动MySQL服务 错误1067:进程意外终止
- No3.组合查询--机房收费
- JDBC 与 MyBatis
- BestCoder Round #83(HDU 5680)
- Ubuntu13.10系统 apt-get update失败解决办法