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的更多相关文章

  1. 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 ...

  2. CF1324B Yet Another Palindrome Problem 题解

    原题链接 CF 127个测试点,好评 简要题意: 多组数据,问数组中是否有长度 \(\geq 3\) 的回文子序列. 我们需要找到本质. 题目不让我们求这个长度,只让我们判断,这是为什么呢? 如果答案 ...

  3. Codeforces Round #627 (Div. 3) B - Yet Another Palindrome Problem(逻辑)

    题意: 问一个数组中是否存在至少长为3的回文子数组(按下标排列,可不连续). 思路: 找三个相同数或两个不连续的相同数. #include <bits/stdc++.h> using na ...

  4. [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 ...

  5. OJ题解记录计划

    容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001  A+B Problem First AC: 2 ...

  6. 2016-2017 ACM-ICPC CHINA-Final Solution

    Problem A. Number Theory Problem Solved. 水. #include<bits/stdc++.h> using namespace std; ; typ ...

  7. 带权并查集: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 ...

  8. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  9. Codeforces Round #627 (Div. 3)

    1324A - Yet Another Tetris Problem(思维) 题意 给一个数组,每一个数组中的元素大小表示在竖直方向的方块数量,元素相邻怎竖直方向的方块也相邻,类似于俄罗斯方块当底层被 ...

  10. KMP 与 Z 函数

    \(\text{By DaiRuiChen007}\) 一.KMP 算法 I. 问题描述 在文本串 \(S\) 中找到模式串 \(T\) 的所有出现,其中 \(|S|=n,|T|=m\) II. 前置 ...

随机推荐

  1. Vue35 路由

    1 简介 vue-router是vue的一个插件,专门用来实现SPA应用.SPA也就是单页Web应用,特点是:整个应用只有一个完整的页面,点击页面中的导航链接不会刷新页面,只会做页面的局部更新,数据需 ...

  2. 用ChatGPT来了解ChatGPT

    用ChatGPT来了解ChatGPT 之前学习一个新技术, 想着要搞清楚这6个问题(来自陈皓介绍的学习方法): 1.这个技术出现的背景, 初衷, 要达到什么样的目标或是要解决什么样的问题. 2.这个技 ...

  3. IDEA新手使用教程【详解】

    IDEA是一款功能强悍.非常好用的Java开发工具,近几年编程开发人员对IDEA情有独钟. Intellij Idea使用技巧总结 1.如何设置通过鼠标滑轮改变编辑器字体大小 2.如何设置自动导包功能 ...

  4. IOS12 + Xscode12 报错:Building for iOS Simulator, but linking in dylib built for iOS, file '/Users/XXX/Desktop/XXXX/XXX.framework/JSSDK' for architecture arm64

    问题描述:编译过程出现错误,Building for iOS Simulator, but linking in dylib built for iOS, file '/Users/XXX/Deskt ...

  5. Vmware15 + Ubuntu18.0.4 安装教程(史上最详细记录)【多图预警】

    转载csdn: Vmware15 + Ubuntu18.0.4 安装教程(史上最详细记录)[多图预警]_亦靈兒的博客-CSDN博客

  6. linux配置两个不同网段的ip and linux批量添加连续IP

    转载csdn: centos 下批量添加连续IP_cdefg198的专栏-CSDN博客_centos批量添加ip 转载csdn: linux配置两个不同网段的ip_子曰小玖的博客-CSDN博客_lin ...

  7. Centos 7.x系统下忘记用户登录密码,重置密码的方法

    转载csdn: Centos 7.x系统下忘记用户登录密码,重置密码的方法_ATree的博客-CSDN博客_centos7密码忘记 重置密码的方法 Centos7修改root密码_shanvlang的 ...

  8. C# 通过反射获取类字段名和值并加入到字典中(包含递归获取)

    //测试类 public class HKAddvisitor1{    public string code { set; get; }    public string msg { set; ge ...

  9. css3自动滚动

    <!DOCTYPE html> <html lang="en"><div class="wrap"> <ul clas ...

  10. 使用Shapefile-js读取shp文件并使用WebGL绘制

    1. 引言 坐标数据是空间数据文件的核心,空间数据的数据量往往是很大的.数据可视化是GIS的一个核心应用,绘制海量的坐标数据始终是一个考验设备性能的难题,使用GPU进行绘制可有效减少CPU的负载,提升 ...