A.小红的Baidu
思路:字符串大小为 5 5 5,并且 " B a i d u " "Baidu" "Baidu" 每个字符都出现过
void solve()
{
string s; cin >> s;
if (s.find('B') != -1 && s.find('a') != -1 && s.find('i') != -1 && s.find('d') != -1 && s.find('u') != -1 && s.size() == 5) {
puts("Yes");
}
else puts("No");
}
B.小红盖章
思路:模拟实现即可,注意边界。
char str[550][550];
void solve()
{
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j ++){
str[i][j] = '.';
}
}
while (q--) {
int x, y;
char c[2];
cin >> x >> y >> *c;
str[x][y] = c[0];
for (int i = x; i >= x - 2 && i >= 0; i--) str[i][y] = c[0];
for (int i = x; i <= x + 2 && i >= 0; i++) str[i][y] = c[0];
for (int i = y; i >= y - 2 && i >= 0; i--) str[x][i] = c[0];
for (int i = y; i <= y + 2 && i >= 0; i++) str[x][i] = c[0];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << str[i][j];
}
cout << endl;
}
}
C.小红的数组修改
思路:将所有 a a a 累加后求出总和,对每一个数减掉自身对总和的贡献取余,求能让这个总和取余后变为 0 0 0 的方案数即可。
void solve()
{
int n, p, x;
cin >> n >> p >> x;
ll cnt = 0,sum = 0;
vector<int>a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
for (int i = 0; i < n; i++) {
ll ans = (sum - a[i]) % x;
cnt += (ans + p) / x;//总共能有多少种方案
if (p >= a[i] && sum % x == 0) cnt--;//能被整除的话原先数是一个方案,因此多了一次。
}
cout << cnt << endl;
}
D.括号匹配问题
思路:求连续子串的权值之和。遍历这个字符串,每一个连续子串他的贡献会增加到后面的子串上,因此顺序遍历即可。观察发现,每一个子串的权值之和为 S i + 1 = s i + 2 ∗ s t a c k . t o p ( ) S_{i + 1} = s_i + 2 * stack.top() Si+1=si+2∗stack.top(),stack放入遍历所得的 ′ ( ′ '(' ′(′ 下标。
void solve()
{
string s; cin >> s;
stack<int> S;
ll res = 0, ans = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
S.push(i + 1);
}
else {
if (!S.empty()) {
int v = S.top();
ans += 2 * v;
S.pop();
}
}
res += ans;
//cout << res << ' ' << ans <<endl;
}
cout << res << endl;
}