字符串
1.小红的数字串
https://www.nowcoder.com/questionTerminal/d70a5f22b118407f85a88ed7f87ed45c?page=5&onlyReference=false
#include <iostream> using namespace std; int main() { string s; long long k; cin >> s; cin >> k; int n = 0; for (long long i = 0; i < s.size(); i++){ long long m = 0; for (long long j = i; j < s.size(); j++) { m = m * 10 + s[j] - '0'; if (m >= k) break; n++; } } cout << n <<endl; return 0; } // 64 位输出请用 printf("%lld")
2.游游的字母串
https://ac.nowcoder.com/acm/contest/66943/B
知识点:
1. #include <climits> 取极大数 int res = INT_MAX; LLONG_MAX
2. 取绝对值abs
#include <iostream> #include <cstdlib> // for abs(int) #include <algorithm> #include <climits> using namespace std; int main(){ string str; cin >> str; int res = INT_MAX; for (int i = 'a'; i <= 'z'; i++){ int ops = 0; for (int j = 0; j < str.size(); j++){ ops = ops + min(abs(str[j] - i), 26-abs(str[j] - i)); } res = min(res, ops); } cout << res << endl; }
3.小红的纸牌游戏
https://www.nowcoder.com/practice/a454e716209c467c924577295f6e0d8d?tpId=182&rp=1&sourceUrl=%2Fexam%2Foj&difficulty=2&judgeStatus=&tags=&title=&gioEnter=menu
知识点:
1.find函数 找到返回位置,找不到返回-1
2.%2 偶数是小红操作,奇数是小紫操作
#include<bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string str; cin >> str; for (int i = 0; i < n - k; i++) { if (i % 2 == 0) { int pos = str.find('0'); if (pos != -1 && pos != str.size() - 1) { str = str.substr(0, pos) + str.substr(pos + 1, str.size()); } else { str.pop_back(); } } else { int pos = str.find('1'); if (pos != -1 && pos != str.size() - 1) { str = str.substr(0, pos) + str.substr(pos + 1, str.size()); } else { str.pop_back(); } } } cout << str << endl; }
4.小红的循环移位
https://ac.nowcoder.com/acm/contest/80742/C
知识点:
stoi函数:字符串转整形

#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int n = s.size(); int ans = -1; for (int i = 0; i < n; ++i) { // 每次左移一位:把 s[0] 放到末尾 if (n >= 2) { string lastTwo = s.substr(n - 2, 2); int num = stoi(lastTwo); if (num % 4 == 0) { ans = i; break; } } else { // 特判长度为1的情况 int num = s[0] - '0'; if (num % 4 == 0) { ans = i; break; } } // 左移一次 s = s.substr(1) + s[0]; } cout << ans << endl; return 0; }
5.对称之美
https://ac.nowcoder.com/acm/contest/10746/H
#include <bits/stdc++.h> using namespace std; bool judge(vector<string>& strs){ int n = strs.size(); for(int i=0; i <= n/2; i++){ if(i == n-i-1){ break; } for(int j = 0; j < strs[i].size(); j++){ if(strs[n-i-1].find(strs[i][j]) != -1){ break; } else { if(j == strs[i].size() - 1){ return false; } } } } return true; } int main(){ int t; cin >> t; while (t--){ int n; cin >> n; vector<string> strs; while (n--){ string str; cin >> str; strs.push_back(str); } if (judge(strs)) { cout << "Yes" << endl; } else { cout << "No" << endl; } } }
6.小红的01串
https://www.nowcoder.com/questionTerminal/d371489291b8455ab5e7fb86a26ee5bc?f=discussion
要注意如果是01001 不能变成01101 只能变成00001
#include <iostream> using namespace std; int main() { string str; cin >> str; int n = 0; for (int i = 0; i < str.size() - 3; i++){ string substr = str.substr(i,3); if (substr == "010") { if (str.substr(i+2, 3) == "001") { str[i+1] = '0'; } else { str[i+2] = '1'; } n++; } if (substr == "101") { if (str.substr(i+2, 3) == "110"){ str[i+1] = '1'; } else { str[i+2] = '0'; } n++; } } cout << n << endl; } // 64 位输出请用 printf("%lld")
浙公网安备 33010602011771号