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. 前置 ...
随机推荐
- (原创)【B4A】一步一步入门03:APP名称、图标等信息修改
一.前言 上篇 (原创)[B4A]一步一步入门02:可视化界面设计器.控件的使用 中我们已经了解了B4A程序的基本框架,现在我们还进一步讲解. 本篇文章会讲解如何修改APP的名称.图标等信息,以让一个 ...
- Servlet HTTP Request Response笔记
# 今日内容: 1. Servlet 2. HTTP协议 3. Request## Servlet: 1. 概念 2. 步骤 3. 执行原理 4. 生命周期 ...
- 学习Java Day12
今天学习了多维数组
- uboot启动过程 3
uboot启动过程1描述到 _start -> reset -> save_boot_params -> save_boot_params_ret -> cpu_init_ ...
- JZOJ 4211. 【五校联考1day2】送你一颗圣诞树
题目 解析 \(Code\) #include<cstdio> #include<map> #include<iostream> #include<cstri ...
- Hexo系列(二):Hexo安装
作者:独笔孤行 官网: http://anyamaze.com 公众号:云实战 1.安装nodejs和npm yum install epel-release -y yum install ...
- CSS 定位position
.link span { position: absolute; top: 0; left: 50%; transform: translateX(-50%); }
- PYTHON编写程序练习-打印99乘法表
使用for循环嵌套的知识点编写 for i in range(1,10): #第一层循环,循环乘数 for j in range(1,i+1): #第二层循环,循环被乘数 print(f&qu ...
- oracle to mogdb 迁移---mtk工具
## 一.MTK工具介绍--------- MTK–异构数据迁移工具 MTK全称为 Database Migration Toolkit,是一个可以将Oracle/DB2/MySQL/openGaus ...
- 单兵 Web 快速开发框架!
Jmix 是低代码? 自从 Jmix 2018 年在中国推广以来(那时叫 CUBA 平台),很多开发者会在使用之前询问我们,Jmix 是不是低代码,扩展性怎么样? 低代码应用程序平台(LCAP)是当今 ...