D. Peculiar Movie Preferences(思维 + 一个坑)

Problem - D - Codeforces

米海打算去看电影。他只喜欢回文电影,所以他想跳过一些(可能是零)场景,让电影的其余部分变成回文。给你一个包含n个长度不超过3的非空字符串的列表,代表Mihai的电影场景。如果s的子序列非空,并且子序列中字符串的串联顺序为回文,则称为awesome。你能帮Mihai检查是否至少有一个很棒的s子序列吗?一个回文是向后读和向前读一样的字符串,例如字符串"z", "aaa", "aba", "abccba"是回文,但是字符串"codeforces", "reality", "ab"不是。序列A是非空序列b的非空子序列,如果A可以通过删除几个(可能为0,butnotal) eleents。输入输入的第一行包含一个整数t (1 t < 100)测试用例的数量。测试用例的描述如下。每个测试用例的第一行包含一个整数n (1 < n < 105)——电影中的场景数。然后是n行,第i行包含一个长度不超过3的非空字符串s,由小写拉丁字母组成。它保证所有测试用例的n和不超过105。输出对于每个测试用例,如果存在一个很棒的s子序列,则打印“YES”,否则打印“NO”(不区分大小写)。

Example

input

Copy

 

6

5

zx

ab

cc

zx

ba

2

ab

bad

4

co

def

orc

es

3

a

b

c

3

ab

cd

cba

2

ab

ab

output

Copy

YES
NO
NO
YES
YES
NO

题解:
这题思路并不难想,记录每个串的前缀即可,看后来的串整个反转或后缀反转.是否出现过即可

但是有一个很大的坑点,就是需要两个map来记录

为什么?

由于我们会记录前缀,长度为3时记录(01,012),前缀为0时不需要记录的,因为如果出现单个字母,则一定成立

长度为2时记录(01),理由同上

但是我们在询问反转后缀时会有这几种情况

长度为2时,询问整个反转(10)是否出现过,没什么问题

长度为3时,询问整个反转(210)是否出现过,也没什么问题

关键是

长度为3时,询问反转(21),肯能会出现与记录长度为3时(01)向匹配

类似abc  dba,尽管前后缀相配,但却不对的

所以用两个map记录

记得特判首位相同的情况

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<cstdio>
using namespace std;
//#define int long long
const int N = 2e5 + 10;
typedef pair<int, int> PII;
typedef long long ll;
void solve() 
{
	int n;
	cin >> n;
	int ff = 0;
	map<string,int >  a;
	map<string,int> b;
	for(int i = 1;i <= n;i++)
	{
		string s;
		cin >> s;
		string p(s),q(s);
		q.erase(0,1);
		reverse(q.begin(),q.end());
		reverse(p.begin(),p.end());
		if(a[q]||a[p]||b[p])
		ff = 1;
		if(s.front() == s.back() || s.size() == 1)
		ff = 1;
		a[s] = 1;
		s.erase(s.size()-1,1);
		b[s] = 1;
	}
	if(ff)
	{
		cout <<"YES\n";
	}
	else
	{
		cout <<"NO\n";
	}
}

//1 2 4
signed main() 
{
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int t = 1;
//	cin >> t;
scanf("%d",&t);
	while (t--) 
	{
		solve();
	}
}
//1 1 1 0 1

//1 1 1 0 1
//1 1 1 0 1
//1 1 1 0 1
//0 1 1 1 1
//0 1 1 1 1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L). To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river. Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N). FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks. Input Line 1: Three space-separated integers: L, N, and M Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position. Output Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks Sample Inputcopy Outputcopy 25 5 2 2 14 11 21 17 4 Hint Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
07-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值