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. 面向对象程序设计(二):C++模板初探

    背景:老师留了一个作业,对两个数组进行相加,但是总是会出现错误:首先我们需要知道当数组作为参数传递的时候是不能用 sizeof 的,因为当数组传入子函数就变成了普通的数组头:这时候使用 sizeof ...

  2. Java的两个好用的工具包 Apache commons

    Apache commons 介绍 这是apache commons lang3的工具类的截图 这个工具,小皮一般用在业务层较多 这是apache commons codec下面的工具 这个工具包,今 ...

  3. MySQL 中的事务理解

    MySQL 中的事务 前言 原子性 一致性 持久性 并发事务存在的问题 脏读 幻读 不可重复读 隔离性 事务的隔离级别 事务隔离是如何实现 可重复读 和 读提交 串行化 读未提交 可重复读解决了幻读吗 ...

  4. vs code 关联gitee码云或github以及GIT 常用命令

    一.准备 1.本地安装vs code 和GIT源代码管理工具 2.配置vscode git全局变量 打开左下角设置-->点击用户-->搜索git.path-->settings.js ...

  5. JSTL标签库C标签的使用注意事项

    今天在写jsp中在c标签上踩了不少坑,在此记录一下. <c:if>标签单独使用,不与<c:otherwise>配套使用,搭配使用会报错. <c:otherwise> ...

  6. 2021级《JAVA语言程序设计》上机考试试题9

    专业负责人功能页 <%@ page language="java" contentType="text/html; charset=UTF-8" page ...

  7. vue-seamless-scroll滚动加点赞衔接处数据不同步问题

    VUE使用vue-seamless-scroll自动滚动加点赞,因为有两个overhidden导致点击不同同步dom,在代码中会出现两处vue-seamless-scroll上下悬接,悬接处点赞触发没 ...

  8. vue的数据更新视图不同步的处理用Vue.$set()

    // vue的数据更新视图不同步的处理用Vue.$set() // 通过Vue.set方法设置data属性vm.$set(最终值,数组索引,数组值) ==Vue.$set(arr,index,val) ...

  9. LeetCode-396 选转函数

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/rotate-function 题目描述 给定一个长度为 n 的整数数组 nums . 假设 ar ...

  10. Linux操作命令(九)1.comm命令 2.diff命令 3.patch命令

    1.comm 命令 比较文本文件的内容 comm 命令将逐行比较已经排序的两个文件.显示结果包括 3 列:第 1 列为只在第一个文件中找到的行,第 2 列为只在第二个文件中找到的行,第 3 列为两个文 ...