每日一题:codeforces题解
题目
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题解的更多相关文章
- 【剑指Offer】简单部分每日五题 - Day 1
		
今天开始更新leetcode上<剑指Offer>的题解,先从简单难度开始.预计按下列顺序更新: 简单难度:每日5题 中等难度:每日3题 困难难度:每日1题 17 - 打印从1到最大的n位数 ...
 - 【js】Leetcode每日一题-完成所有工作的最短时间
		
[js]Leetcode每日一题-完成所有工作的最短时间 [题目描述] 给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间. 请你将这些工作分配给 k 位工人.所有工 ...
 - 【JavaScript】Leetcode每日一题-最大整除子集
		
[JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(an ...
 - 【JavaScript】Leetcode每日一题-矩形区域不超过K的最大值和
		
[JavaScript]Leetcode每日一题-矩形区域不超过K的最大值和 [题目描述] 给你一个 m x n 的矩阵 matrix 和一个整数 k ,找出并返回矩阵内部矩形区域的不超过 k 的最大 ...
 - 【JavaScript】【KMP】Leetcode每日一题-实现strStr()
		
[JavaScript]Leetcode每日一题-实现strStr() [题目描述] 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字 ...
 - [LeetCode每日一题]81. 搜索旋转排序数组 II
		
[LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...
 - 【python】Leetcode每日一题-扰乱字符串
		
[python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...
 - 【python】Leetcode每日一题-前缀树(Trie)
		
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
 - 【python】Leetcode每日一题-二叉搜索树节点最小距离
		
[python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...
 - 【python】Leetcode每日一题-最大数
		
[python]Leetcode每日一题-最大数 [题目描述] 给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数. 注意:输出结果可能非常大,所以你需要返回一个 ...
 
随机推荐
- [MAUI] 在.NET MAUI中结合Vue实现混合开发
			
 在MAUI微软的官方方案是使用Blazor开发,但是当前市场大多数的Web项目使用Vue,React等技术构建,如果我们没法绕过已经积累的技术,用Blazor重写整个项目并不现实. Vue是当前流 ...
 - 匿名函数托管 func-spring-boot-starter
			
匿名函数托管 func-spring-boot-starter 项目地址 func-spring-boot-starter开源项目地址: https://gitee.com/yiur/func-spr ...
 - [Altium Designer 学习]怎样输出Gerber文件和钻孔文件
			
为了资料保密和传输方便,交给PCB厂商打样的资料一般以Gerber和钻孔文件为主,换句话说,只要有前面说的两种文件,就能制作出你想要的PCB了. 一般来说,交给PCB厂商的Gerber有以下几层: G ...
 - 【刷题-LeetCode】152 Maximum Product Subarray
			
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
 - CMake语法—普通变量与子目录(Normal Variable And Subdirectory)
			
目录 CMake语法-普通变量与子目录(Normal Variable And Subdirectory) 1 CMake普通变量与子目录示例 1.1 代码目录结构 1.2 父目录CMakeLists ...
 - Dapr 和 Azure Functions : Hello world
			
本篇文章内容来自 https://charliedigital.com/2021/07/01/dapr-and-azure-functions-part-1-hello-world/ ,是按这篇文章的 ...
 - 写程序时try,catch查看报错的行号
			
try { //////////////// 代码段 //////////////// }catch(Exception ex) { MessageBox.Show(ex.St ...
 - ApacheCN C/C++ 译文集 20211201 更新
			
笨办法学C 中文版 前言 导言:C的笛卡尔之梦 练习0:准备 练习1:启用编译器 练习2:用Make来代替Python 练习3:格式化输出 练习4:Valgrind 介绍 练习5:一个C程序的结构 练 ...
 - js源码-数组中的push()和unshift()方法的源码实现
			
人话不多,直接上代码,在代码中解析,不足之处请谅解: push() Array.prototype._push=function(...value){//在Array原型链上添加_push方法 for ...
 - PyTorch 介绍 | AUTOMATIC DIFFERENTIATION WITH TORCH.AUTOGRAD
			
训练神经网络时,最常用的算法就是反向传播.在该算法中,参数(模型权重)会根据损失函数关于对应参数的梯度进行调整. 为了计算这些梯度,PyTorch内置了名为 torch.autograd 的微分引擎. ...