链接:https://ac.nowcoder.com/acm/contest/5666/F
来源:牛客网
For a string xxx, Bobo defines x∞=xxx…x, which is xxx repeats for infinite times, resulting in a string of infinite length.
Bobo has two strings a and b. Find out the result comparing a∞ and 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, and the second line contains a string b.
1≤∣a∣,∣b∣≤105
a, b consists of lower case letters.
The total length of input strings does not exceed 2×106
输出描述:
For each test case, print “=
” (without quotes) if a∞=b∞ . Otherwise, print “<
” if a∞<b∞, or “>
” if a∞>b∞
示例1
输入
aa
b
zzz
zz
aba
abaa
输出
<
=
>
题目大意:有a,b字符串比较,比较a∞,b∞字符串大小
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
int main()
{
int i,j,c;
string a,b;
while(cin>>a>>b)
{
int y=2*max(a.size(),b.size()); //!!!
int flag = 1;
for(i=0,j=0,c=0; ;i++,j++,c++){
if(i>=a.size()){
i = 0;
}
if(j>=b.size())
j = 0;
if(a[i]>b[j]){
flag = 2;
break;
}
else if(a[i]<b[j]){
flag = 0;
break;
}
if(c>=y)break;
}
if(flag==0){
printf("<\n");
}
else if(flag==2)
printf(">\n");
else if(flag==1)
printf("=\n");
}
return 0;
}
另一种方法,
来源于
https://blog.csdn.net/BeNoble_/article/details/107308782
#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;
}