Kalindrome Array

题目链接:

Kalindrome Array - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题面翻译

对于长度为 \(m\) 的序列 \(b\),我们称 \(b\) 是「回文的」,当且仅当对于所有 \(i\in[1,m]\),都有 \(b_i=b_{m-i+1}\)。特别的,空序列也是回文的。

对于一个序列,我们称其是「可爱的」,当且仅当且满足如下条件:

  • 存在数 \(x\),使得删除序列中若干值等于 \(x\) 的元素后,序列是回文的。(删除元素后,剩余元素会并在一起)

需要注意的是,你并不需要删除所有值等于 \(x\) 的元素,并且,你也可以选择不删除任何元素。

例如:

  • \([1,2,1]\) 是可爱的,因为你不需要删除任何一个数,其本身就是回文的。
  • \([3,1,2,3,1]\) 是可爱的,因为你可以选择 \(x=3\),然后删除所有值等于 \(3\) 的元素,将其变为回文的。
  • \([1,2,3]\) 则不是可爱的。

现在蓝给出了一个长度为 \(n\) 的序列 \(a\),她希望你能帮她确定其是否是可爱的。

本题多组数据,数据组数为 \(t\),会在输入的开头给出。对于每组数据,如果给出的序列 \(a\) 是可爱的,请输出 YES,否则输出 NO

题目数据满足:\(1 \leq t \leq 10^4\),\(1 \leq n \leq 2\times10^5\),\(1 \leq a_i \leq n\),\(1 \leq \sum n \leq 2\times10^5\)。

题目描述

An array $ [b_1, b_2, \ldots, b_m] $ is a palindrome, if $ b_i = b_{m+1-i} $ for each $ i $ from $ 1 $ to $ m $ . Empty array is also a palindrome.

An array is called kalindrome, if the following condition holds:

  • It's possible to select some integer $ x $ and delete some of the elements of the array equal to $ x $ , so that the remaining array (after gluing together the remaining parts) is a palindrome.

Note that you don't have to delete all elements equal to $ x $ , and you don't have to delete at least one element equal to $ x $ .

For example :

  • $ [1, 2, 1] $ is kalindrome because you can simply not delete a single element.
  • $ [3, 1, 2, 3, 1] $ is kalindrome because you can choose $ x = 3 $ and delete both elements equal to $ 3 $ , obtaining array $ [1, 2, 1] $ , which is a palindrome.
  • $ [1, 2, 3] $ is not kalindrome.

You are given an array $ [a_1, a_2, \ldots, a_n] $ . Determine if $ a $ is kalindrome or not.

输入格式

The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the length of the array.

The second line of each test case contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \le a_i \le n $ ) — elements of the array.

It's guaranteed that the sum of $ n $ over all test cases won't exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, print YES if $ a $ is kalindrome and NO otherwise. You can print each letter in any case.

样例 #1

样例输入 #1

4
1
1
2
1 2
3
1 2 3
5
1 4 4 1 4

样例输出 #1

YES
YES
NO
YES

提示

In the first test case, array $ [1] $ is already a palindrome, so it's a kalindrome as well.

In the second test case, we can choose $ x = 2 $ , delete the second element, and obtain array $ [1] $ , which is a palindrome.

In the third test case, it's impossible to obtain a palindrome.

In the fourth test case, you can choose $ x = 4 $ and delete the fifth element, obtaining $ [1, 4, 4, 1] $ . You also can choose $ x = 1 $ , delete the first and the fourth elements, and obtain $ [4, 4, 4] $ .

思路讲解:

我们首先有一个特例:当原序列是回文序列时,不需要删除任何数字,它本身就是”可爱的“。

这时,我们只需要写出判断一个字符串序列是否是回文序列即可。

我们采用双指针法对数组进行回文序列判断

bool ishuiwen(int n, int b[])
{
//若i和n-i+1对应数值不同,肯定不是回文序列
for (int i = 1; i <= n; i++) {
if (b[i] != b[n - i + 1]) return false;
}
return true;
}

