牛客网-Infinite String Comparision

42 篇文章 1 订阅
6 篇文章 0 订阅

有 志 者 事 竟 成,破 釜 沉 舟,百 二 秦 关 终 属 楚;
苦 心 人 天 不 负,卧 薪 尝 胆,三 千 越 甲 可 吞 吴!

Infinite String Comparision

题目描述

For a string xx, Bobo defines x = =xxx…, which is xx repeats for infinite times,
resulting in a string of infinite length.
Bobo has two strings aa and bb. 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

Sample Input

aa
b
zzz
zz
aba
abaa

Sample Output

<
=
>

解题思路

无限循环并不需要一直循环,只需要让无限循环应该具有的性质体现出来即可。无非也就在关联处会有一些坑点。
咱们要让最大长度的字符串展开二倍。这样所有的细节都会在这个新的字符串中体现。然后让小的字符串一直扩增,直到长度相同。
这时两个新的字符串就能代表这无限循环的字符串了。

AC时间到

#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<utility>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#pragma warning(disable:4244)
#define PI 3.141592653589793
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define EPS 1.0e-8
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a) {
	if (a < 0)putchar('-'), a = -a;
	if (a >= 10)out(a / 10);
	putchar(a % 10 + '0');
}
ll phi(ll n)
{
	ll ans = n, mark = n;
	for (ll i = 2; i * i <= mark; i++)
		if (n % i == 0) { ans = ans * (i - 1) / i; while (n % i == 0)n /= i; }
	if (n > 1)ans = ans * (n - 1) / n; return ans;
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
ll mat_mod;
struct Mat {
	ll m[10][10];
};
Mat Mul(Mat A, Mat B, ll mat_size) {
	Mat res;
	memset(res.m, 0, sizeof(res.m));
	for (int i = 0; i < mat_size; i++)for (int j = 0; j < mat_size; j++)for (int k = 0; k < mat_size; k++)
		res.m[i][j] = (res.m[i][j] + (A.m[i][k] * B.m[k][j]) % mat_mod) % mat_mod;
	return res;
}
Mat mat_qpow(Mat data, ll power, ll mat_size) {
	Mat res;
	memset(res.m, 0, sizeof(res.m));
	for (int i = 0; i < mat_size; i++)res.m[i][i] = 1;
	while (power) {
		if (power & 1)res = Mul(res, data, mat_size);
		data = Mul(data, data, mat_size), power >>= 1;
	}
	return res;
}
#define Floyd for(int k = 1; k <= n; k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
#define read read()
string c(string b, int n, int l)
{
	string res = b;
	for (int i = 0; i < n; i++)res += b;
	return res.substr(0, l);
}
int main()
{
	string a, b;
	while (cin >> a >> b)
	{
		if (a.size() >= b.size())
		{
			a = a + a;
			int la = a.size();
			int lb = b.size();
			int k = la / lb;
			k++;
			b = c(b, k, la);
			if (a > b)cout << ">" << endl;
			else if (a < b)cout << "<" << endl;
			else cout << "=" << endl;
		}
		else
		{
			swap(a, b);
			a = a + a;
			int la = a.size();
			int lb = b.size();
			int k = la / lb;
			k++;
			b = c(b, k, la);
			if (a > b)cout << "<" << endl;
			else if (a < b)cout << ">" << endl;
			else cout << "=" << endl;
		}
	}
}

By-轮月

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值