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. 前置 ...
随机推荐
- Cow Picnic S 更新了(在后面)
解题思路: 从每头奶牛的节点开始做搜索,用dfs走遍所有路径(走到底,不回头).每遍历到一个节点该节点遍历次数就加一,最后所有奶牛都搜索完之后,检查每个节点的遍历次数,如果该节点的遍历次数等于奶牛数则 ...
- HashMap简要介绍
https://www.cnblogs.com/vitasyuan/p/9220773.html 1.HashMap-1.8介绍 HashMap为Map接口的一个实现类,实现了所有Map的操作.Has ...
- centos7 在线或离线安装python3
1.前言 本文会使用到yum和wget,如果两者都不能用,参考安装教程 https://www.cnblogs.com/dennisdong/p/17037248.html 2.查看是否安装wget和 ...
- react 高效高质量搭建后台系统 系列 —— 前端权限
其他章节请看: react 高效高质量搭建后台系统 系列 权限 本系列已近尾声,权限是后台系统必不可少的一部分,本篇首先分析spug项目中权限的实现,最后在将权限加入到我们的项目中来. spug 中权 ...
- 大三末java实习生一面凉经
在南京投了一些小公司,想找个实习,因为知道自己很菜,就收到了一家公司的面试. 面试一般在线上进行,我是在腾讯会议上进行的.面试官其实挺好的,一般不会为难你,因为他知道你是在校生不会懂那么多企业的技术. ...
- 学习Vue过程中遇到的问题---code: 'MODULE_NOT_FOUND'
在学习安装脚手架Vue-cli过程中 步骤为: 第一步(仅第一次执行):全局安装@vue/cli. npm install -g @vue/cli 第二步:切换到你要创建项目的目录,然后使用命令创建项 ...
- P3804 【模板】后缀自动机 (SAM) && P6139 【模板】广义后缀自动机(广义 SAM)
普通 \(\text{SAM Code}\) #include <cstdio> #include <iostream> #include <cstring> #d ...
- CentOS 搭建 samba 服务器并通过 Windows 访问
第一步 下载安装 samba.samba-client sudo yum -y install samba samba-client 终端提示安装完成 第二步 创建共享文件夹,这个文件夹到时候可以通过 ...
- 关于 Knex update 语句报错:Undefined binding(s) detected when compiling UPDATE
下图是详细的报错截图,我敢保证前端传递的数据一个不漏,但还是报我没有绑定对应的字段: 官方文档的使用案例基本上都是where 子句在 update 语句之前.但,select 语句的 where 子句 ...
- Canvas:绘制矩形
函数 CanvasPath.rect(x, y, w, h) 参数名 类型 描述 x Number 矩形起始位置 y Number 矩形起始位置 w Number 矩形宽度 h Number 矩形高度 ...