然后,我们就来处理普遍情况,也是采用双指针的办法,从数组的两头出发,检查对称位置是否有不相同的元素,

根据题意,我们遇见不相同元素时,可以任意删除其中的一个元素,以数组[3,1,2,3,1]为例,我们删除一个数字,我们如果删除所有等于这个数字的值以后,如果是回文的,那删除这个数字的某些值一定是回文的。比如,这里的3和1位置不同,如果我们删除某些3以后得到的序列是回文序列,那么我们删除所有的3以后得到的序列一定是回文序列,反之同理,即:如果我们删除所有的3以后,得到的序列不是回文序列,那么我们删除某些数量的3得到的序列肯定不是回文序列,这个因为回文序列的性质,一个序列如果是回文的话,那么对应位置的元素肯定是偶数次出现的,我们讲这个数字全部删掉以后,这个数字出现的次数为0次,也是偶次。

懂得这个道理以后,我们只需要使用双指针遍历数组,碰见对应位置不相同的元素,我们就试着删除数组中所有值等于这个元素的元素,比如这里的3和1,是对称位置,并且数值不同,我们只需要尝试着将所有的3删除,检查是否是回文序列,或者删除所有的1,检查是否是回文序列,这两种情况只要有一种满足回文序列,那这个字符串就是”可爱的“

这里我们需要使用一个新的数组来储存删除了元素后的新的数组,定义为tmp。

其它要注意的细节都在注释中详细描述了。

代码:

#include<iostream>
#include<cstring>
using namespace std;
//预处理n的最大值,这是一个好习惯
const int N = 2e5 + 10;
int a[N], tmp[N];
//检查一串数是否是回文序列
bool ishuiwen(int n, int b[])
{
for (int i = 1; i <= n; i++) {
if (b[i] != b[n - i + 1]) return false;
}
return true;
}
//检查a数组中删除数字x以后得到的新序列是否是回文序列
bool check(int x, int n)
{ //作为新数组tmp的下标
int index = 0;
for (int i = 1; i <= n; i++) {
//如果a中的元素不等于x,我就把a中的值拷贝到tmp中
//这里我使用的是++index,因为acm模式中习惯以1开头,所以我将0的位置舍弃不用了
if (a[i] != x) tmp[++index] = a[i];
}
//检查新序列是否是回文序列
return ishuiwen(index, tmp);
}
void solve() {
int n; cin >> n;
//每次进入测试样例后清空a数组,这是一个好习惯
memset(a, 0, sizeof(a)); memset(tmp, 0, sizeof(tmp));
for (int i = 1; i <= n; i++) cin >> a[i];
//如果原数组就是回文序列,直接输出YES,进入下一轮循环
if (ishuiwen(n, a)) {
cout << "YES" << endl;
return;
}
//双指针,检查对称序列有哪些位置的值不相同
int l = 1, r = n, x = 1, y = r;
while (l <= r) {
//如果检查到了不相同的值,直接记录下他们的值,然后退出查找
if (a[l] != a[r]) {
x = a[l], y = a[r];
break;
}
l++; r--;
}
//只需要删除一个位置的值后,变成了回文序列,那么a数组就是“可爱的”
if (check(x, n) || check(y, n)) {
cout << "YES" << endl;
}
else cout << "NO" << endl;
return;
}
int main()
{
int t; cin >> t;
while (t--) solve();
return 0;
}

