每日一题:codeforces题解
阅读原文时间:2023年07月11日阅读:2

题目

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 ss 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
#include
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");

    }

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器