题目

B. Peculiar Movie Preferences

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic.

You are given a list s<?XML:NAMESPACE PREFIX = "[default] http://www.w3.org/1998/Math/MathML" NS = "http://www.w3.org/1998/Math/MathML" />s of nn non-empty strings of length at most 33, representing the scenes of Mihai's movie.

A subsequence of ss is called awesome if it is non-empty and the concatenation of the strings in the subsequence, in order, is a palindrome.

Can you help Mihai check if there is at least one awesome subsequence of ss?

A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.

A sequence aa is a non-empty subsequence of a non-empty sequence bb if aa can be obtained from bb by deletion of several (possibly zero, but not all) elements.

Input

The first line of the input contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the number of scenes in the movie.

Then follows nn lines, the ii-th of which containing a single non-empty string sisi of length at most 33, consisting of lowercase Latin letters.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case, print "YES" if there is an awesome subsequence of ss, or "NO" otherwise (case insensitive).

Example

input

Copy

6 5 zx ab cc zx ba 2 ab bad 4 co def orc es 3 a b c 3 ab cd cba 2 ab ab

output

Copy

YES NO NO YES YES NO

Note

In the first test case, an awesome subsequence of ss is [ab,cc,ba][ab,cc,ba]

题目分析

这道题的题意是在给出的字符串中,按照顺序选择相应的字符串,使之形成回文字符串,根据题意,输入的一组字符串中,只要有一个是回文字符串,就可以输出‘Yes’,如果所有的字符串本身都不是回文字符串,那么就在输入的全部字符串中搜寻是否可以组合字符串形成回文串,就像例子中的ab,cd,cba,单个都不是回文串,但是可以组合成回文串abcdcba。

首先,对输入的字符串进行处理,将字符串的每一个字母都转化为数字存储在一个二维数组的行中,将每一个数组的长度存储在每一行的第四列(字符串长度最长为3)。

如果这些输入的字符串中没有子串,则进入下一个环节,双重循环,每一个字符串的头和其它的字符串的尾相比较,如果相同,则依次比较每一个字符是否相同,如果满足回文,则跳出循环,为了防止在字符串相同的情况下反复循环,在进行内层循环前判断一下头尾和否和上一个已经判断过并且不能形成回文串的字符串相同,如果相同,则不必要再进行一次内层循环,可以直接跳转到外层循环。

代码实现:

#include <stdio.h>
#include <string.h>
int main(){
int n;
scanf("%d",&n);
while(n--){
int m;
scanf("%d",&m);
int a[m+5][7],flag=0,same=1;
for(int u=0;u<m;u++){
char str[5];
scanf("%s",str);
int l=strlen(str);
a[u][4]=l; for(int j=0;j<l;j++){
a[u][j]=str[j]-'a'+1;
// a[u][5]=a[u][5]+str[i]-'a'+1;
} a[u][l]=-1;
if(l==1) flag=1;
if(l==2&&a[u][0]==a[u][1])flag=1;
if(l==3&&a[u][0]==a[u][2]) flag=1; if(flag==0){
int v,q;
for(v=0,q=l-1;v<q;v++,q--){
if(a[u][v]!=a[u][q]){
break;
}
}
if(v>=q){
flag=1;
}
}
} if(flag==0){
for(int i=0;i<m-1;i++){
if(flag==1) break;
int y1=a[i][4]-1;
if(i>=1&&a[i][0]==a[i-1][0]&&a[i][4]==a[i-1][4]&&a[i][y1]==a[i-1][y1]) continue;
for(int j=i+1;j<m;j++){
int lt=a[j][4]-1;
if(a[i][0]==a[j][lt]){
int s1=0,s2=lt;
while(a[i][s1]==a[j][s2]&&a[i][s1]!=-1&&s2>=0){
s1++;s2--;
}
if(a[i][s1]==-1||s2<0){
flag=1;
break;
}
}
}
if(flag==1) break;
}
}
if(flag==0)
printf("NO\n");
if(flag==1)
printf("YES\n"); } }

每日一题:codeforces题解的更多相关文章

  1. 【剑指Offer】简单部分每日五题 - Day 1

    今天开始更新leetcode上<剑指Offer>的题解,先从简单难度开始.预计按下列顺序更新: 简单难度:每日5题 中等难度:每日3题 困难难度:每日1题 17 - 打印从1到最大的n位数 ...

  2. 【js】Leetcode每日一题-完成所有工作的最短时间

    [js]Leetcode每日一题-完成所有工作的最短时间 [题目描述] 给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间. 请你将这些工作分配给 k 位工人.所有工 ...

  3. 【JavaScript】Leetcode每日一题-最大整除子集

    [JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(an ...

  4. 【JavaScript】Leetcode每日一题-矩形区域不超过K的最大值和

    [JavaScript]Leetcode每日一题-矩形区域不超过K的最大值和 [题目描述] 给你一个 m x n 的矩阵 matrix 和一个整数 k ,找出并返回矩阵内部矩形区域的不超过 k 的最大 ...

  5. 【JavaScript】【KMP】Leetcode每日一题-实现strStr()

    [JavaScript]Leetcode每日一题-实现strStr() [题目描述] 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字 ...

  6. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

  7. 【python】Leetcode每日一题-扰乱字符串

    [python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...

  8. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  9. 【python】Leetcode每日一题-二叉搜索树节点最小距离

    [python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...

  10. 【python】Leetcode每日一题-最大数

    [python]Leetcode每日一题-最大数 [题目描述] 给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数. 注意:输出结果可能非常大,所以你需要返回一个 ...

随机推荐

  1. Zookeeper绍二(分布式锁介)

    一.为什么会有分布式锁? 在多线程环境下,由于上下文的切换,数据可能出现不一致的情况或者数据被污染,我们需要保证数据安全,所以想到了加锁. 所谓的加锁机制呢,就是当一个线程访问该类的某个数据时,进行保 ...

  2. Flowable实战(六)集成JPA

      上文提到,Flowable所有的表单数据都保存在一张表(act_hi_varinst)中,随着时间的推移,表中数据越来越多,再加上数据没有结构优化,查询使用效率会越来越低.   在Flowable ...

  3. vscode设置vue结构的初始代码片段

    { "Print to console": { "prefix": "vue", "body": [ "< ...

  4. Web开发之response

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. 我们要获取客户机提交过来的数据,只需要找request对象就行 ...

  5. RISC-V CPU加电执行流程

    市面上采用RISC-V架构的CPU很多,且没有如X86那样高度细节的标准,故采用说明文档详细的SiFive Freedom U540-C000芯片来做介绍(下面统一称为FU540). FU540支持多 ...

  6. Golang 基准测试Benchmark

    基准测试 Go语言标准库内置的 testing 测试框架提供了基准测试(benchmark)的能力,实现了对某个特定目标场景的某项性能指标进行定量的和可对比的测试. 基本规则 基准测试的代码文件必须以 ...

  7. 第56篇-ProfileData与DataLayout

    某些指令需要创建某些实例,如下: 指令 对应的DataLayout._struct._tag值 _checkcast._instanceof._aastore receiver_type_data_t ...

  8. 毫米转像素dpi

    public static double MillimeterToPixel_X(double length) //length是毫米,1厘米=10毫米 { System.Windows.Forms. ...

  9. 【转载】select case break引发的血案

    原文请看:select case break引发的血案 我也遇到了,浪费了一个多小时. 牢记: for { switch var1{ case "not match": go En ...

  10. vue学习17-插槽作用域

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta http ...