【bzoj1398】Vijos1382寻找主人 Necklace

*题目描述:
1398
给定两个项链的表示,判断他们是否可能是一条项链。
*输入:
输入文件只有两行,每行一个由0至9组成的字符串,描述一个项链的表示(保证项链的长度是相等的)。
*输出:
如果两条项链不可能同构,那么输出’No’,否则的话,第一行输出一个’Yes’,第二行输出该项链的字典序最小的表示。 设L = 项链长度, 对于50%的数据L <= 100000; 对于100%的数据L <= 1000000。
*样例输入:
*样例输出:
*提示:
*来源:
*题解:
最小表示法果题。
*代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#ifdef WIN32
    #define LL "%I64d"
#else
    #define LL "%lld"
#endif

#ifdef CT
    #define debug(...) printf(__VA_ARGS__)
    #define setfile() 
#else
    #define debug(...)
    #define filename ""
    #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
    R char ch; R int cnt = 0; R bool minus = 0;
    while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    ch == '-' ? minus = 1 : cnt = ch - '0';
    while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    return minus ? -cnt : cnt;
}
#define maxn 1000010
char str1[maxn], str2[maxn];
inline int smallest(char a[])
{
    R int i = 0, j = 1, k = 0, n = strlen(a);
    while (i < n && j < n && k < n)
    {
        R int tmp = a[(i + k) % n] - a[(j + k) % n];
        if (!tmp) ++k;
        else
        {
            if (tmp > 0) i += k + 1;
            else j += k + 1;
            if (i == j) ++j;
            k = 0;
        }
    }
    return dmin(i, j);
}
int main()
{
//  setfile();
    gets(str1);
    gets(str2);
    R int s1 = smallest(str1), s2 = smallest(str2), l1 = strlen(str1);
    for (R int i = 0; i < l1; ++i) str1[i + l1] = str1[i], str2[i + l1] = str2[i];
    R bool flag = 1;
    for (R int i = s1, j = s2; i <= s1 + l1 && flag; ++i, ++j)
        flag = str1[i] == str2[j];
    if (flag)
    {
        puts("Yes");
        for (R int i = s1; i < s1 + l1; ++i) printf("%c", str1[i]);
    }
    else
        puts("No");
    return 0;
}
原文地址:https://www.cnblogs.com/cocottt/p/6764995.html