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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

BaoBao has just found two strings??and??in his left pocket, where??indicates the?-th character in string?, and??indicates the?-th character in string?.

As BaoBao is bored, he decides to select a substring of??and reverse it. Formally speaking, he can select two integers??and??such that??and change the string to?.

In how many ways can BaoBao change??to??using the above operation exactly once? Let??be an operation which reverses the substring?, and??be an operation which reverses the substring?. These two operations are considered different, if??or?.

Input

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

The first line contains a string??(), while the second line contains another string??(). Both strings are composed of lower-cased English letters.

It's guaranteed that the sum of??of all test cases will not exceed?.

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).

題目大意:

給你兩個串s1和s2,可以翻轉s1串的一個區間,只能翻轉一次,,問有多少對l,r使得翻轉后的s1串等于s2串

解題報告:

? 當兩串完全相同的時候就是馬拉車,不同的時候就先兩邊往里掃到兩串第一個不相同的位置,在第一個串往外擴,看能擴幾次,答案就是多少。

注意無數細節,,,那個初始化(見注釋)還有那個while(1)里面必須是if(l>=ed)就可以break了一開始寫成了l>ed,,,WA了一上午難受難受。。。

其實不是細節多,,而是代碼姿勢不太對,,,還是太菜,,不過這么簡單的一題比賽的時候沒有開有點小虧。做網絡同步賽打了三小時比賽水了7題,就去準備晚上給學弟講課的PPT去了。。。這成績對比了下,好像7題放現場賽也只是個銅(不過罰時較少可能是銀?)。。不過這題是真不難。。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 4e6 + 5; char s1[MAX],s2[MAX],str[MAX<<1]; int p[MAX<<1],top; int manacher() {if(top == 0) return 0 ;int maxx = -1;int c = -1,r = -1;for(int i = 1; i<top; i++) {p[i] = r>i ? min(p[2*c-i],r-i) : 1;for(;str[i-p[i]] == str[i+p[i]];p[i]++);if(i+p[i] > r) {r = i+p[i];c=i;}maxx = max(maxx,p[i]);}return maxx-1; }int main() {int t;cin>>t;while(t--) {scanf("%s%s",s1,s2);int len = strlen(s1);if(strcmp(s1,s2) != 0) {int st=-1,ed=len,l,r;//需要賦初值!!! int flag = 1;for(int i = 0; i<len; i++) {if(s1[i] == s2[i]) st = i;else break;}for(int i = len-1; i>=0; i--) {if(s1[i] == s2[i]) ed = i;else break;}l=st+1,r=ed-1;while(1) {if(l >= ed) break;if(s1[l] == s2[r]) l++,r--;else {flag = 0;break; }}if(flag == 0) {puts("0");continue; }l=st,r=ed; while(1) {if(l==-1||r==len) break;if(s1[l] == s1[r]) l--,r++;else break; }printf("%d\n",st-l+1); continue;}top=0;str[top++] = '@';for(int i = 0; i<len; i++) {str[top++] = '#';str[top++] = s1[i];}str[top++] = '#';str[top] = '\0';manacher();ll ans = 0;for(int i = 2; i<top; i++) { ans += p[i]/2; }printf("%lld\n",ans);}return 0 ; } //@#a#a#a# //@#a#a#

?

總結

以上是生活随笔為你收集整理的【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

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