双指针习题:Kalindrome Array

打印 上一主题 下一主题

主题 914|帖子 914|积分 2742

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
  1. 4
  2. 1
  3. 1
  4. 2
  5. 1 2
  6. 3
  7. 1 2 3
  8. 5
  9. 1 4 4 1 4
复制代码
样例输出 #1
  1. YES
  2. YES
  3. NO
  4. 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] $ .
思路讲解:

我们首先有一个特例:当原序列是回文序列时,不需要删除任何数字,它本身就是”可爱的“。
这时,我们只需要写出判断一个字符串序列是否是回文序列即可。
我们采用双指针法对数组进行回文序列判断
[code]bool ishuiwen(int n, int b[]){    //若i和n-i+1对应数值差别,肯定不是回文序列        for (int i = 1; i  a;        //假如原数组就是回文序列,直接输出YES,进入下一轮循环        if (ishuiwen(n, a)) {                cout
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

反转基因福娃

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表