【manacher】Strings in the Pocket

Strings in the Pocket

Time Limit: 1 Second Memory Limit: 65536 KB
BaoBao has just found two strings s=s1s2…sns=s_1s_2\dots s_ns=s1?s2?sn? and t=t1t2…tnt=t_1t_2\dots t_nt=t1?t2?tn? in his left pocket, where sis_isi? indicates the iii-th character in string , and tit_iti? indicates the iii-th character in string ttt.

As BaoBao is bored, he decides to select a substring of and reverse it. Formally speaking, he can select two integers lll and rrr such 1≤l≤r≤n1 \le l \le r \le n1lrnthat and change the string to s1s2…sl?1srsr?1…sl+1slsr+1…sn?1sns_1s_2\dots s_{l-1}s_rs_{r-1}\dots s_{l+1}s_ls_{r+1}\dots s_{n-1}s_ns1?s2?sl?1?sr?sr?1?sl+1?sl?sr+1?sn?1?sn?.

In how many ways can BaoBao change to using the above operation exactly once? Let (a,b)(a,b)(a,b) be an operation which reverses the substring sasa+1…sbs_as_{a+1}\dots s_bsa?sa+1?sb?, and (c,d)(c,d)(c,d) be an operation which reverses the substring scsc+1…sds_cs_{c+1}\dots s_dsc?sc+1?sd?. These two operations are considered different, if a=?ca=\not ca=??c or b=?db=\not db=??d.

Input

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

The first line contains a string s(1≤∣s∣≤2×106)s(1\le |s| \le 2 \times 10^6)s(1s2×106), while the second line contains another string t(∣t∣=∣s∣)t(|t| = |s|)t(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×1072 \times 10^72×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).

題目大意:

先輸入一個整數ttt,代表共有ttt組測試樣例,對于每一組測試樣例,輸入兩個字符串s1,s2s_1,s_2s1?,s2?,現在僅能將字符串s1s_1s1?中的某個字串翻轉s1s_1s1?s2s_2s2?相等,問共有幾中反轉方法。

解題思路:

此題可以將題目分為兩種情況,一種是字符串s1s_1s1?與字符串s2s_2s2?完全相同,對于這種情況,可以通過manacher來計算字符串sis_isi?中每個回文串的長度,將長度求和即為總的方案數,對于s1s_1s1?s2s_2s2?不等的情況,可以先從左往右找到s1s_1s1?s2s_2s2?第一個不同的位置lll,同理,從右往左找到兩字符串第一次不同的位置rrr,先判斷s1s_1s1?的字串slsl+1…sr?1srs_ls_{l+1}\dots s_{r-1}s_rsl?sl+1?sr?1?sr?反轉后是否會使s1s_1s1?s2s_2s2?完全相同,不會的話證明無法在反轉一次的情況下使兩字符串相同,輸出000,否則則以此字串為邊界同時往兩邊擴展,判斷字串長度是否能延伸,記錄輸出即可。

代碼

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
string s1,s2,s1_new;
int p[6004000];
int get_newstring() {s1_new.clear();int len=s1.size();s1_new+='$';for(int i=0;i<len;i++) {s1_new+='#';s1_new+=s1[i];}s1_new+='#';return s1_new.size();
}
ll manacher() {int len=get_newstring();fill(p,p+len,0);int id=0,mx=0;ll ans=0;for(int i=0;i<len;i++) {if(i<mx) p[i]=min(p[2*id-i],mx-i);elsep[i]=1;while(s1_new[i-p[i]]==s1_new[i+p[i]]) {p[i]++;}if(p[i]+i>mx) {mx=i+p[i];id=i;}ans+=(ll)(p[i]/2);}return ans;
}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;cin>>t;while(t--) {cin>>s1>>s2;ll ans=0;if(s1==s2) {ans=manacher();}else {int l=0,r=s1.size()-1;while(s1[l]==s2[l]) l++;while(s1[r]==s2[r]) r--;bool ju=false;int t1=l,t2=r;while(s1[t1]==s2[t2]) {if(t1==r&&t2==l) break;t1++;t2--;}if(t2==l&&t1==r) {ju=true;}else ju=false;if(l==r) ju=false;if(ju==true) {ans=1;l--,r++;while(l>=0&&r<s1.size()&&s1[l]==s2[r]&&s1[r]==s2[l]) {ans++;l--;r++;}}else ans=0;}cout<<ans<<endl;}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536290.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536290.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536290.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【并查集+dp】Team

Team 時間限制: 1 Sec 內存限制: 128 MB 提交: 124 解決: 10 [提交] [狀態] [命題人:admin] 題目描述 ACM-ICPC is a interesting game. A team takes part in this game must consist of exactly (no more and no less) three players. Every year, many new members wil…

【線段樹】Segment Tree

Segment Tree 時間限制: 1 Sec 內存限制: 512 MB 提交: 107 解決: 23 [提交] [狀態] [命題人:admin] 題目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satis?ed that the maximum c can reach w…

UVA1025——A Spy in the Metro【dp】

題目鏈接&#xff1a;https://cn.vjudge.net/problem/UVA-1025 題目大意&#xff1a;Mario從第1站出發&#xff0c;目的是在時刻T會見車站 nnn 的一個間諜。由于在車站等待容易被抓&#xff0c;所以應盡量躲在開動的火車上&#xff0c;即在車站等待的時間最短&#xff0c;且Ma…

HDU1284——錢幣兌換問題【dp】

錢幣兌換問題 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14528 Accepted Submission(s): 8784 Problem Description 在一個國家僅有1分&#xff0c;2分&#xff0c;3分硬幣&#xff0c;將錢N兌換成硬…

