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 some (possibly, zero) elements from aa (not necessarily consecutive) without changing the order of remaining elements. For example, [2][2], [1,2,1,3][1,2,1,3] and [2,3][2,3] are subsequences of [1,2,1,3][1,2,1,3], but [1,1,2][1,1,2]and [4][4] are not.
Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array aa of length nn is the palindrome if ai=an−i−1ai=an−i−1 for all ii from 11 to nn. For example, arrays [1234][1234], [1,2,1][1,2,1], [1,3,2,2,3,1][1,3,2,2,3,1] and [10,100,10][10,100,10] are palindromes, but arrays [1,2][1,2] and [1,2,3,1][1,2,3,1] are not.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.
Next 2t2t lines describe test cases. The first line of the test case contains one integer nn (3≤n≤50003≤n≤5000) — the length of aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), where aiai is the ii-th element of aa.
It is guaranteed that the sum of nn over all test cases does not exceed 50005000 (∑n≤5000∑n≤5000).
Output
For each test case, print the answer — "YES" (without quotes) if aa has some subsequence of length at least 33 that is a palindrome and "NO" otherwise.
Example
5
3
1 2 1
5
1 2 2 3 2
3
1 1 2
4
1 2 2 1
10
1 1 2 2 3 3 4 4 5 5
YES
YES
NO
YES
NO
Note
In the first test case of the example, the array aa has a subsequence [1,2,1][1,2,1] which is a palindrome.
In the second test case of the example, the array aa has two subsequences of length 33 which are palindromes: [2,3,2][2,3,2] and [2,2,2][2,2,2].
In the third test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.
In the fourth test case of the example, the array aa has one subsequence of length 44 which is a palindrome: [1,2,2,1][1,2,2,1] (and has two subsequences of length 33 which are palindromes: both are [1,2,1][1,2,1]).
In the fifth test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.
这个题的意思是能不能在一组数中找到一个回文数(只要有三个数就行,而且在数组中的位置不一定要挨着)
这道题再简化一步的想法是能否在一个数组中找到两个相等的数,并且第一关数和第二个数之间至少隔一个位置
题目要求数组的大小上界为5000,一定要在主函数外定义这个数组,因为这个数组太大了,当时比赛的时候一直找不到哪里错了,下来之后才知道因该在全局定义
#include<iostream>
using namespace std;
const long long maxn=+;
int a[maxn];
int main(){
int t,i,j,flag,k,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&a[i]);
}
flag=;
for(i=;i<n-;i++){ //第一个数最多遍历到倒第三个位置
if(i&==) continue; //从第0个位置开始如果第一个数的位置是2的倍数,那么这个位置就和第二个数的上一个位置重合,直接跳过
for(k=i+;k<n;k++){
if(a[k]==a[i]){
cout<<"YES"<<endl;
flag=;
break;
}
}
if(flag==) break;
}
if(i==n-) cout<<"NO"<<endl;
}
}
B - Yet Another Palindrome Problem的简单方法的更多相关文章
- Xcode7使用插件的简单方法&&以及怎样下载到更早版本的Xcode
Xcode7自2015年9上架以来也有段时间了, 使用Xcode7以及Xcode7.1\Xcode7.2的小伙伴会发现像VVDocumenter-Xcode\KSImageNamed-Xcode\HO ...
- chm转换为html的超简单方法
在Windows下chm转换为html的超简单方法(反编译CHM文件的方法) 通过调用Windows命令,将chm 文件转换为html 文件. 方法: 命令行(cmd),输入hh -decompile ...
- JS去掉首尾空格 简单方法大全(原生正则jquery)
JS去掉首尾空格 简单方法大全 var osfipin= ' http://www.cnblogs.com/osfipin/ '; //去除首尾空格 osfipin.replace(/(^\s*)|( ...
- python反转字符串(简单方法)及简单的文件操作示例
Python反转字符串的最简单方法是用切片: >>> a=' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号之前)表示切片 ...
- 使用strace工具故障排查的5种简单方法
使用strace工具故障排查的5种简单方法 本文源自5 simple ways to troubleshoot using strace strace 是一个非常简单的工具,用来跟踪可执行程序的系统调 ...
- PhotoShop中画圆角矩形最简单方法(图文并茂)!
PhotoShop中画圆角矩形最简单方法(图文并茂)! 1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHhubjUyMA==/font/5a6L5L ...
- ASP.net中导出Excel的简单方法介绍
下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...
- 两个简单方法加速DataGridView
两个简单方法加速DataGridView (2009-03-24 16:57:13) 转载▼ 标签: 杂谈 分类: .NET DataGridView虽然好用,但是如果数据量比较大的话就会出现性能的问 ...
- shell实现四则运算简单方法
在刚刚学习写shell 批处理时候,进行逻辑运算中,少不了需要进行基础的:四则运算,这里说说在linux shell 里面简单的实现方法.1.简单方法$ b=$((5*5+5-3/2)) $ echo ...
随机推荐
- Numpy入门(一):Numpy的安装和创建
在数据分析和机器学习中,大量的使用科学计算,Numpy提供了大型矩阵计算的方式,而这些是python标准库中所缺少的.Numpy也是许多优秀的第三方库的基础,依赖于Numpy的库非常多,后续会慢慢的进 ...
- 2019-04-18-NFV基础概念
NFV技术的起源和概念 在移动互联网时代,运营商面临内外困局.就自身而言,采用的流量增长-网络扩容-收入增长的商业模型正在失效,庞大.僵化的电信基础网络,不能够满足用户的丰富需求:就竞争对手而言,互联 ...
- 11. 无数人难办事? - 迪米特法则(LoD)
11.1 第一天上班 时间: 4月2日19点 地点: 小菜大鸟住所的客厅 任务: 小菜, 大鸟 "回来啦! 怎么样? 第一天上班感受多吧." 大鸟关关心的问道. ...
- jQuery学习笔记二
事件监听者是DOM的一部分,任何页面都可以增加事件监听者.浏览器利用事件监听者监视页面上做了什么,然后告诉Javascript解释器是否需要采取行动.如:$('#showMessage').click ...
- 【原创】从零开始搭建Electron+Vue+Webpack项目框架(五)预加载和Electron自动更新
导航: (一)Electron跑起来(二)从零搭建Vue全家桶+webpack项目框架(三)Electron+Vue+Webpack,联合调试整个项目(四)Electron配置润色(五)预加载及自动更 ...
- phpstudy渗透到服务器
0x00 目标站点www.test.ichunqiu 0x01 尝试登陆系统 -尝试弱密码登陆 结果:forbidden!!! -尝试万能账号密码登陆 1‘ or 1=1--+ 和 1‘ or 1=1 ...
- spring——AOP原理及源码(三)
在上一篇中,我们创建并在BeanFactory中注册了AnnotationAwareAspectJAutoProxyCreator组件.本篇我们将要探究,这个组件是在哪里以及何时发挥作用的. 调试的起 ...
- 修改gridfilters.js源码,往后台多传递一个参数,并设置NumericFilter、StringFilter默认提示信息
创作不易,转载请注明出处!!! 效果 修改:ext-extend.js源码 在最后面添加3行,重写方法 代码拷贝区 Ext.override(Ext.ux.grid.GridFilters, { me ...
- 【Amaple教程】2. 模块
正如它的名字,模块用于amaplejs单页应用的页面分割,所有的跳转更新和代码编写都是以模块为单位的. 定义一个模块 一个模块由<module>标签对包含,内部分为template模板.J ...
- springboot连接redis错误 io.lettuce.core.RedisCommandTimeoutException:
springboot连接redis报错 超时连接不上 可以从以下方面排查 1查看自己的配置文件信息,把超时时间不要设置0毫秒 设置5000毫秒 2redis服务长时间不连接就会休眠,也会连接不上 重 ...