Strings in the Pocket(2019浙江省省赛)(马拉车-Manacher)

2024-01-17 13:50

本文主要是介绍Strings in the Pocket(2019浙江省省赛)(马拉车-Manacher),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Strings in the Pocket(2019浙江省省赛)(马拉车-Manacher)

Time limit:1000 ms
Memory limit:65536 kB
judge:
ZOJ
vjudge

Description

BaoBao has just found two strings s = s 1 s 2 … s n s = s_1s_2\dots s_n s=s1s2sn and t = t 1 t 2 … t n t = t_1t_2\dots t_n t=t1t2tn in his left pocket, where s i s_i si indicates the i i i-th character in string s s s, and t i t_i ti indicates the i i i-th character in string t t t.

As BaoBao is bored, he decides to select a substring of s s s and reverse it. Formally speaking, he can select two integers l l l and r r r such that 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn and change the string to s 1 s 2 … s l − 1 s r s r − 1 … s l + 1 s l s r + 1 … s n − 1 s n s_1s_2\dots s_{l-1}s_rs_{r-1} \dots s_{l+1}s_ls_{r+1}\dots s_{n-1}s_n s1s2sl1srsr1sl+1slsr+1sn1sn.

In how many ways can BaoBao change s s s to t t t using the above operation exactly once? Let ( a , b ) (a, b) (a,b) be an operation which reverses the substring s a s a + 1 … s b s_as_{a+1} \dots s_b sasa+1sb, and ( c , d ) (c, d) (c,d) be an operation which reverses the substring s c s c + 1 … s d s_cs_{c+1} \dots s_d scsc+1sd. These two operations are considered different, if a ≠ c a \ne c a=c or b ≠ d b \ne d b=d.

Input

There are multiple test cases. The first line of the input contains an integer T T T, indicating the number of test cases. For each test case:

The first line contains a string s s s ( 1 ≤ ∣ s ∣ ≤ 2 × 1 0 6 1 \le |s| \le 2 \times 10^6 1s2×106), while the second line contains another string t t t ( ∣ t ∣ = ∣ s ∣ |t| = |s| t=s). Both strings are composed of lower-cased English letters.

It’s guaranteed that the sum of ∣ s ∣ |s| s of all test cases will not exceed 2 × 1 0 7 2 \times 10^7 2×107.

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

2
abcbcdcbd
abcdcbcbd
abc
abc

Sample Output

3
3

Hint

For the first sample test case, BaoBao can do one of the following three operations: (2, 8), (3, 7) or (4, 6).

For the second sample test case, BaoBao can do one of the following three operations: (1, 1), (2, 2) or (3, 3).

题意

给你两个字符串 s s s t t t ,你可以选择 s s s 的一个区间,然后区间内的元素左右翻转。此操作只能执行一次。问你有多少种操作可以使 s s s 变为 t t t

题解

假设 s s s 可以变为 t t t (废话)

那么还要分类讨论一下

  1. s s s 不等于 t t t 时,此时 s s s t t t 一定有一段区间可以通过反转来变为 t t t 的对应区间。那么这一段区间就是一种答案。此外一次区间为基准向外扩展,因为如果此区间外左右两边的元素相同的话换与不换都不影响结果,所以包含他们就又是一种答案,以此类推,外面有几层相同的,答案就加几。
    在这里插入图片描述
  2. s s s 等于 t t t 时,我们可以找到一个回文子串,然后反转这个区间,效果就相当于没反转,那么 s s s t t t 还是相同的。所以问题就转化为了求 s s s 的回文子串的数目。
    由此想到了马拉车算法,因为p数组的意义就是以当前位置为中心的回文串的最大宽度。那么宽度就是回文串的半径,也就是可以组成的回文串的个数。但是由于马拉车算法往字符里塞了一些特殊字符,所以对p[i]/2求和即可。
    在这里插入图片描述

代码

