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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【2019牛客暑期多校训练营(第三场)- B】Crazy Binary String(思维,01串,前缀和)

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2019牛客暑期多校训练营(第三场)- B】Crazy Binary String(思维,01串,前缀和) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

鏈接:https://ac.nowcoder.com/acm/contest/883/B
來源:牛客網
?

ZYB loves binary strings (strings that only contains `0' and `1'). And he loves equal?binary?strings\textit{equal binary strings}equal?binary?strings more, where the number of `0' and the number of `1' in the string are equal.

ZYB wants to choose a substring from an original string ?T\ T?T?so that it is an equal?binary?string\textit{equal binary string}equal?binary?string with the longest length possible. He also wants to choose a subsequence of ?T\ T?T which meets the same requirements.

A string ?v\ v?v is a substring of a string ?w\ w?w if ?v\ v?v is empty, or there are two integers ?l\ l?l and r?(1≤l≤r≤∣w∣)r \ (1 \le l \le r \le |w|)r?(1≤l≤r≤∣w∣)?such that v=wlwl+1?wrv=w_lw_{l+1}\cdots w_rv=wl?wl+1??wr?. A string ?v\ v?v is a subsequence of a string ?w\ w?w? if it can be derived from ?w\ w?w? by deleting any number (including zero) of characters without changing the order of the remaining characters.?

For simplicity, you only need to output the maximum possible length. Note that the empty string is both a substring and a subsequence of any string.

輸入描述:

The first line of the input contains a single integer N?(1≤N≤100000)N \ (1 \le N \leq 100000)N?(1≤N≤100000), the length of the original string ?T\ T?T. The second line contains a binary string with exactly ?N\ N?N?characters, the original string ?T\ T?T.

輸出描述:

Print two integers ?A\ A?A and ?B\ B?B, denoting the answer for substring and subsequence respectively.

示例1

輸入

復制

8 01001001

輸出

復制

4 6

題目大意:

給你一個01字符串,讓你求01個數相等的子串和子序列長度。

解題報告:

? ?對于子序列肯定好說,直接就是0和1取個最大值再乘以2就是答案。

? ?對于子串,正常想法是對于偶數長度進行二分,但是其實是沒有單調性的,比如這個樣例:00111100,長度為6是不成立的,長度為8是成立的。所以正解是用前綴和,記錄第一個出現的位置即可。注意初始化0的初始位置是0。因為他可以從頭開始取。注意取長度的時候不需要再+1了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; const int ZERO = 1e5; int sum[MAX]; int pos[MAX]; int num[2],n; char s[MAX]; int main() {memset(pos,-1,sizeof pos);cin>>n;scanf("%s",s+1);for(int i = 1; i<=n; i++) s[i] -= '0',num[s[i]]++,sum[i] = sum[i-1] + (s[i] == 0 ? -1 : 1);int ans2 = min(num[0],num[1])*2;int ans1 = 0;pos[ZERO] = 0;for(int i = 1; i<=n; i++) {if(pos[ZERO+sum[i]] != -1) ans1 = max(ans1,i - pos[ZERO + sum[i]]);else pos[ZERO+sum[i]] = i;}printf("%d %d\n",ans1,ans2);return 0 ; }

?

總結

以上是生活随笔為你收集整理的【2019牛客暑期多校训练营(第三场)- B】Crazy Binary String(思维,01串,前缀和)的全部內容,希望文章能夠幫你解決所遇到的問題。

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