A - Single Wildcard Pattern Matching
Question
You are given two strings s and t. The string s consists of lowercase Latin letters and at most one wildcard character ‘*’, the string t consists only of lowercase Latin letters. The length of the string s equals n, the length of the string t equals m.
The wildcard character ‘’ in the string s (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of s can be replaced with anything. If it is possible to replace a wildcard character '’ in s to obtain a string t, then the string t matches the pattern s.
For example, if s = “aba*aba” then the following strings match it “abaaba”, “abacaba” and “abazzzaba”, but the following strings do not match: “ababa”, “abcaaba”, “codeforces”, “aba1aba”, “aba?aba”.
If the given string t matches the given string s, print “YES”, otherwise print “NO”.
题目大意
给定两个字符串 s s s和 t t t。字符串 s s s由小写拉丁字母组成,并且最多包含一个通配符字符 ‘*’,字符串 t t t仅由小写拉丁字母组成。字符串 s s s的长度等于 n n n,字符串 t t t的长度等于 m m m。
字符串 s s s中的通配符字符 ‘*’(如果有的话)可以被任意序列(可能为空)的小写拉丁字母替换。
字符串 s s s中的其他字符不能被替换。如果可以替换字符串 s s s中的通配符字符 ‘*’ 以获得字符串 t t t,那么字符串t就匹配模式 s s s。
例如,如果 s s s =“aba*aba”,那么以下字符串与之匹配:“abaaba”、“abacaba” 和 “abazzzaba”,但以下字符串不匹配:“ababa”、“abcaaba”、“codeforces”、“aba1aba”、“aba?aba”。
如果给定字符串 t t t匹配给定字符串 s s s,则打印 “YES”,否则打印 “NO”。
Input
6 10
code*s
codeforces
Output
YES
分析
首先,如果不算上星号,字符串s的长度比字符串t的长度长,那么就一定不匹配(输出 “NO”)。
其次,在比较两个字符串是否匹配时,可以把字符串
s
s
s分成两部分比较:
- 星号前作为一部分,从头和字符串t比较.
- 星号后作为一部分,从尾和字符串t比较.
(细节详见代码)
AC Code
#include <bits/stdc++.h>
using namespace std;
int n, m, id;
string s, t;
bool check1() {
for(int i = 0; i < id; i++) //星号前作为一部分,从头和字符串t比较
if(s[i] != t[i])
return false;
for(int i = n - 1, j = m - 1; i > id; i--, j--)
//星号后作为一部分,从尾和字符串t比较
if(s[i] != t[j])
return false;
return true;
}
bool check2() {
for(int i = 0; i < n; i++)
if(s[i] != t[i])
return false;
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
cin >> s >> t;
if(n - 1 > m) {puts("NO"); return 0;}
bool flg = 0, ans;
for(int i = 0; i < n; i++)
if(s[i] == '*')
id = i, flg = 1;
if(!flg) { //如果字符串s中没有星号
if(n != m) {puts("NO"); return 0;}
ans = check2();
}
else ans = check1();
if(ans) puts("YES");
else puts("NO");
return 0;
}
B - Pair of Toys
Question
Tanechka is shopping in the toy shop. There are exactly n toys in the shop for sale, the cost of the i-th toy is i burles. She wants to choose two toys in such a way that their total cost is k burles. How many ways to do that does she have?
Each toy appears in the shop exactly once. Pairs (a,b) and (b,a) are considered equal. Pairs (a,b), where a=b, are not allowed.
题目大意
塔涅奇卡正在玩具店购物。店里恰好有 n n n个玩具在售,第 i i i个玩具的价格是i卢布。她想挑选两个玩具,使它们的总价正好是 k k k卢布。请问她有多少种选择方式?
每个玩具在店里仅出现一次。选择顺序不同的组合 ( a , b ) (a,b) (a,b)和 ( b , a ) (b,a) (b,a)被视为相同。不允许选择 ( a , b ) (a,b) (a,b)这样 a = b a=b a=b的组合。
Input
8 5
Output
2
分析
考虑从
k
k
k入手。
想要两个加数相加等于
k
k
k,拿
k
k
k = 5举例,有以下几种情况:
- a = 1 , b = 4 a = 1, b = 4 a=1,b=4
- a = 2 , b = 3 a = 2, b = 3 a=2,b=3
- a = 3 , b = 2 a = 3, b = 2 a=3,b=2
- a = 4 , b = 1 a = 4, b = 1 a=4,b=1
因为题目说选择顺序不同的组合(a,b)和(b,a)被视为相同。所以,后两种情况不算。一共有两种情况。
不难发现,左边a值的变化规律为1, 2, 3, 4有5 - 1=4种情况,依题意,当
a
a
a值差不多过半时,后面的情况都不算。
由此可得正解。
(细节详见代码)
AC Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
//不开long long见祖宗
ll n, k, ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
if(n >= k) {
if(k % 2) ans = k / 2;
else ans = k / 2 - 1;
}
else if(n < k / 2) ans = 0;
else {
if(k % 2) ans = (k - 1)/ 2 - k + n + 1;
else ans = k / 2 - k + n;
}
//奇偶情况要分开考虑
cout << ans << '\n';
return 0;
}
C - Bracket Subsequence
Question
A bracket sequence is a string containing only characters “(” and “)”. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters “1” and “+” between the original characters of the sequence. For example, bracket sequences “()()” and “(())” are regular (the resulting expressions are: “(1)+(1)” and “((1+1)+1)”), and “)(”, “(” and “)” are not.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
You are given a regular bracket sequence s and an integer number k. Your task is to find a regular bracket sequence of length exactly k such that it is also a subsequence of s.
It is guaranteed that such sequence always exists.
题目大意
括号序列是仅包含字符"(“和”)“的字符串。一个正则括号序列可以通过在原字符间插入"1"和”+“转换为正确的算术表达式。例如”()()“和”(())“是正则序列(转换后分别为”(1)+(1)“和”((1+1)+1)“),而”)(“、”(“和”)"则不是。
子序列是指在不改变剩余元素顺序的情况下,通过删除某些元素从另一个序列派生出的序列。
给定一个正则括号序列 s s s和整数 k k k。你的任务是找出一个长度恰好为 k k k的正则括号序列,且该序列是 s s s的子序列。
题目保证这样的序列总是存在。
Input
6 4
()(())
Output
()()
分析
一个长度恰好为k的正则括号序列也就是输出中包含
k
/
2
k/2
k/2 个括号。
所以,总结出思路如下:
- 扫一遍字符串
- 过程中,如果 s i s_i si为"(“且当前”("的数量不足 k / 2 k/2 k/2 个,就直接输出。
- 如果 s i s_i si为")“且”)“的个数与”("的个数不相等,也直接输出。
正确性比较明显,在这里就不证明了。
(细节详见代码)
AC Code
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k, f[N];
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
cin >> s;
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < n; i++) {
if(cnt1 + cnt2 == k)
break;
if(s[i] == '(' && cnt1 < k / 2)
cout << s[i], cnt1++;
else if(s[i] == ')' && cnt2 < cnt1)
cout << s[i], cnt2++;
}
//模拟思路
return 0;
}
The End
33



被折叠的 条评论
为什么被折叠?