双指针习题:Kalindrome Array的更多相关文章

  1. LeetCode题型分类及索引

    目录 这是一个对LeetCode题目归类的索引,分类标准参考了July大神的<编程之法>以及LeetCode的tag项.分类可能还不太合理,逐步完善,请见谅~ 题主本人也在一点一点的刷题, ...

  2. String Algorithm Summary - 1

    目录 Suffix Array Summay 单个字符串问题 两个字符串问题 多个字符串问题 AC-Automaton Summary 求长度为n(2e9)不包含给定字符串的合法串个数 包含至少一个词 ...

  3. Codeforces Global Round 17

    Codeforces Global Round 17 A. Anti Light's Cell Guessing 坑点:\(n=1,m=1\) 时答案为 \(0\) . 其他情况:当 \(n=1\) ...

  4. (双指针 二分) leetcode 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  6. CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】

    You are given an array d1,d2,-,dn consisting of n integer numbers. Your task is to split this array ...

  7. 80. Remove Duplicates from Sorted Array II(双指针)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  9. LeetCode167. Two Sum II - Input array is sorted(双指针)

    题意:对于一个有序数组,输出和为target的两个元素的下标.题目保证仅有唯一解. 分析: 法一:二分.枚举第一个元素,二分找另一个元素,时间复杂度O(nlogn),非最优解. class Solut ...

  10. array题目合集

    414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...

随机推荐

  1. 基于MybatisPlus的简单分页查询和条件分页查询

    分页查询 分析: 分析文档要求 查看前端传递给后台的参数 分析参数进行编码 后台返回给前端的数据 思路 浏览器 - > Controller层 - > Service层 - > Ma ...

  2. 2024年 智能机器人元年 —— 国内的智能机器人(humanoid)公司当下最大的压力(最为急迫的任务)是什么?

    可以说,2024年是人形机器人的元年.我国在去年年底将发展智能机器人立为了第一线的重要科技发展方向,并计划在2024年.2025年建立出完整的产业链条,并培育出几家成熟的行业领先的智能机器人公司.而我 ...

  3. 国产计算框架mindspore在gpu环境下编译分支r1.3,使用suod权限成功编译并安装,成功运行——(修复部分bug,给出具体编译和安装过程)—— 第二部分:源码编译及编译后文件安装、运行

    前文: 国产计算框架mindspore在gpu环境下编译分支r1.3,使用suod权限成功编译并安装,成功运行--(修复部分bug,给出具体编译和安装过程)-- 第一部分:依赖环境的安装 我们已经进行 ...

  4. mini_imagenet 数据集生成工具

    最近在看小样本方面的论文,发现这个mini_imagenet这个数据集比较常用,但是却不好找,找了半天也没有找到,最后在找到了这样的答案: 小样本学习(Few shot learning)标准数据集( ...

  5. Java核心编程-第一卷:基础知识

    public static void main(String[] args) { BigInteger bigInteger1 = BigInteger.probablePrime(20, new R ...

  6. VisionOn:新一代在线制图工具,简单易用又高颜值

    Vision On 一款集流程图.思维导图.白板于一体的轻量级在线图形工具 在工作和学习过程中,通过可视化的图形,有助于清晰高效地表达我们的灵感.想法.思想. 工欲善其事,必先利其器. 目前,思维导图 ...

  7. RISC-V全志D1多媒体套件文章汇总

    提示 此开发板的任何问题都可以在我们的论坛交流讨论 https://forums.100ask.net/c/aw/d1/57 文章目录汇总 教程共计14章,下面是章节汇总: 第0章_RISC-V全志D ...

  8. idea启动项目发现端口被占用!!!导致启动不起来

    windows端口被占用 netstat -ano |findstr 端口号 任务管理器详细信息 PID排序找到刚才查到的 右键结束 原因: idea被异常终止导致tomcat没死

  9. 好多kafka难题啊,看看其中的化解之道

    文末有面经共享群 前面已经分享过几篇面试了,这是一篇关于更加面向项目和技术的面经详解,第一次遇见问那么多kafka的问题,看看这个粉丝是怎么回答的. 先来看看 职位描述: 岗位职责: 负责基于 Go ...

  10. MyBatis分页实现

    目录 分页实现 limit实现分页 RowBounds分页 分页实现 limit实现分页 为什么需要分页? 在学习mybatis等持久层框架的时候,会经常对数据进行增删改查操作,使用最多的是对数据库进 ...