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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Subsequence Hate CodeForces - 1363B(前缀和+dp)

發布時間:2023/12/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Subsequence Hate CodeForces - 1363B(前缀和+dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Shubham has a binary string s. A binary string is a string containing only characters “0” and “1”.

He can perform the following operation on the string any amount of times:

Select an index of the string, and flip the character at that index. This means, if the character was “0”, it becomes “1”, and vice versa.
A string is called good if it does not contain “010” or “101” as a subsequence — for instance, “1001” contains “101” as a subsequence, hence it is not a good string, while “1000” doesn’t contain neither “010” nor “101” as subsequences, so it is a good string.

What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.

Input
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases.

Each of the next t lines contains a binary string s (1≤|s|≤1000).

Output
For every string, output the minimum number of operations required to make it good.

Example
Input
7
001
100
101
010
0
1
001100
Output
0
0
1
1
0
0
2
Note
In test cases 1, 2, 5, 6 no operations are required since they are already good strings.

For the 3rd test case: “001” can be achieved by flipping the first character — and is one of the possible ways to get a good string.

For the 4th test case: “000” can be achieved by flipping the second character — and is one of the possible ways to get a good string.

For the 7th test case: “000000” can be achieved by flipping the third and fourth characters — and is one of the possible ways to get a good string.
思路:符合題目要求的字符串類型是:“1111110000000…”,“1111111111111111…”,“0000000001111111111111…”,"000000000000"這四種情況,但是總結起來就兩種:
①x個0+y個1.
②x個1+y個0.
我們求一下1的個數的前綴和,然后從頭開始遍歷,討論就可以了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e3+100; int sum[maxx]; string s;int main() {int t;scanf("%d",&t);while(t--){cin>>s;int n=s.length();int cnt=0;int ans=1e9;memset(sum,0,sizeof(sum));sum[0]=(s[0]=='1');for(int i=1;i<n;i++) sum[i]=sum[i-1]+(s[i]=='1');for(int i=0;i<n;i++) ans=min(ans,i+1-sum[i]+((sum[n-1]-sum[i])));//前i個都是0for(int i=0;i<n;i++) ans=min(ans,sum[i]+(n-i-1-(sum[n-1]-sum[i])));//前i個都是1cout<<ans<<endl;}return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Subsequence Hate CodeForces - 1363B(前缀和+dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

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