HDU6092——Rikka with Subset 【dp】

題目網址&#xff1a; https://vjudge.net/problem/HDU-6092 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has nn positive A1?AnA1?An and their sum is mm…

洛谷P1725琪露諾【單調隊列+dp】

題目描述 在幻想鄉&#xff0c;琪露諾是以笨蛋聞名的冰之妖精。 某一天&#xff0c;琪露諾又在玩速凍青蛙&#xff0c;就是用冰把青蛙瞬間凍起來。但是這只青蛙比以往的要聰明許多&#xff0c;在琪露諾來之前就已經跑到了河的對岸。于是琪露諾決定到河岸去追青蛙。 小河可以看…

扶桑號戰列艦【RMQ+分治】

扶桑號戰列艦 時間限制: 1 Sec 內存限制: 128 MB Special Judge 提交: 197 解決: 63 [提交] [狀態] [命題人:admin] 題目描述 眾所周知&#xff0c;一戰過后&#xff0c;在世界列強建造超無畏級戰列艦的競爭之中&#xff0c;舊日本海軍根據“個艦優越主義”&#xff0c;建造了扶…

大鳳號裝甲空母【找規律+矩陣快速冪】

大鳳號裝甲空母 時間限制: 1 Sec 內存限制: 128 MB 提交: 108 解決: 15 [提交] [狀態] [命題人:admin] 題目描述 大鳳號航空母艦很喜歡算術。 它&#xff0c;是舊日本海軍中最為先進的航空母艦。 它&#xff0c;是舊日本海軍中最為短命的航空母艦。 同時&#xff0c;她還是最平…

codevs5429 多重背包【多重背包+單調隊列】

5429 多重背包 時間限制: 1 s 空間限制: 256000 KB 題目等級 : 鉆石 Diamond 題目描述 Description 你有一個容量為M的背包&#xff0c;和N種物品。 每種物品都有三個屬性&#xff0c;vi&#xff0c;wi&#xff0c;與ci&#xff0c;分別表示這種物品的體積、價值和件數。 你的…

generator 1【矩陣快速冪】

題目描述 You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0?,x1?,a,b. And you know xia?xi?1b?xi?2x_i a \cdot x_{i-1} b \cdot x_{i-2}xi?a?xi?1?b?xi?2? for all i≥2i \ge 2i≥2. Given two positive integers n, and MOD, please calcul…

Give Candies【快速冪+歐拉】

Give Candies 時間限制: 1 Sec 內存限制: 128 MB 提交: 243 解決: 92 [提交] [狀態] [命題人:admin] 題目描述 There are N children in kindergarten. Miss Li bought them N candies。To make the process more interesting, Miss Li comes up with the rule: All the childr…

Save the Room【找規律】

Save the Room 時間限制: 1 Sec 內存限制: 128 MB 提交: 149 解決: 90 [提交] [狀態] [命題人:admin] 題目描述 Bob is a sorcerer. He lives in a cuboid room which has a length of A, a width of B and a height of C, so we represent it as ABC. One day, he finds that …

Participate in E-sports【Java大數+二分】

Participate in E-sports 時間限制: 2 Sec 內存限制: 128 MB 提交: 194 解決: 53 [提交] [狀態] [命題人:admin] 題目描述 Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to…

Poor God Water【矩陣快速冪】

Poor God Water 時間限制: 1 Sec 內存限制: 128 MB 提交: 102 解決: 50 [提交] [狀態] [命題人:admin] 題目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonou…

Transport Ship【多重背包】

Transport Ship 時間限制: 1 Sec 內存限制: 128 MB 提交: 175 解決: 65 [提交] [狀態] [命題人:admin] 題目描述 There are N different kinds of transport ships on the port. The i^th kind of ship can carry the weight of V[i] and the number of the ith kind of ship i…

洛谷P2015 二叉蘋果樹【樹形dp】

P2015 二叉蘋果樹 時間限制 1.00s 內存限制 125.00MB 題目描述 有一棵蘋果樹&#xff0c;如果樹枝有分叉&#xff0c;一定是分2叉&#xff08;就是說沒有只有1個兒子的結點&#xff09; 這棵樹共有N個結點&#xff08;葉子點或者樹枝分叉點&#xff09;&#xff0c;編號為1-N,…

洛谷P2014【樹形dp】

P2014 選課 時間限制 1.00s 內存限制 125.00MB 題目描述 在大學里每個學生&#xff0c;為了達到一定的學分&#xff0c;必須從很多課程里選擇一些課程來學習&#xff0c;在課程里有些課程必須在某些課程之前學習&#xff0c;如高等數學總是在其它課程之前學習。現在有N門功課&…

洛谷P2016 戰略游戲【樹形dp】

P2016 戰略游戲 時間限制 1.00s 內存限制 125.00MB 題目描述 Bob喜歡玩電腦游戲&#xff0c;特別是戰略游戲。但是他經常無法找到快速玩過游戲的辦法。現在他有個問題。 他要建立一個古城堡&#xff0c;城堡中的路形成一棵樹。他要在這棵樹的結點上放置最少數目的士兵&#x…

Shell Pyramid【數學+二分】

Shell Pyramid 時間限制: 1 Sec 內存限制: 128 MB 提交: 291 解決: 95 [提交] [狀態] [命題人:admin] 題目描述 In the 17th century, with thunderous noise, dense smoke and blazing fire, battles on the sea were just the same as those in the modern times. But at tha…

Degree Sequence of Graph G【模擬】

Degree Sequence of Graph G 時間限制: 1 Sec 內存限制: 128 MB 提交: 362 解決: 92 [提交] [狀態] [命題人:admin] 題目描述 Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep lov…