Codeforces B. Peculiar Movie Preferences

题目
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");

        }
    
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值