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. 前置 ...
随机推荐
- 简单搭建react开发环境
目录 前言 一.node.js使用 二.vscode 1.打开文件 2.运行 3. 第一个react程序,从helloworld写起 总结 前言 安装前需要安装:node.js.vscode 一.no ...
- 学习Java Day16
今天学习静态数据的使用
- Vue搭建项目的完整流程 如何搭建一个完整的vue项目 vue项目架构
vue项目架构 技术栈:vue3.vue-router .vuex(和pinia).element plus .axios.ts.sass 1.安装vue3 脚手架+ ts vue create ad ...
- Windows常用指令与常见问题
关键目录 启动目录:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup hosts:C:\Windows\System32\dri ...
- uWSGI 结合 nginx 配置动静分离
uWSGI 结合 nginx 配置动静分离 目录 uWSGI 结合 nginx 配置动静分离 1 环境准备 2 初始配置文件 2.1 uwsgi 配置文件 2.2 Nginx配置文件 2.3 Djan ...
- 网页js版音频数字信号处理:H5录音+特定频率信号的特征分析和识别提取
目录 一.网页中的音频数据源 二.FFT:时域转频域 三.信号的特征分析 四.信号的识别提取 附录 音频数字信号处理 Audio DSP (Digital Signal Processing) 是一个 ...
- .NET周报 【2月第4期 2023-02-25】
国内文章 .NET微服务系统迁移至.NET6.0的故事 https://www.cnblogs.com/InCerry/p/microservice-migration-net-6.html 本次迁移 ...
- GIS若干相关的概念
投影 常用的投影坐标系: CGCS2000_3_Degree_GK_CM_111E CGCS2000_3_Degree_GK_Zone_37 CGCS2000_GK_Zone_19 Beijing_1 ...
- pat乙级1022 D进制的A+B
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #de ...
- IDEA+java swing+MySQL配置
1.建立一个java项目(不是空项目) 2.创建GUI Form(减少代码压力) 生成代码 出现了这个窗体 此时说明swing已经可用了 3.连接MySQL