CF1324B Yet Another Palindrome Problem 题解

原题链接

CF 127个测试点,好评

简要题意:

多组数据,问数组中是否有长度 \(\geq 3\) 的回文子序列。

我们需要找到本质。

题目不让我们求这个长度,只让我们判断,这是为什么呢?

如果答案是 YES,那么必然存在一个长度为 \(3\) 的回文子序列。否则为 NO.

你想,如果原数组的最长回文序列是奇数的话,只要每次在两边同时删去一个,就可以得到长度为 \(3\) 的回文子序列。

如果是偶数,只要每次在两边同时删去一个,再在最中间两个任意删去一个,也可以得到长度为 \(3\) 的回文子序列。

反之如果没有,则就没有了。

比方说:

10
1 2 3 4 2 1 4 2 4 3

你发现这个数组的最长回文子序列为:

3 4 2 2 4 3

是偶数,那么其中必然存在一个长度为 \(3\) 的是:

4 2 4

(当然不止一种)

所以,题意改为:

判断一个数组中是否有长度为 \(3\) 的回文子序列。

???这还用判断吗???

长度为 \(3\),中间数没有什么限制,旁边两个数相等即可。

也就是说,需要 找到两个相等的数,使得它们不相邻(即中间还放得下一个数,构成回文子序列)

???这还用判断吗???

显然,一波哈希解决问题。正好 \(a_i \leq n\),这不是天赐良机?

时间复杂度: \(O(T \times n)\).

空间复杂度: \(O(n)\).

实际得分: \(100pts\).

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

inline int read(){char ch=getchar();int f=1;while(ch<‘0‘ || ch>‘9‘) {if(ch==‘-‘) f=-f; ch=getchar();}
	int x=0;while(ch>=‘0‘ && ch<=‘9‘) x=(x<<3)+(x<<1)+ch-‘0‘,ch=getchar();return x*f;}

int a[5001],n;
int T;

int main(){
	T=read(); while(T--) {
		n=read(); bool f=0;
		memset(a,0,sizeof(a));
		for(int i=1,t;i<=n;i++) {
			t=read();
			if(a[t] && i-a[t]>1) f=1;
			if(!a[t]) a[t]=i;
		} if(f) puts("YES");
		else puts("NO");
	}
	return 0;
}

原文地址:https://www.cnblogs.com/bifanwen/p/12550080.html

时间: 2024-08-30 18:02:13

CF1324B Yet Another Palindrome Problem 题解的相关文章

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决. DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i

Palindrome Degree题解

Palindrome Degree题解 其实是道水题, 但是我太弱了!!! 开始想着如何判断后缀是回文, 屈辱看题解后发现, 只要判断前缀,然后判断后缀的反向是否与前缀相等即可, 但是我居然将kmp与回文弄混了,直接判前后缀相不相等, 太弱了!!败犬的哀嚎 #include<bits/stdc++.h> #define ull unsigned long long using namespace std; const int N=5e6+7; int n,t,f[N],ans=0; ull b

POJ 3280 Cheapest Palindrome 动态规划法题解

Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are

POJ2826:An Easy Problem?!——题解(配特殊情况图)

http://poj.org/problem?id=2826 题目大意:给两条线,让它接竖直下的雨,问其能装多少横截面积的雨. ———————————————————————————— 水题,看题目即可知道. 但是细节是真的多……不过好在两次AC应该没算被坑的很惨(全程没查题解). 推荐交之前看一下讨论版的数据,方便一次AC(我第一次就是作死直接交了结果我自己都想好的情况忘了写了……) 相信看到这篇题解的人都看过题了,那么先说细节: 1.C++提交(G++不知道为什么WA了……) 2.精度 3.

B - Yet Another Palindrome Problem

You are given an array aa consisting of nn integers. Your task is to determine if aa has some subsequence of length at least 33 that is a palindrome. Recall that an array bb is called a subsequence of the array aa if bb can be obtained by removing so

Leetcode-The Skyline Problem题解

一. 题目 Leetcode平台上天际线问题 A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo

HDU4632 Palindrome subsequence 题解 区间DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632[题目描述]<回文子串数量>给你一个长度为N(N≤1000)的字符串,你输出它所有的回文子串的数量(对10007取模).只要从字符串 s 中顺序地取出一些字符(不要求连续,可以是 s 本身,不能是空串),不改变他们的顺序的情况下,这个子串是一个回文串,那么他就是一个 s 的回文子串.[输入格式]首先是一个数T(T≤50),表示测试用例的数量.接下来T行,每行包含一个字符串.[输出格式]对于每一

CF1324A Yet Another Tetris Problem 题解

原题链接 简要题意: 再简要一波: 每次可以把一个数增加 \(2\),问最后能不能让所有数相等.(也就是抵消掉) 什么?题意变成这样子还做个啥? 你会发现,必须所有数的奇偶性都相同,才可以:反之就不可以. 这结论不用证明了,因为每次增加不会改变奇偶性的. #pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; inline int read(){char ch=getchar();int f=1;while(ch