2020牛客暑期多校训练营(第一场) F
Infinite String Comparision
https://ac.nowcoder.com/acm/contest/5666/F
题目描述
For a string , Bobo defines , which is repeats for infinite times, resulting in a string of infinite length.
Bobo has two strings and . Find out the result comparing and 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 , and the second line contains a string .
. consists of lower case letters. The total length of input strings does not exceed .
输出描述:
For each test case, print =
(without quotes) if . Otherwise, print <
if , or >
if .
示例1
输入
aa
b
zzz
zz
aba
abaa
输出
<
>
分析
有些串直接比较即可,如 abc
和 ad
,而类似 abc
和 abca
这样的字符串,需要重复几次变为 abcabc
和 abcaabca
才能看出端倪。不妨将 都重复四次,再直接进行比较,即可得到结果。
代码
/****************************************************************** Copyright: 11D_Beyonder All Rights Reserved Author: 11D_Beyonder Problem ID: 2020牛客暑期多校训练营(第一场) Problem F Date: 8/10/2020 Description: Just Use IQ *******************************************************************/ #include<string> #include<cstdio> #include<iostream> using namespace std; int main() { string a,b; while(cin>>a>>b) { int i; //重复四次 a=a+a+a+a; b=b+b+b+b; //-1 < //0 = //1 > short flag=0; if(a.size()>b.size()) { for(i=0;i<a.size();i++) { if(a[i]<b[i%b.size()]) { flag=-1; break; } else if(a[i]>b[i%b.size()]) { flag=1; break; } } } else { for(i=0;i<b.size();i++) { if(a[i%a.size()]<b[i]) { flag=-1; break; } else if(a[i%a.size()]>b[i]) { flag=1; break; } } } switch (flag) { case -1: putchar('<'); break; case 0: putchar('='); break; case 1: putchar('>'); break; default: break; } putchar('\n'); } return 0; }
收集牛客暑期多校训练营的题解