A. Prefix and Suffix Array

题目

Marcos loves strings a lot, so he has a favorite string s consisting of lowercase English letters. For this string, he wrote down all its non-empty prefixes and suffixes (except for s) on a piece of paper in arbitrary order. You see all these strings and wonder if Marcos' favorite string is a palindrome or not. So, your task is to decide whether s is a palindrome by just looking at the piece of paper.

A string a is a prefix of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the end.

A string a is a suffix of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning.

A palindrome is a string that reads the same backward as forward, for example, strings "gg", "ioi", "abba", "icpci" are palindromes, but strings "codeforces", "abcd", "alt" are not.

Input

Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤1201≤≤120) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (2≤n≤202≤≤20) — the length of the string s.

The second line of each test case contains 2n−22−2 strings a1,a2,⋯,a2n−21,2,⋯,2−2 — all non-empty prefixes and suffixes of s, not including itself, in arbitrary order.

It is guaranteed that these strings are all the non-empty prefixes and suffixes of some string consisting of lowercase English letters.

Output

For each test case, output "YES" if s is a palindrome, and "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

原题

Problem - A - Codeforces

题目大意:

判断是否输入的是回文数。

思路

只需输入两个n-1的string,此时的string是去除第一个字母和最后一个字母的str

用reverse函数进行翻转,然后判断第一个字符即可

#include <bits/stdc++.h>

using namespace std;

void solve()
 {
	int n;
	cin >> n;

	vector<string> a;
	for (int i = 0; i < 2 * n - 2; i++)
    {
		string s;
		cin >> s;

		if (s.size() == n - 1)
			a.push_back(s);
	}
    
    reverse(a[1].begin(),a[1].end());
    if(a[1]==a[0])
    	cout<<"YES"<<endl;
    else
    	cout<<"NO"<<endl;
        
}

int main() {

	int t;
	cin >> t;

	while (t--) 
       {
		solve();
	}

	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值