生活随笔
收集整理的這篇文章主要介紹了
Acwing第 26 场周赛【完结】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 4076. 字符串權值【簽到】
- 4077. k顯性字符【思維 貪心 / 二分】
- 4078. 01串【DP】
4076. 字符串權值【簽到】
https://www.acwing.com/problem/content/4079/
#include<bits/stdc++.h>
using namespace std
;
int main(void)
{string s
; cin
>>s
;int sum
=0;for(int i
=0;i
<s
.size();i
++){if(s
[i
]=='A') sum
+=1;else if(s
[i
]=='1') sum
+=10;else sum
+=s
[i
]-'0';}cout
<<sum
;return 0;
}
4077. k顯性字符【思維 貪心 / 二分】
https://www.acwing.com/problem/content/4080/
二分做法:
#include<bits/stdc++.h>
using namespace std
;
set
<char>st
;
string s
;
bool check(int k
)
{for(auto i
=st
.begin();i
!=st
.end();i
++){int cnt
[30]={0};char temp
=*i
;bool flag
=0;for(int j
=0;j
<s
.size();j
++){if(j
>=k
){int l
=j
-k
;cnt
[s
[l
]-'a']--;}cnt
[s
[j
]-'a']++;if(j
>=k
-1&&!cnt
[temp
-'a']) flag
=1;}if(!flag
) return true;}return false;
}
int main(void)
{cin
>>s
;for(int i
=0;i
<s
.size();i
++) st
.insert(s
[i
]);int l
=1,r
=s
.size();while(l
<r
){int mid
=l
+r
>>1;if(check(mid
)) r
=mid
;else l
=mid
+1;}cout
<<l
<<endl
;return 0;
}
貪心,對于每一個字符求其最大的間距,注意開頭和結尾也得特別加。
最后在所有的結果中取一個min即可。
#include<bits/stdc++.h>
using namespace std
;
int last
[30],maxv
[30],n
;
string s
;
int main(void)
{cin
>>s
;s
="0"+s
;for(int i
=1;i
<s
.size();i
++){int t
=s
[i
]-'a';maxv
[t
]=max(maxv
[t
],i
-last
[t
]);last
[t
]=i
;}int n
=s
.size()-1;for(int i
=0;i
<26;i
++) maxv
[i
]=max(maxv
[i
],n
-last
[i
]+1);int ans
=n
;for(int i
=0;i
<26;i
++) {ans
=min(ans
,maxv
[i
]);}cout
<<ans
;return 0;
}
4078. 01串【DP】
https://www.acwing.com/problem/content/description/4081/
狀態表示: f[i] 長度為i 的01串的優秀字符串數量
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
const int N
=1e5+10;
const int mod
=1e9+7;
LL f
[N
],t
,k
;
int main(void)
{cin
>>t
>>k
;for(int i
=0;i
<N
;i
++){if(i
<k
) f
[i
]=1;else if(i
>=k
) f
[i
]=(f
[i
-1]+f
[i
-k
])%mod
;}for(int i
=1;i
<N
;i
++) f
[i
]=(f
[i
]+f
[i
-1])%mod
;while(t
--){int l
,r
; cin
>>l
>>r
;cout
<<(f
[r
]-f
[l
-1]+mod
)%mod
<<endl
;}return 0;
}
總結
以上是生活随笔為你收集整理的Acwing第 26 场周赛【完结】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。