每日一题: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,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数. 注意:输出结果可能非常大,所以你需要返回一个 ...
随机推荐
- USB Tethering always displays grey when USB tethering type is Linux(EEM)
USB Tethering always displays grey when USB tethering type is Linux(EEM) 1.Problem DESCRIPTION USB T ...
- 【Java】静态与非静态
文章目录 静态与非静态 static关键字 使用static修饰属性:静态变量(或类变量) 类变量与实例变量的内存解析 使用static修饰方法:静态方法 使用static的注意点 开发中,如何确定一 ...
- Teamcenter无法创建多余账号怎么办?
西门子的产品Teamcenter,用户账号的许可是命名的许可类型,数量是限定的:例如,账号许可购买了25个,那么活动账号已经达到25了,再创建第26个账号将无法创建.没办法创建多余的账号,怎么办? 当 ...
- IO_FILE——FSOP、house of orange
FSOP 是 File Stream Oriented Programming 的缩写.所有的 _IO_FILE 结构会由 _chain 字段连接形成一个链表,由 _IO_list_all 来维护. ...
- 集合框架-ListIterator接口
1 package cn.itcast.p4.list.demo; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 imp ...
- ansible roles实践——服务器初始化
1.服务器初始化可以做哪些工作 关闭selinux ntp同步时间 修改dns为自建dns 配置ssh互信 修改yum源 设置主机名 内核参数优化 安装jdk 2.roles编写
- ElasticSearch+Kibana+Packetbeat
一.介绍 Packetbeat 是一个实时网络数据包分析工具,通过嗅探应用服务器之间的网络通讯,来解码应用层协议类型如HTTP.MySQL.redis等等,关联请求与响应,并记录每个事务有意义的字段. ...
- 论文解读(DFCN)《Deep Fusion Clustering Network》
Paper information Titile:Deep Fusion Clustering Network Authors:Wenxuan Tu, Sihang Zhou, Xinwang Liu ...
- Android开发-记账本布局-实现记账页面功能选择
记账页面需要软件盘的弹出,时间的显示和记账类型的选择.为了实现选择的效果,点击图片图片发生变化. 需要制作记账类型数据库,并设置单机事件,单机图片,图片变色,代表选中. 至于软键盘的制作,我直接拿来用 ...
- 人口信息普查系统-JavaWeb-二
上次发表了人口普查系统的题目要求,今天和大家分享一下我的技术方案. 技术上用到的是html+jsp+JavaBean+servlet+JavaScript 其实现在的前端页面主流还是html,它可以实 ...