日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【manacher】Strings in the Pocket

發布時間:2025/3/8 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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; }

總結

以上是生活随笔為你收集整理的【manacher】Strings in the Pocket的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 美女激情av| 国产在线精品福利 | 快播av在线 | 放荡的美妇在线播放 | 国产乱妇无码大片在线观看 | 久久国产91| 国产福利在线视频 | 日本国产网站 | 综合色影院| 日韩激情视频网站 | 精品视频999 | 爱爱激情网 | www.天堂av.com| 国产高清色 | 性喷潮久久久久久久久 | 色噜噜狠狠一区二区 | 男人的天堂中文字幕 | 亚洲欧美电影 | 女性裸体瑜伽无遮挡 | 四虎影院永久 | 午夜精品久久久久久久蜜桃 | 国产精品无码AV无码国产 | 欧美一区二区三区婷婷月色 | 97视频一区二区三区 | 操操色| 国产伦精品一区二区三区免费视频 | 免费av网站在线播放 | 熟妇人妻无乱码中文字幕真矢织江 | 人人妻人人澡人人爽精品 | 啪啪.com| 亚洲中文字幕第一区 | 成人免费xxxxx在线观看 | 亚洲色图第1页 | 丰满少妇一区二区三区视频 | 日韩毛片高清在线播放 | 色综合中文字幕 | 亚洲综合色吧 | 亚洲国产日韩欧美在线观看 | 日本污污网站 | 女优视频在线观看 | 久久99精品久久久久 | 久久久极品 | 欧美高大丰满少妇xxxx | 久久综合99| 亚洲社区在线观看 | 亚洲免费网站 | 色射视频 | a级一级黄色片 | 色综合天天综合网天天狠天天 | 欧美性大战久久久久xxx | 在线视频 一区二区 | 在线播放国产视频 | 台湾a级艳片潘金莲 | 潮见百合子 | 亚洲高清视频一区 | 国产亚洲一区在线 | 欧美人与动牲交a欧美精品 欧美三级在线看 | 天天爽天天插 | 在线精品亚洲欧美日韩国产 | 久久免费视频6 | 天天躁狠狠躁狠狠躁夜夜躁68 | 人妻偷人精品一区二区三区 | 和美女啪啪 | 日韩第八页 | 欧美一区视频在线 | 富二代成人短视频 | 人人插人人澡 | 日韩三级精品 | 爱涩av | 成人在线免费播放视频 | 四虎成人精品在永久免费 | 久久美女精品 | 亚洲一区和二区 | 日本高清视频在线播放 | 最色网站 | 一起草av在线 | 日本a一级片 | 久久国产视频一区 | 国产裸体永久免费无遮挡 | 久久精品视频1 | 91中文在线观看 | 神马久久久久久久久 | 国产毛片一区二区 | 免费20分钟超爽视频 | 高清视频一区二区三区 | 亚洲日本久久久 | 欧美成人精品一区二区免费看片 | 亚洲视频一区二区 | 久久精品久久久久 | 亚洲免费av电影 | 日韩乱淫| 黄色av网站免费看 | 古装做爰无遮挡三级视频 | 欧美一区二区三区在线 | 中文字幕亚洲精品在线观看 | 欧美一级专区免费大片 | 免费在线黄色网址 | 国产黑丝一区二区 | bbbbbxxxxx性欧美 |