[NowCoder5666F]Infinite String Comparision

题意

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

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

∣ S ∣ ≤ 1 0 5 |S|\le10^5 S105


题解

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

把字符串当做 26 26 26进制数,将串 s s s化为无限循环“小数”: s 2 6 ∣ s ∣ − 1 \displaystyle\frac{s}{26^{|s|}-1} 26s1s
则有
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} s>t26s1s>26t1ts26t+t>t26s+sst>ts
另外两种情况同理。

单组数据时间复杂度 O ( ∣ S ∣ ) 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,t,若 s ∞ s^{\infty} s t ∞ t^{\infty} t的前 ∣ s ∣ + ∣ t ∣ − gcd ⁡ ( ∣ s ∣ , ∣ t ∣ ) |s|+|t|-\gcd(|s|,|t|) s+tgcd(s,t)位都相同,则 s ∞ = t ∞ s^{\infty}=t^{\infty} s=t

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值