补题

 
 
 
 
 
 
 
 
 
 
 

 

B.Reverse Binary Strings

题目:给定一个字符串,字符串由0和1构成,一半0一半1,可对字符串进行的操作是,任意相邻的子字符串进行翻转,然后使其变成010101……或者101010……,最少需要进行几次翻转

思路:思维题,不要考虑01问题,其实就是让其字符串没有连续重复,前面几个连续的,后面也有几个连续的字串,因为数量一定,计算全字符串0重复的次数和全字符串中1重复的次数,这就是两种不同的翻转次数

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 int main(){
 8     int t;
 9     scanf("%d",&t);
10     while(t--){
11         int len;
12         char s[100010];
13         scanf("%d",&len);
14         getchar();
15         scanf("%s",&s);
16         int sum1=0,sum2=0;
17         for(int i=0;i<len-1;i++){
18             if(s[i]==s[i+1]){
19                 if(s[i]=='0'){
20                     sum1++;
21                 }else{
22                     sum2++;
23                 }
24             }
25         }
26         printf("%d\n",max(sum1,sum2));
27     }
28 }
View Code

 2.完全背包问题

思路:熟悉用一维的数组进行查找最大值

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<string>
 6 using namespace std;
 7 const int maxx=1e4+5;
 8 int w[maxx],v[maxx],f[maxx];
 9 int main(){
10     int n,m;
11     scanf("%d %d",&n,&m);
12     for(int i=1;i<=n;i++){
13         scanf("%d %d",&w[i],&v[i]);
14     }
15     for(int i=1;i<=m;i++){
16         for(int j=1;j<=n;j++){
17             if(i<w[j]){
18                 continue;//f[i]=f[i-1];
19             }else{
20                 f[i]=max(f[i-w[j]]+v[j],f[i]);//
21             }
22         }
23     }
24     printf("%d",f[m]);
25 }
View Code

 

 
 
 
 
 
 
 
 
 
posted @ 2020-11-01 19:39  bonel  阅读(107)  评论(0)    收藏  举报