链接:https://ac.nowcoder.com/acm/contest/5666/F
来源:牛客网
题目描述
For a string x x x, Bobo defines x ∞ = x x x … x^\infty = x x x \dots x∞=xxx…, which is x x x repeats for infinite times, resulting in a string of infinite length.
Bobo has two strings aa and bb. Find out the result comparing a ∞ a^\infty a∞ and b ∞ b^\infty b∞ in lexicographical order.
You can refer the wiki page for further information of Lexicographical Order.
输入描述:
The input consists of several test cases terminated by end-of-file.
The first line of each test case contains a string a a a, and the second line contains a a a string b b b.
1 ≤ ∣ a ∣ , ∣ b ∣ ≤ 1 0 5 1 \leq |a|, |b| \leq 10^5 1≤∣a∣,∣b∣≤105
a a a, b b b consists of lower case letters.
The total length of input strings does not exceed 2 × 1 0 6 2 \times 10^6 2×106.
输出描述:
For each test case, print “=
” (without quotes) if a ∞ = b ∞ a^\infty = b^\infty a∞=b∞. Otherwise, print “<
” if a ∞ < b ∞ a^\infty < b^\infty a∞<b∞, or “>
” if a ∞ > b ∞ a^\infty > b^\infty a∞>b∞.
示例1
输入
aa
b
zzz
zz
aba
abaa
输出
<
=
>
设 s 1 = a + b s_1=a+b s1=a+b, s 2 = b + a s_2=b+a s2=b+a,则
s 1 ∞ = a b a b a b … s_1^\infty=ababab\dots s1∞=ababab…
s 2 ∞ = b a b a b a … s_2^\infty=bababa\dots s2∞=bababa…
显然 s 1 ∞ s_1^\infty s1∞和 s 2 ∞ s_2^\infty s2∞都包含子序列 a ∞ a^\infty a∞和 b ∞ b^\infty b∞,且 s 1 s_1 s1, s 2 s_2 s2的字典序大小关系与 s 1 ∞ s_1^\infty s1∞, s 2 ∞ s_2^\infty s2∞的字典序大小关系相同。
令 s 1 ∞ s_1^\infty s1∞, s 2 ∞ s_2^\infty s2∞长度相等,则可以把序列看做是数字,字典序大小看做是数字大小的比较。
比较大小应当是各位的数乘上对应位的权值,不妨设 s 1 ∞ s_1^\infty s1∞中 a ∞ a^\infty a∞对应的权值总和为 s u m 1 sum_1 sum1; b ∞ b^\infty b∞对应的权值总和为 s u m 2 sum_2 sum2,则 s 1 ∞ s_1^\infty s1∞中 a ∞ a^\infty a∞对应的权值总和为 s u m 2 sum_2 sum2; b ∞ b^\infty b∞对应的权值总和为 s u m 1 sum_1 sum1。
s 1 ∞ − s 2 ∞ = ( s u m 1 ⋅ a ∞ + s u m 2 ⋅ b ∞ ) − ( s u m 2 ⋅ a ∞ + s u m 1 ⋅ b ∞ ) = ( s u m 1 − s u m 2 ) ⋅ ( a ∞ − b ∞ ) \begin{aligned} s_1^\infty-s_2^\infty &=(sum_1·a^\infty+sum_2·b^\infty)-(sum_2·a^\infty+sum_1·b^\infty)\\ &=(sum_1-sum_2)·(a^\infty-b^\infty) \end{aligned} s1∞−s2∞=(sum1⋅a∞+sum2⋅b∞)−(sum2⋅a∞+sum1⋅b∞)=(sum1−sum2)⋅(a∞−b∞)
显然 s u m 1 − s u m 2 > 0 sum_1-sum_2>0 sum1−sum2>0
因此 a ∞ a^\infty a∞, b ∞ b^\infty b∞的字典序大小关系与 s 1 ∞ s_1^\infty s1∞, s 2 ∞ s_2^\infty s2∞的字典序大小关系相同。
即 a ∞ a^\infty a∞, b ∞ b^\infty b∞的字典序大小关系与 s 1 s_1 s1, s 2 s_2 s2的字典序大小关系相同。
因此直接比较 s 1 s_1 s1, s 2 s_2 s2的字典序大小关系即可。复杂度为 O ( n ) O(n) O(n)。
#include
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector
#define pii pair
#define mii unordered_map
#define msi unordered_map
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
string a, b;
string s1, s2;
int main() {
while (cin >> a, cin >> b) {
s1 = a + b, s2 = b + a;
if (s1 > s2)puts(">");
else if (s1 < s2)puts("<");
else puts("=");
}
return 0;
}