1456.定長子串中母音的最大數目
題目連結:https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/
題解程式碼:
class Solution {
public:
int maxVowels(string s, int k) {
int ans=0,vowel=0;
for(int i=0;i<s.size();i++){
if(s[i]=='a'||s[i]=='e'||s[i]=='o'||s[i]=='i'||s[i]=='u') vowel++;
if(i<k-1) continue;
ans=ans>vowel?ans:vowel;
char r=s[i-k+1];
if (r == 'a' || r == 'e' || r == 'i' || r == 'o' || r == 'u') vowel--;
}
return ans;
}
};
D. Range = √Sum
題目連結:https://codeforces.com/contest/1758/problem/D
題解程式碼:
//數學不好抄了題解思路
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n % 2 == 0) {
vector<int> result;
for (int i = n / 2; i <= n / 2 + n; ++i) {
if (i != n) {
result.push_back(i);
}
}
for (int i = 0; i < result.size(); ++i) {
if (i > 0) cout << " ";
cout << result[i];
}
cout << endl;
} else {
vector<int> a(n);
for (int i = 0; i < n; ++i) {
a[i] = n / 2 + 3 + i;
}
a[0] -= 1;
a[n - 1] += 1;
a[n - 2] += 1;
for (int i = 0; i < n; ++i) {
if (i > 0) cout << " ";
cout << a[i];
}
cout << endl;
}
}
return 0;
}
C. Hossam and Trainees不會寫
D. Sakurako's Hobby
題目連結:https://codeforces.com/problemset/problem/2008/D
題解程式碼:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> p(n + 1);
vector<int> F(n + 1, 0);
vector<bool> visited(n + 1, false);
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
int l = i;
int count = 0;
while (!visited[l]) {
visited[l] = true;
if (s[l - 1] == '0') count++;
l = p[l];
}
l = i;
while (true) {
F[l] = count;
l = p[l];
if (l == i) break;
}
}
}
for (int i = 1; i <= n; i++) {
cout << F[i] << " ";
}
cout << endl;
}
return 0;
}
P1803 凌亂的yyy / 線段覆蓋
題目連結:https://www.luogu.com.cn/problem/P1803
題解程式碼:
#include<bits/stdc++.h>
using namespace std;
int n,sum=1;
struct E{
int a,b;
}s[1000005];
bool cmp(E x,E y){
return x.b<y.b;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>s[i].a>>s[i].b;
sort(s+1,s+n+1,cmp);
for(int i=2;i<=n;i++){
if(s[i].a>=s[i-1].b) sum++;
else{
s[i].b=s[i-1].b;
s[i].a=s[i-1].a;
}
}
cout<<sum;
return 0;
}
D. Slavic's Exam
題目連結:https://codeforces.com/contest/1999/problem/D
題解程式碼:
#include <bits/stdc++.h>
using namespace std;
int main() {
int te; cin >> te;
while(te--) {
string s, t; cin >> s >> t;
int x = 0;
for(int i = 0; i < (int)s.size(); ++i) {
if(s[i] == '?') {
if(x < (int)t.size()) s[i] = t[x++];
else s[i] = 'a';
} else if(s[i] == t[x]) ++x;
}
if(x >= t.size()) cout << "YES\n" << s << "\n";
else cout << "NO\n";
}
return 0;
}
P1106 刪數問題
題目連結:https://www.luogu.com.cn/problem/P1106
題解程式碼:
#include<bits/stdc++.h>
using namespace std;
int main(){
string n;
int s,i;
cin>>n>>s;
while(s){
i=0;
while(n[i]<=n[i+1]) i++;
n.erase(i,1);
s--;
}
while(n[0]=='0'&&n.size()>1) n.erase(0,1);
cout<<n;
return 0;
}
B. Palindromic Numbers
題目連結:https://codeforces.com/contest/1700/problem/B
題解程式碼:
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
if (s[0] != '9') {
for (char c : s) {
cout << 9 - (c - '0');
}
cout << endl;
return;
}
vector<int> ans(n);
int carry = 0;
for (int i = n - 1; i >= 0; i--) {
int dig = s[i] - '0';
if (carry == 0 && dig == 0) {
ans[i] = 1;
continue;
}
dig += carry;
if (dig == 1) {
ans[i] = 0;
carry = 0;
} else {
ans[i] = 11 - dig;
carry = 1;
}
}
for (int i : ans) {
cout << i;
}
cout << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}