#include <iostream>
#include <cstring>
#define maxn 4000005
using namespace std;int p[maxn];
int T;
char str[maxn / 2], ttr[maxn / 2];
char ss[maxn];long long manacher() {int len = 0;ss[len++] = '$';ss[len++] = '#';int n = strlen(str);for (int i = 0; i < n; i++) {ss[len++] = str[i];ss[len++] = '#';}int mx = 0, id = 0;for (int i = 1; i < len; i++) {if (mx > i) p[i] = (p[2 * id - i] < (mx - i) ? p[2 * id - i] : (mx - i));else p[i] = 1;while (i - p[i] >= 0 && i + p[i] < len && ss[i - p[i]] == ss[i + p[i]]) p[i]++;if (i + p[i] > mx) {mx = i + p[i];id = i;}}long long ans = 0;for (int i = 2; i < len - 1; ++i) {if (ss[i] == '#') ans += p[i] / 2;else ans += (p[i] + 1) / 2;}return ans;
}int getno() {int ans = 1, len = strlen(str);int l = 0, r = len - 1;while (l < r && str[l] == ttr[l]) ++l;while (r > l && str[r] == ttr[r]) --r;for (int i = l, j = r; i <= r; ++i, --j) if (str[i] != ttr[j]) return 0;for (int i = l - 1, j = r + 1, le = len; i >= 0 && j < le && str[i] == ttr[j]; --i, ++j) ++ans;return ans;
}int main() {while (cin >> T) {for (int i = 0; i < T; ++i) {scanf("%s%s", str, ttr);if (strcmp(str, ttr) == 0) cout << manacher() << "\n";else cout << getno() << "\n";}}return 0;
}

这篇关于Strings in the Pocket(2019浙江省省赛)(马拉车-Manacher)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/616254

相关文章

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

2019学习计划

工作三年了,第一年感觉是荒废的,第二年开始学习python,第三年开始自动化 感觉自己会的东西比较少,而且不够深入,流于表面 现制定一下今年大概的学习计划 需持续巩固加强:python、ui自动化、接口自动化、sql等 代码量需提升,敲的不够(重点) 学习: 1.移动端测试,appium等 2.前端知识系统整理学习  3.性能测试 4.docker入门,环境搭建 5.shell

找不同-第15届蓝桥省赛Scratch初级组真题第4题

[导读]:超平老师的《Scratch蓝桥杯真题解析100讲》已经全部完成,后续会不定期解读蓝桥杯真题,这是Scratch蓝桥杯真题解析第183讲。 如果想持续关注Scratch蓝桥真题解读,可以点击《Scratch蓝桥杯历年真题》并订阅合集,查阅教程更方便。 第15届蓝桥杯省赛已于2024年8月24日落下帷幕,编程题一共有5题,分别如下: 猪八戒落地 游乐场 画西瓜 找不同 消

最简单的使用JDBC[连接数据库] mysql 2019年3月18日

最极简版本的, 我们这里以mysql为例: 首先要创建maven工程, 需要引入jar包:,这里需要注意, 如果你安装的是mysql最新版本8以上的, 下面有些地方需要更改,具体就是mysql连接的url, 和5版本的不一样,具体解决请自行百度哈.这里只演示mysql5版本的? 依赖: <dependency>   <groupId>mysql</groupId>   <artifactId

[LeetCode] 583. Delete Operation for Two Strings

题:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in

第十五届蓝桥杯图形化省赛题目及解析

第十五届蓝桥杯图形化省赛题目及解析 一. 单选题 1. 运行以下程序,角色会说( )? A、29     B、31     C、33     D、35 正确答案:C 答案解析: 重复执行直到m>n不成立,即重复执行直到m<=n。所有当m小于或者 等于n时,循环结束。循环过程中变量m与变量n的变化如下表: 通过上述表格可知,循环到第五次循环结束。m的值为14,n的值为19

(php伪随机数生成)[GWCTF 2019]枯燥的抽奖

审核源码发现加载check.php,审计发现使用了mt_rand()函数,这个函数生成的值是伪随机的 参考下面这篇文章 PHP mt_rand安全杂谈及应用场景详解 - FreeBuf网络安全行业门户 kali里面输入下载工具 git clone https://github.com/openwall/php_mt_seed.git cd进去输入make后编译出的文件先

2019年2月17日

今天又重新看了一下输出第1500个丑数 在我错了八次之后发现要输出一个句号还要输出换行 接下来的两天应该进入复习阶段了。

National Contest for Private Universities (NCPU), 2019 E. Generalized Pascal's Triangle

编辑代码 2000ms 262144K Generalized Pascal's Triangle Pascal's triangle is a triangular array in which each number can be calculated by the sum of the two numbers directly above that number as shown i