[NowCoder5666F]Infinite String Comparision

题意

对于字符串 s s ,定义 s = s s s . . . s^{\infty}=sss...

给定字符串 s , t s,t ,判断 s s^{\infty} t t^{\infty} 的字典序大小关系。

S 1 0 5 |S|\le10^5


题解

类比 10 10 进制中无限循环小数,如 0.233233... = 233 999 = 233 1 0 3 1 \displaystyle0.233233...=\frac{233}{999}=\frac{233}{10^3-1}

把字符串当做 26 26 进制数,将串 s s 化为无限循环“小数”: s 2 6 s 1 \displaystyle\frac{s}{26^{|s|}-1}
则有
s > t s 2 6 s 1 > t 2 6 t 1 s 2 6 t + t > t 2 6 s + s s t > t s s^{\infty}>t^{\infty}\Leftrightarrow\frac{s}{26^{|s|}-1}>\frac{t}{26^{|t|}-1}\Leftrightarrow s\cdot26^{|t|}+t>t\cdot26^{|s|}+s\Leftrightarrow\overline{st}>\overline{ts}
另外两种情况同理。

单组数据时间复杂度 O ( S ) O(|S|)

#include <bits/stdc++.h>
using namespace std;
string s, t;
int main() {
    while (cin >> s >> t)
        if (s + t < t + s)
            puts("<");
        else if (s + t > t + s)
            puts(">");
        else
            puts("=");
    return 0;
}

拓展:Periodicity Lemma:两个串 s , t s,t ,若 s s^{\infty} t t^{\infty} 的前 s + t gcd ( s , t ) |s|+|t|-\gcd(|s|,|t|) 位都相同,则 s = t s^{\infty}=t^{\infty}

猜你喜欢

转载自blog.csdn.net/BeNoble_/article/details/107308782