B - Yet Another Palindrome Problem
B - Yet Another Palindrome Problem



思路:
给一个长为n(≤5000≤5000)的数组,问是否存在一个长度至少为3的子序列是回文的。回文的定义是把序列reverse,序列不变,如[10,20,10]就是回文的。
考虑奇数长度的回文序列,删除其首尾元素仍然回文;再考虑偶数长度的回文序列,删除最中间那一对的某个元素,变成奇数长度的回文序列;因此原题等价于判断是否存在一个长度为3的子序列。for两遍就行。
代码:
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int a[5005], b[5005]; int fun(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (b[a[j]])
return 1;
}
b[a[i]] = 1;
}
return 0;
} int main()
{
int t, n;
cin >> t;
while (t--)
{
memset(b, 0, sizeof(b));
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
if (fun(n) == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
B - Yet Another Palindrome Problem的更多相关文章
- B - Yet Another Palindrome Problem的简单方法
You are given an array aa consisting of nn integers. Your task is to determine if aa has some subseq ...
- CF1324B Yet Another Palindrome Problem 题解
原题链接 CF 127个测试点,好评 简要题意: 多组数据,问数组中是否有长度 \(\geq 3\) 的回文子序列. 我们需要找到本质. 题目不让我们求这个长度,只让我们判断,这是为什么呢? 如果答案 ...
- Codeforces Round #627 (Div. 3) B - Yet Another Palindrome Problem(逻辑)
题意: 问一个数组中是否存在至少长为3的回文子数组(按下标排列,可不连续). 思路: 找三个相同数或两个不连续的相同数. #include <bits/stdc++.h> using na ...
- [Algorithms] Determine if a string is a palindrome
A palindrome is a string that reads the same forward and backward, for example, radar, toot, and mad ...
- OJ题解记录计划
容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001 A+B Problem First AC: 2 ...
- 2016-2017 ACM-ICPC CHINA-Final Solution
Problem A. Number Theory Problem Solved. 水. #include<bits/stdc++.h> using namespace std; ; typ ...
- 带权并查集:CF-2015 ACM Arabella Collegiate Programming Contest(F题)
F. Palindrome Problem Description A string is palindrome if it can be read the same way in either di ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- Codeforces Round #627 (Div. 3)
1324A - Yet Another Tetris Problem(思维) 题意 给一个数组,每一个数组中的元素大小表示在竖直方向的方块数量,元素相邻怎竖直方向的方块也相邻,类似于俄罗斯方块当底层被 ...
- KMP 与 Z 函数
\(\text{By DaiRuiChen007}\) 一.KMP 算法 I. 问题描述 在文本串 \(S\) 中找到模式串 \(T\) 的所有出现,其中 \(|S|=n,|T|=m\) II. 前置 ...
随机推荐
- day08-SpringMVC底层机制简单实现-04
SpringMVC底层机制简单实现-04 https://github.com/liyuelian/springmvc-demo.git 8.任务7-完成简单视图解析 功能说明:通过目标方法返回的 S ...
- C#中的Byte,String,Int,Hex之间的转换函数
/// <summary> Convert a string of hex digits (ex: E4 CA B2) to a byte array. </summary> ...
- ASP.NET Core - 依赖注入(一)
1. Ioc 与 DI Ioc 和DI 这两个词大家都应该比较熟悉,这两者已经在各种开发语言各种框架中普遍使用,成为框架中的一种基本设施了. Ioc 是控制反转, Inversion of Contr ...
- 与 Flutter 共创未来 | Flutter Forward 活动精彩回顾
作者 / Google 开发者框架和语言 (含 Flutter.Dart 和 Go) 产品经理 & 用户体验总监 Tim Sneath 我们很高兴可以在 Flutter Forward 活动 ...
- LG P4148 简单题
\(\text{Code}\) #include <cstdio> #include <iostream> #include <algorithm> #define ...
- RETRO研究: 持续缓解的RA患者的减量维持方案[EULAR2015_SAT0056]
RETRO研究: 持续缓解的RA患者的减量维持方案 SAT0056 RETRO – STUDY OF REDUCTION OF THERAPY IN PATIENTS WITH RHEUMATOI ...
- K8S 1.20 弃用 Docker 评估之 Docker CLI 的替代产品
title: K8S 1.20 弃用 Docker 评估之 Docker CLI 的替代产品 tags: - Docker - K8S - OCI - 容器 - 最佳实践 - RedHat - Sko ...
- RadioGroup 自动换行且保留点击事件
转:(22条消息) RadioGroup 自动换行且保留点击事件_再见孙悟空_的博客-CSDN博客 public class MyRadioGroup extends RadioGroup { pri ...
- Codeforces Round #757 (Div. 2) - D2. Divan and Kostomuksha (hard version)
GCD + DP + 调和级数/埃式筛 [Problem - D - Codeforces](https://codeforces.com/contest/1610/problem/D) 题意 给出一 ...
- Linux中使用Makefile来运行QuestaSim
环境:Win7x64,VMware15.0,centOS7.0,QuestaSim10.7c 假设已经编辑好了一个全加器还有运行这个DUT的testbech,代码如下: 点击查看代码 // filen ...