HDU 4162 Shape Number(最小表示法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4162


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


题意:

给定一个只有0-7数字组成的串。现在要由原串构造出一个新串,新串的构造方法:相邻2个位置的数字的差值(后一位减一位),若为负数则要加上8(最后一位由第一位减),问新构造出来的串的一个字典序最小同构串是什么?

题目思路:

把字符串转化出来,最小表示法。

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;

int minRepresent(string s)
{
    int n = s.length();
    int i=0,j=1,k=0;
    while(i<n&&j<n&&k<n)
    {
        int t = s[(i+k)%n] - s[(j+k)%n];
        if(t==0)
            k++;
        else
        {
            if(t>0)
                i += k+1;
            else
                j += k+1;
            if(i==j)
                j++;
            k=0;
        }
    }
    return i<j?i:j;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    string s;
    while(cin>>s)
    {
        int len = s.length();
        string mo="";
        for(int i=0;i<len;i++)
        {
            int t;
            if(i==len-1)
                t = s[0] -  s[i];
            else
                t = s[i+1] - s[i];
            if(t<0)
                mo += '0' + (t+8);
            else
                mo += '0' + t;
        }
        int loc = minRepresent(mo);
        for(int i=loc;i<len;i++)
            cout<<mo[i];
        for(int i=0;i<loc;i++)
            cout<<mo[i];
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值