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

输出

<

>

分析

  有些串直接比较即可,如 abcad,而类似 abcabca 这样的字符串,需要重复几次变为 abcabcabcaabca 才能看出端倪。不妨将 都重复四次,再直接进行比较,即可得到结果。

代码

/******************************************************************
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;
}
牛客暑期多校训练营题解 文章被收录于专栏

收集牛客暑期多校训练营的题解

全部评论

相关推荐

10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
10-21 23:48
蚌埠坦克学院
csgq:可能没hc了 昨天一面完秒挂
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务