You are given a string S S S of length N N N consisting of A
, B
, and C
.
Find the position where ABC
first appears as a (contiguous) substring in S S S. In other words, find the smallest integer n n n that satisfies all of the following conditions.
1 ≤ n ≤ N − 2 1 \leq n \leq N - 2 1≤n≤N−2.
The string obtained by extracting the n n n-th through ( n + 2 ) (n+2) (n+2)-th characters of S S S is ABC
.
If ABC
does not appear in S S S, print -1
.
3 ≤ N ≤ 100 3 \leq N \leq 100 3≤N≤100
S S S is a string of length N N N consisting of A
, B
, and C
.
The input is given from Standard Input in the following format:
N N N
S S S
Print the position where ABC
first appears as a substring in S S S, or -1
if it does not appear in S S S.
8
ABABCABC
3
ABC
first appears in S S S at the 3 3 3-rd through 5 5 5-th characters of S S S. Therefore, the answer is 3 3 3.
3
ACB
-1
If ABC
does not appear in S S S, print − 1 -1 −1.
20
BBAAABBACAACABCBABAB
13
对于每一个起始位置,进行判断是否能构成 ABC
即可。
#include
#define int long long
using namespace std;
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int N;
string s;
cin >> N >> s;
for (int i = 0; i < s.size(); i ++)
if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C')
{
cout << i + 1 << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
You are given two strings S S S and T T T consisting of lowercase English letters. The lengths of S S S and T T T are N N N and M M M, respectively. (The constraints guarantee that N ≤ M N \leq M N≤M.)
S S S is said to be a prefix of T T T when the first N N N characters of T T T coincide S S S.
S S S is said to be a suffix of T T T when the last N N N characters of T T T coincide S S S.
If S S S is both a prefix and a suffix of T T T, print 0 0 0;
If S S S is a prefix of T T T but not a suffix, print 1 1 1;
If S S S is a suffix of T T T but not a prefix, print 2 2 2;
If S S S is neither a prefix nor a suffix of T T T, print 3 3 3.
1 ≤ N ≤ M ≤ 100 1 \leq N \leq M \leq 100 1≤N≤M≤100
S S S is a string of length N N N consisting of lowercase English letters.
T T T is a string of length M M M consisting of lowercase English letters.
The input is given from Standard Input in the following format:
N N N M M M
S S S
T T T
Print the answer according to the instructions in the problem statement.
3 7
abc
abcdefg
1
S S S is a prefix of T T T but not a suffix, so you should print 1 1 1.
3 4
abc
aabc
2
S S S is a suffix of T T T but not a prefix.
3 3
abc
xyz
3
S S S is neither a prefix nor a suffix of T T T.
3 3
aaa
aaa
0
S S S and T T T may coincide, in which case S S S is both a prefix and a suffix of T T T.
通过枚举判断是否是前缀或后缀即可,最后按照要求输出。
#include
#define int long long
using namespace std;
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int N, M;
string s, t;
cin >> N >> M;
cin >> s >> t;
bool flg1 = 1;
for (int i = 0; i < N; i ++)
if (s[i] != t[i])
{
flg1 = 0;
break;
}
bool flg2 = 1;
for (int i = N - 1; i >= 0; i --)
if (s[i] != t[M - N + i])
{
flg2 = 0;
break;
}
if (flg1 && flg2) cout << 0 << endl;
else if (flg1) cout << 1 << endl;
else if (flg2) cout << 2 << endl;
else cout << 3 << endl;
return 0;
}
The AtCoder Kingdom holds a festival for N N N days. On M M M of these days, namely on the A 1 A_1 A1-th, A 2 A_2 A2-th, … \dots …, A M A_M AM-th days, fireworks will be launched. It is guaranteed that fireworks will be launched on the last day of the festival. (In other words, A M = N A_M=N AM=N is guaranteed.)
For each i = 1 , 2 , … , N i=1,2,\dots,N i=1,2,…,N, solve the following problem.
How many days later from the i i i-th day will fireworks be launched for the first time on or after the i i i-th day? If fireworks are launched on the i i i-th day, it is considered to be 0 0 0 days later.
1 ≤ M ≤ N ≤ 2 × 1 0 5 1 \le M \le N \le 2 \times 10^5 1≤M≤N≤2×105
1 ≤ A 1 ≤ A 2 ≤ ⋯ ≤ A M = N 1 \le A_1 \le A_2 \le \dots \le A_M = N 1≤A1≤A2≤⋯≤AM=N
All input values are integers.
The input is given from Standard Input in the following format:
N N N M M M
A 1 A_1 A1 A 2 A_2 A2 … \dots … A M A_M AM
Print N N N lines.
The i i i-th line ( 1 ≤ i ≤ N ) (1 \le i \le N) (1≤i≤N) should contain an integer representing the number of days from the i i i-th day until fireworks are launched for the first time on or after the i i i-th day.
3 2
2 3
1
0
0
The kingdom holds a festival for 3 3 3 days, and fireworks are launched on the 2 2 2-nd and 3 3 3-rd days.
From the 1 1 1-st day, the first time fireworks are launched is the 2 2 2-nd day of the festival, which is 1 1 1 day later.
From the 2 2 2-nd day, the first time fireworks are launched is the 2 2 2-nd day of the festival, which is 0 0 0 days later.
From the 3 3 3-rd day, the first time fireworks are launched is the 3 3 3-rd day of the festival, which is 0 0 0 days later.
8 5
1 3 4 7 8
0
1
0
0
2
1
0
0
本蒟蒻直接 无脑双指针,貌似可能不需要。
对于每一个 i i i,找出比他大的最小的数,然后输出差值即可。(lowerbound
也是可以的)
#include
#define int long long
using namespace std;
const int SIZE = 2e5 + 10;
int N, M;
int A[SIZE];
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> N >> M;
for (int i = 1; i <= M; i ++)
cin >> A[i];
int j = 1;
for (int i = 1; i <= N; i ++)
{
while (j < M && A[j] < i) j ++;
cout << A[j] - i << endl;
}
return 0;
}
纯模拟,跑路了……
AtCoder Inc. is planning to develop a product. The product has K K K parameters, whose values are currently all zero. The company aims to raise all parameter values to at least P P P.
There are N N N development plans. Executing the i i i-th development plan ( 1 ≤ i ≤ N 1 \le i \le N 1≤i≤N) increases the value of the j j j-th parameter by A i , j A_{i,j} Ai,j for every integer j j j such that 1 ≤ j ≤ K 1 \le j \le K 1≤j≤K, at the cost of C i C_i Ci.
A development plan cannot be executed more than once. Determine whether the company can achieve its goal, and if it can, find the minimum total cost required to achieve the goal.
1 ≤ N ≤ 100 1 \le N \le 100 1≤N≤100
1 ≤ K , P ≤ 5 1 \le K,P \le 5 1≤K,P≤5
0 ≤ A i , j ≤ P ( 1 ≤ i ≤ N , 1 ≤ j ≤ K ) 0 \le A_{i,j} \le P(1 \le i \le N,1 \le j \le K) 0≤Ai,j≤P(1≤i≤N,1≤j≤K)
1 ≤ C i ≤ 1 0 9 ( 1 ≤ i ≤ N ) 1 \le C_i \le 10^9(1 \le i \le N) 1≤Ci≤109(1≤i≤N)
All input values are integers.
The input is given from Standard Input in the following format:
N N N K K K P P P
C 1 C_1 C1 A 1 , 1 A_{1,1} A1,1 A 1 , 2 A_{1,2} A1,2 … \dots … A 1 , K A_{1,K} A1,K
C 2 C_2 C2 A 2 , 1 A_{2,1} A2,1 A 2 , 2 A_{2,2} A2,2 … \dots … A 2 , K A_{2,K} A2,K
… \dots …
C N C_N CN A N , 1 A_{N,1} AN,1 A N , 2 A_{N,2} AN,2 … \dots … A N , K A_{N,K} AN,K
If AtCoder Inc. can achieve its goal, print the minimum total cost required to achieve the goal; otherwise, print -1
.
4 3 5
5 3 0 2
3 1 2 3
3 2 4 0
1 0 1 4
9
If you execute the first, third, and fourth development plans, each parameter will be 3 + 2 + 0 = 5 , 0 + 4 + 1 = 5 , 2 + 0 + 4 = 6 3+2+0=5,0+4+1=5,2+0+4=6 3+2+0=5,0+4+1=5,2+0+4=6, all of which are at least 5 5 5, so the goal is achieved. The total cost in this case is 5 + 3 + 1 = 9 5 + 3 + 1 = 9 5+3+1=9.
It is impossible to achieve the goal at a total cost of 8 8 8 or less. Thus, the answer is 9 9 9.
7 3 5
85 1 0 1
37 1 1 0
38 2 0 0
45 0 2 2
67 1 1 0
12 2 2 0
94 2 2 1
-1
You cannot achieve the goal no matter what you do. Thus, print -1
.
一道动态规划 Dynamic Programming题目。
这里讲一下考场上的奇葩作法:
#include
#define int long long
using namespace std;
int N, K, P;
int A[101][6], C[101];
int F[101][6][6][6][6][6];
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> N >> K >> P;
for (int i = 1; i <= N; i ++)
{
cin >> C[i];
for (int j = 1; j <= K; j ++)
cin >> A[i][j];
}
memset(F, 0x3f, sizeof F);
for (int i = 0; i <= N; i ++)
F[i][0][0][0][0][0] = 0;
for (int i = 1; i <= N; i ++)
for (int j = 0; j < i; j ++)
for (int a = 0; a <= P; a ++)
for (int b = 0; b <= P; b ++)
for (int c = 0; c <= P; c ++)
for (int d = 0; d <= P; d ++)
for (int e = 0; e <= P; e ++)
{
int &Tmp = F[i][min(P, a + A[i][1])][min(P, b + A[i][2])][min(P, c + A[i][3])][min(P, d + A[i][4])][min(P, e + A[i][5])];
Tmp = min(Tmp, F[j][a][b][c][d][e] + C[i]);
}
if (K == 1)
{
int T = F[N][P][0][0][0][0];
for (int i = 1; i <= N; i ++)
T = min(T, F[i][P][0][0][0][0]);
if (T > 1e16) cout << -1 << endl;
else cout << T << endl;
}
if (K == 2)
{
int T = F[N][P][P][0][0][0];
for (int i = 1; i <= N; i ++)
T = min(T, F[i][P][P][0][0][0]);
if (T > 1e16) cout << -1 << endl;
else cout << T << endl;
}
if (K == 3)
{
int T = F[N][P][P][P][0][0];
for (int i = 1; i <= N; i ++)
T = min(T, F[i][P][P][P][0][0]);
if (T > 1e16) cout << -1 << endl;
else cout << T << endl;
}
if (K == 4)
{
int T = F[N][P][P][P][P][0];
for (int i = 1; i <= N; i ++)
T = min(T, F[i][P][P][P][P][0]);
if (T > 1e16) cout << -1 << endl;
else cout << T << endl;
}
if (K == 5)
{
int T = F[N][P][P][P][P][P];
for (int i = 1; i <= N; i ++)
T = min(T, F[i][P][P][P][P][P]);
if (T > 1e16) cout << -1 << endl;
else cout << T << endl;
}
return 0;
}
You are given a string S S S of length N N N consisting of 0
and 1
. Let S i S_i Si denote the i i i-th character of S S S.
Process Q Q Q queries in the order they are given.
Each query is represented by a tuple of three integers ( c , L , R ) (c, L, R) (c,L,R), where c c c represents the type of the query.
When c = 1 c=1 c=1: For each integer i i i such that L ≤ i ≤ R L \leq i \leq R L≤i≤R, if S i S_i Si is 1
, change it to 0
; if it is 0
, change it to 1
.
When c = 2 c=2 c=2: Let T T T be the string obtained by extracting the L L L-th through R R R-th characters of S S S. Print the maximum number of consecutive 1
s in T T T.
1 ≤ N ≤ 5 × 1 0 5 1 \leq N \leq 5 \times 10^5 1≤N≤5×105
1 ≤ Q ≤ 1 0 5 1 \leq Q \leq 10^5 1≤Q≤105
S S S is a string of length N N N consisting of 0
and 1
.
c ∈ { 1 , 2 } c \in \lbrace 1, 2 \rbrace c∈{1,2}
1 ≤ L ≤ R ≤ N 1 \leq L \leq R \leq N 1≤L≤R≤N
N N N, Q Q Q, c c c, L L L, and R R R are all integers.
The input is given from Standard Input in the following format, where q u e r y i \mathrm{query}_i queryi represents the i i i-th query:
N N N Q Q Q
S S S
q u e r y 1 \mathrm{query}_1 query1
q u e r y 2 \mathrm{query}_2 query2
⋮ \vdots ⋮
q u e r y Q \mathrm{query}_Q queryQ
Each query is given in the following format:
c c c L L L R R R
Let k k k be the number of queries with c = 2 c=2 c=2. Print k k k lines.
The i i i-th line should contain the answer to the i i i-th query with c = 2 c=2 c=2.
7 6
1101110
2 1 7
2 2 4
1 3 6
2 5 6
1 4 7
2 1 7
3
1
0
7
The queries are processed as follows.
Initially, S = S= S= 1101110
.
For the first query, T = T = T= 1101110
. The longest sequence of consecutive 1
s in T T T is 111
from the 4 4 4-th character to 6 6 6-th character, so the answer is 3 3 3.
For the second query, T = T = T= 101
. The longest sequence of consecutive 1
s in T T T is 1
at the 1 1 1-st or 3 3 3-rd character, so the answer is 1 1 1.
For the third query, the operation changes S S S to 1110000
.
For the fourth query, T = T = T= 00
. T T T does not contain 1
, so the answer is 0 0 0.
For the fifth query, the operation changes S S S to 1111111
.
For the sixth query, T = T = T= 1111111
. The longest sequence of consecutive 1
s in T T T is 1111111
from the 1 1 1-st to 7 7 7-th characters, so the answer is 7 7 7.
貌似是 线段树 经典题,场上没搞出来,(后悔
这题无疑两个操作:
考虑线段树内存储哪些信息,建议先做一下:P4513 小白逛公园 和 P6492 [COCI2010-2011#6] STEP
做完这两道题,相信大家就知道答案了:
P u s h u p \mathrm{Pushup} Pushup 操作:
这样,只是求出了最长连续 1 1 1 的个数,还不支持翻转:
考虑如何处理翻转,我们会发现其实就是最长连续的 1 1 1,变成了最长连续的 0 0 0。所以我们线段数还要处理最长连续的 0 0 0,这样我们再仿照上面的操作进行一遍就可以切掉这道题了。
时间有限,写的可能简略,如有不懂 一定 要问我呦!
#include
#define int long long
using namespace std;
const int SIZE = 5e5 + 10;
int N, Q;
int A[SIZE];
struct Segment
{
int l, r;
int L0, L1, R0, R1;
int Len0, Len1;
int Swap;
}Tree[SIZE * 4];
void Pushup(int u)
{
if (Tree[u << 1].r - Tree[u << 1].l + 1 == Tree[u << 1].Len0)
Tree[u].L0 = Tree[u << 1].Len0 + Tree[u << 1 | 1].L0;
else
Tree[u].L0 = Tree[u << 1].L0;
if (Tree[u << 1 | 1].r - Tree[u << 1 | 1].l + 1 == Tree[u << 1 | 1].Len0)
Tree[u].R0 = Tree[u << 1 | 1].Len0 + Tree[u << 1].R0;
else
Tree[u].R0 = Tree[u << 1 | 1].R0;
if (Tree[u << 1].r - Tree[u << 1].l + 1 == Tree[u << 1].Len1)
Tree[u].L1 = Tree[u << 1].Len1 + Tree[u << 1 | 1].L1;
else
Tree[u].L1 = Tree[u << 1].L1;
if (Tree[u << 1 | 1].r - Tree[u << 1 | 1].l + 1 == Tree[u << 1 | 1].Len1)
Tree[u].R1 = Tree[u << 1 | 1].Len1 + Tree[u << 1].R1;
else
Tree[u].R1 = Tree[u << 1 | 1].R1;
Tree[u].Len0 = max(max(Tree[u << 1].Len0, Tree[u << 1 | 1].Len0), Tree[u << 1].R0 + Tree[u << 1 | 1].L0);
Tree[u].Len1 = max(max(Tree[u << 1].Len1, Tree[u << 1 | 1].Len1), Tree[u << 1].R1 + Tree[u << 1 | 1].L1);
}
void Pushdown(int u)
{
if (Tree[u].Swap != 0)
{
swap(Tree[u << 1].L0, Tree[u << 1].L1), swap(Tree[u << 1].R0, Tree[u << 1].R1);
swap(Tree[u << 1].Len0, Tree[u << 1].Len1);
Tree[u << 1].Swap ^= 1;//这里要注意如果本来要翻转,就不要再进行翻转了。
swap(Tree[u << 1 | 1].L0, Tree[u << 1 | 1].L1), swap(Tree[u << 1 | 1].R0, Tree[u << 1 | 1].R1);
swap(Tree[u << 1 | 1].Len0, Tree[u << 1 | 1].Len1);
Tree[u << 1 | 1].Swap ^= 1;//这里要注意如果本来要翻转,就不要再进行翻转了。
Tree[u].Swap = 0;
}
}
void Build(int u, int l, int r)
{
if (l == r)
{
Tree[u] = {l, l, !A[l], A[l], !A[l], A[l], !A[l], A[l]};
return;
}
Tree[u] = {l, r};
int mid = l + r >> 1;
Build(u << 1, l, mid), Build(u << 1 | 1, mid + 1, r);
Pushup(u);
}
void Modify(int u, int l, int r)
{
if (Tree[u].l >= l && Tree[u].r <= r)
{
swap(Tree[u].L0, Tree[u].L1), swap(Tree[u].R0, Tree[u].R1);
swap(Tree[u].Len0, Tree[u].Len1);
Tree[u].Swap ^= 1; //这里要注意如果本来要翻转,就不要再进行翻转了。
return;
}
Pushdown(u);
int mid = Tree[u].l + Tree[u].r >> 1;
if (mid >= l) Modify(u << 1, l, r);
if (mid < r) Modify(u << 1 | 1, l, r);
Pushup(u);
}
Segment Query(int u, int l, int r)
{
if (Tree[u].l >= l && Tree[u].r <= r)
return Tree[u];
Pushdown(u);
int mid = Tree[u].l + Tree[u].r >> 1;
if (mid >= l && mid < r)
{
Segment Left = Query(u << 1, l, r), Right = Query(u << 1 | 1, l, r);
Pushup(u);
Segment Tmp;
Tmp.l = Left.l, Tmp.r = Right.r;
if (Left.L1 == Left.r - Left.l + 1)
Tmp.L1 = Left.L1 + Right.L1;
Tmp.L1 = max(Tmp.L1, Left.L1);
if (Right.r - Right.l + 1 == Right.Len1)
Tmp.R1 = Right.Len1 + Left.R1;
Tmp.R1 = max(Tmp.R1, Right.R1);
Tmp.Len1 = max(max(Left.Len1, Right.Len1), Left.R1 + Right.L1);
return Tmp;
}
else if (mid >= l)
return Query(u << 1, l, r);
else
return Query(u << 1 | 1, l, r);
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> N >> Q;
char x;
for (int i = 1; i <= N; i ++)
cin >> x, A[i] = x & 1;
Build(1, 1, N);
while (Q --)
{
int op, l, r;
cin >> op >> l >> r;
if (op == 1)
Modify(1, l, r);
else
cout << Query(1, l, r).Len1 << endl;
}
return 0;
}
未简化代码,大家见谅~~~