A - Spoiler
Problem Statement
You are given a string
S
S
S consisting of lowercase English letters and |
.
S
S
S is guaranteed to contain exactly two |
s.
Remove the characters between the two |
s, including the |
s themselves, and print the resulting string.
Constraints
-
S
S
S is a string of length between
2
2
2 and
100
100
100, inclusive, consisting of lowercase English letters and
|
. -
S
S
S contains exactly two
|
s.
Sample Input 1
atcoder|beginner|contest
Sample Output 1
atcodercontest
Remove all the characters between the two |
s and print the result.
思路: 非常简单的一道题,找出两个‘|’的位置即可,在输出时跳过
Code
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF 0x3f3f3f3f
using namespace std;
// set<int>::iterator it;
void solve()
{
string s;
cin >> s;
int n = s.size();
int l = 0, r = 0;
f(i, 0, n - 1)
{
if (s[i] == '|')
{
l = i;
break;
}
}
ff(i, n - 1, 0)
{
if (s[i] == '|')
{
r = i;
break;
}
}
f(i, 0, l-1)
{
cout << s[i];
}
f(i, r+1, n-1)
{
cout << s[i];
}
cout << endl;
return;
}
signed main(void)
{
ios::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}
B - Delimiter
Problem Statement
You are given
N
N
N integers
A
1
,
A
2
,
…
,
A
N
A_1,A_2,\dots,A_N
A1,A2,…,AN, one per line, over
N
N
N lines. However,
N
N
N is not given in the input.
Furthermore, the following is guaranteed:
- A i ≠ 0 A_i \neq 0 Ai=0 ( 1 ≤ i ≤ N − 1 1 \le i \le N-1 1≤i≤N−1 )
- A N = 0 A_N = 0 AN=0
Print A N , A N − 1 , … , A 1 A_N, A_{N-1},\dots,A_1 AN,AN−1,…,A1 in this order.
Constraints
- All input values are integers.
- 1 ≤ N ≤ 100 1 \le N \le 100 1≤N≤100
- 1 ≤ A i ≤ 1 0 9 1 \le A_i \le 10^9 1≤Ai≤109 ( 1 ≤ i ≤ N − 1 1 \le i \le N-1 1≤i≤N−1 )
- A N = 0 A_N = 0 AN=0
Sample Input 1
3
2
1
0
Sample Output 1
0
1
2
3
Note again that N N N is not given in the input. Here, N = 4 N=4 N=4 and A = ( 3 , 2 , 1 , 0 ) A=(3,2,1,0) A=(3,2,1,0).
思路:简单题
Code
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF 0x3f3f3f3f
using namespace std;
// set<int>::iterator it;
int a[maxn] = {0};
signed main(void)
{
ios::sync_with_stdio(false), cin.tie(nullptr);
int w = 0;
while (1)
{
int x;
cin >> x;
if (x == 0)
{
a[++w] = 0;
break;
}
a[++w] = x;
}
ff(i, w, 1)
{
cout << a[i] << endl;
}
return 0;
}
C - A+B+C
Problem Statement
You are given three sequences A = ( A 1 , … , A N ) A=(A_1,\ldots,A_N) A=(A1,…,AN), B = ( B 1 , … , B M ) B=(B_1,\ldots,B_M) B=(B1,…,BM), and C = ( C 1 , … , C L ) C=(C_1,\ldots,C_L) C=(C1,…,CL).
Additionally, a sequence X = ( X 1 , … , X Q ) X=(X_1,\ldots,X_Q) X=(X1,…,XQ) is given. For each i = 1 , … , Q i=1,\ldots,Q i=1,…,Q, solve the following problem:
Problem: Is it possible to select one element from each of A A A, B B B, and C C C so that their sum is X i X_i Xi?
Constraints
- 1 ≤ N , M , L ≤ 100 1 \leq N,M,L \leq 100 1≤N,M,L≤100
- 0 ≤ A i , B i , C i ≤ 1 0 8 0 \leq A_i, B_i ,C_i \leq 10^8 0≤Ai,Bi,Ci≤108
- 1 ≤ Q ≤ 2 × 1 0 5 1 \leq Q \leq 2\times 10^5 1≤Q≤2×105
- 0 ≤ X i ≤ 3 × 1 0 8 0 \leq X_i \leq 3\times 10^8 0≤Xi≤3×108
- All input values are integers.
Sample Input 1
3
1 2 3
2
2 4
6
1 2 4 8 16 32
4
1 5 10 50
Sample Output 1
No
Yes
Yes
No
- It is impossible to select one element from each of A A A, B B B, and C C C so that their sum is 1 1 1.
- Selecting 1 1 1, 2 2 2, and 2 2 2 from A A A, B B B, and C C C, respectively, makes the sum 5 5 5.
- Selecting 2 2 2, 4 4 4, and 4 4 4 from A A A, B B B, and C C C, respectively, makes the sum 10 10 10.
- It is impossible to select one element from each of A A A, B B B, and C C C so that their sum is 50 50 50.
思路:暴力模拟即可
Code
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> A, B, C, X;
unordered_set<int> sums;
int n;
cin>>n;
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
A.push_back(a);
}
int m;
cin>>m;
for (int i = 0; i < m; i++)
{
int b;
cin >> b;
B.push_back(b);
}
int l;
cin>>l;
for (int i = 0; i < l; i++)
{
int c;
cin >> c;
C.push_back(c);
}
int p;
cin>>p;
for (int i = 0; i < p; i++)
{
int x;
cin >> x;
X.push_back(x);
}
for (int a : A)
{
for (int b : B)
{
sums.insert(a + b);
}
}
for (int x : X)
{
bool found = false;
for (int c : C)
{
if (sums.count(x - c))
{
found = true;
break;
}
}
cout << (found ? "Yes" : "No") << endl;
}
return 0;
}
D - String Bags
Problem Statement
You initially have an empty string
S
S
S.
Additionally, there are bags
1
,
2
,
…
,
N
1, 2, \dots, N
1,2,…,N, each containing some strings.
Bag
i
i
i contains
A
i
A_i
Ai strings
S
i
,
1
,
S
i
,
2
,
…
,
S
i
,
A
i
S_{i,1}, S_{i,2}, \dots, S_{i,A_i}
Si,1,Si,2,…,Si,Ai.
You will repeat the following steps for i = 1 , 2 , … , N i = 1, 2, \dots, N i=1,2,…,N:
- Choose and perform one of the following two actions:
- Pay 1 1 1 yen, select exactly one string from bag i i i, and concatenate it to the end of S S S.
- Do nothing.
Given a string
T
T
T, find the minimum amount of money required to make the final
S
S
S equal
T
T
T.
If there is no way to make the final
S
S
S equal
T
T
T, print -1
.
Constraints
- T T T is a string consisting of lowercase English letters with length between 1 1 1 and 100 100 100, inclusive.
- N N N is an integer between 1 1 1 and 100 100 100, inclusive.
- A i A_i Ai is an integer between 1 1 1 and 10 10 10, inclusive.
- S i , j S_{i,j} Si,j is a string consisting of lowercase English letters with length between 1 1 1 and 10 10 10, inclusive.
Sample Input 1
abcde
3
3 ab abc abcd
4 f c cd bcde
2 e de
Sample Output 1
2
For example, doing the following makes the final S S S equal T T T with two yen, which can be shown to be the minimum amount required.
- For
i
=
1
i=1
i=1, select
abc
from bag 1 1 1 and concatenate it to the end of S S S, making S = S= S=abc
. - For i = 2 i=2 i=2, do nothing.
- For
i
=
3
i=3
i=3, select
de
from bag 3 3 3 and concatenate it to the end of S S S, making S = S= S=abcde
.
思路:分组背包,加上substr检查,注意覆盖问题,反向填表
Code
#include <bits/stdc++.h>
#include <string>
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF 0x3f3f3f3f
using namespace std;
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string T;
cin >> T;
int N;
cin >> N;
vector<string> bags[N];
f(i, 0, N - 1)
{
int Ai;
cin >> Ai;
for (int j = 0; j < Ai; j++)
{
string Si;
cin >> Si;
if (Si.size() > 0 && Si.size() <= T.size()) // 排除空字符串和长度大于T的字符串
{
bags[i].pb(Si);
}
}
}
vector<int> dp(T.size() + 10, INF);
dp[0] = 0;
f(j, 0, N - 1)
{
ff(i, T.size(), 0) // i是现在填了的长度
{
for (string s : bags[j])
{
if (i + s.size() <= T.size() && T.substr(i, s.size()) == s)
{
dp[i + s.size()] = min(dp[i + s.size()], dp[i] + 1);
}
}
}
}
if (dp[T.size()] == INF)
{
cout << -1 << endl;
}
else
{
cout << dp[T.size()] << endl;
}
return 0;
}
E - Insert or Erase
Problem Statement
You are given a sequence A = ( A 1 , … , A N ) A=(A_1,\ldots,A_N) A=(A1,…,AN) of length N N N. The elements of A A A are distinct.
Process Q Q Q queries in the order they are given. Each query is of one of the following two types:
1 x y
: Insert y y y immediately after the element x x x in A A A. It is guaranteed that x x x exists in A A A when this query is given.2 x
: Remove the element x x x from A A A. It is guaranteed that x x x exists in A A A when this query is given.
It is guaranteed that after processing each query, A A A will not be empty, and its elements will be distinct.
Print A A A after processing all the queries.
Constraints
- 1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2\times 10^5 1≤N≤2×105
- 1 ≤ Q ≤ 2 × 1 0 5 1 \leq Q \leq 2\times 10^5 1≤Q≤2×105
- 1 ≤ A i ≤ 1 0 9 1 \leq A_i \leq 10^9 1≤Ai≤109
- A i ≠ A j A_i \neq A_j Ai=Aj
- For queries of the first type, 1 ≤ x , y ≤ 1 0 9 1 \leq x,y \leq 10^9 1≤x,y≤109.
- When a query of the first type is given, x x x exists in A A A.
- For queries of the second type, 1 ≤ x ≤ 1 0 9 1 \leq x \leq 10^9 1≤x≤109.
- When a query of the second type is given, x x x exists in A A A.
- After processing each query, A A A is not empty, and its elements are distinct.
- All input values are integers.
Sample Input 1
4
2 1 4 3
4
2 1
1 4 5
2 2
1 5 1
Sample Output 1
4 5 1 3
The queries are processed as follows:
- Initially, A = ( 2 , 1 , 4 , 3 ) A=(2,1,4,3) A=(2,1,4,3).
- The first query removes 1 1 1, making A = ( 2 , 4 , 3 ) A=(2,4,3) A=(2,4,3).
- The second query inserts 5 5 5 immediately after 4 4 4, making A = ( 2 , 4 , 5 , 3 ) A=(2,4,5,3) A=(2,4,5,3).
- The third query removes 2 2 2, making A = ( 4 , 5 , 3 ) A=(4,5,3) A=(4,5,3).
- The fourth query inserts 1 1 1 immediately after 5 5 5, making A = ( 4 , 5 , 1 , 3 ) A=(4,5,1,3) A=(4,5,1,3).
思路:根据题意,可以自然的想到双向链表,实现高效的插入和删除
Code
#include <bits/stdc++.h>
#include <iostream>
#include <list>
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define endl '\n'
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF 0x3f3f3f3f
using namespace std;
// set<int>::iterator it;
list<int> a;
map<int, list<int>::iterator> pos;
signed main(void)
{
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
f(i, 1, n)
{
int x;
cin >> x;
a.pb(x);
pos[x] = prev(a.end());//获取a.end()的前一个迭代器
}
int q;
cin >> q;
while (q--)
{
int qq;
cin >> qq;
if (qq == 1)
{
int x, y;
cin >> x >> y;
pos[y] = a.insert(next(pos[x]), y);//小技巧,链表的insert()在插入完成后会返回一个迭代器,即在pos[x]的下一个位置(也就是 x 的后面,这个函数会返回一个迭代器,指向新插入的元素 y
}
else if (qq == 2)
{
int x;
cin >> x;
a.erase(pos[x]);
pos.erase(x);
}
}
for (auto i : a)
{
cout << i << " ";
}
cout << endl;
return 0;
}