2020牛客暑期多校训练营(第一场)F. Infinite String Comparision

链接

https://ac.nowcoder.com/acm/contest/5666/F

题意

比较字符串 a ∞ a^\infty a b ∞ b^\infty b 的大小

思路

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

把字符串当做 26 26 26 进制数,将串 S S S 化为无限循环“小数”: s 2 6 ∣ s ∣ − 1 \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

另外两种情况同理

官方题解

根据 Periodicity Lemma 可得,若 s ∞ s^{\infty} s t ∞ t^{\infty} t 的前 ∣ s ∣ + ∣ t ∣ − g c d ( ∣ s ∣ , ∣ t ∣ ) |s|+|t|-gcd(|s|,|t|) s+tgcd(s,t) 位都相同,则 s ∞ = t ∞ s^{\infty}=t^{\infty} s=t

代码

#include<bits/stdc++.h>
using namespace std;
string a,b;
int main() {
    while(cin>>a>>b) {
        string c=a+b,d=b+a;
        if(c>d) puts(">");
        else if(c<d) puts("<");
        else puts("=");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值