牛客练习赛17-B 好位置

链接: https
://www.nowcoder.com/acm/contest/109/B来源:牛客网

时间限制:C / C ++ 1秒,其他语言2秒
空间限制:C / C ++ 32768K,其他语言65536K
64bit IO格式:%lld

题目描述

给出两个串s和x
定义s中的某一位i为好的位置,当且仅当存在s的子序列 满足y = x且存在j使得i = k j 成立。
问s中是否所有的位置都是好的位置。

输入描述:

一行两个字符串S,X,这两个串均由小写字母构成。
1 <= | s |,| x | <= 200000

输出描述:

是表示是。
没有表示不是。

解题思路:这里一个很精妙的想法就是:如果s中的每个位置i,都是好位置的话,就意味着s中的串是x字符串的循环。否则的话不满足条件,所以可以通过判断s[i]=?x[i%strlen(x)]来判断是否为好位置。

AC代码:

#include<iostream>
#include<string>
using namespace std;
const int maxn = 2e5 + 10;
char s[maxn], x[maxn];
int main()
{
	cin >> s >> x;
	int slength = strlen(s);
	int xlength = strlen(x);
	for (int i = 0; i < slength; i++)
	{
		if (s[i] != x[i%xlength])
		{
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40129237/article/details/80229791