Prefix and Suffix Array 题解

该文讨论了一种算法问题,涉及处理字符串。给定一个回文串和一系列长度为n-1的子串,需要检查是否存在一个子串在反转后与另一个子串相等。通过寻找缺失第一个字符和最后一个字符的子串,可以判断是否满足条件。提供的C++代码示例中,通过比较这两个特殊子串来得出答案。
摘要由CSDN通过智能技术生成

原文题目

谷歌翻译

样例输入

  • 5

  • 4

  • bcd cd a d abc ab

  • 3

  • i io i oi

  • 2

  • g g

  • 3

  • t al lt a

  • 4

  • bba a ab a abb ba

样例输出

  • NO

  • YES

  • YES

  • NO

  • YES

题解 :对于每个样例输入为2*n-2,即n!-2,即一定会有只没有第一个字符,和只没有最后一个字符的字符串,so,找到这两个字符串,(完整字符串abcdcba ,A字符串 abcdcb 与B字符串 bcdcda )由于完整字符串是回文串,则 a[0]=a[n-1],so两个字符串 在其中一个反转后应该是相等的;

献上代码

  1. #include <bits/stdc++.h>

  1. using namespace std;

  1. int main() {

  1. ios::sync_with_stdio(0);

  1. cin.tie(0);

  1. cout.tie(0);

  1. int t; cin >> t;

  1. for (int test_number = 0; test_number < t; test_number++) {

  1. int n; cin >> n;

  1. vector <string> long_subs;

  1. for (int i = 0; i < 2 * n - 2; i++) {

  1. string s;

  1. cin >> s;

  1. if ((int)s.size() == n - 1) {

  1. long_subs.push_back(s);

  1. }

  1. }

  1. reverse(long_subs[1].begin(), long_subs[1].end());

  1. if (long_subs[0] == long_subs[1]) {

  1. cout << "YES\n";

  1. }

  1. else {

  1. cout << "NO\n";

  1. }

  1. }

  1. return 0;

  1. }

语法知识补充

  1. ios::sync_with_stdio(0);

  1. cin.tie(0);

  1. cout.tie(0);

  1. 用于加快 cin与cout的速度 但是 使用之后 不能在使用printf 与scanf ;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值