Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) ABCD

A. Single Wildcard Pattern Matching

You are given two strings ss and tt. The string ss consists of lowercase Latin letters and at most one wildcard character '*', the string ttconsists only of lowercase Latin letters. The length of the string ss equals nn, the length of the string tt equals mm.

The wildcard character '*' in the string ss (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of ss can be replaced with anything. If it is possible to replace a wildcard character '*' in ss to obtain a string tt, then the string tt matches the pattern ss.

For example, if s=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 tt matches the given string ss, print "YES", otherwise print "NO".

Input

The first line contains two integers nn and mm (1n,m21051≤n,m≤2⋅105) — the length of the string ss and the length of the string tt, respectively.

The second line contains string ss of length nn, which consists of lowercase Latin letters and at most one wildcard character '*'.

The third line contains string tt of length mm, which consists only of lowercase Latin letters.

Output

Print "YES" (without quotes), if you can obtain the string tt from the string ss. Otherwise print "NO" (without quotes).

Examples
input
Copy
6 10
code*s
codeforces
output
Copy
YES
input
Copy
6 5
vk*cup
vkcup
output
Copy
YES
input
Copy
1 1
v
k
output
Copy
NO
input
Copy
9 6
gfgf*gfgf
gfgfgf
output
Copy
NO
Note

In the first example a wildcard character '*' can be replaced with a string "force". So the string ss after this replacement is "codeforces" and the answer is "YES".

In the second example a wildcard character '*' can be replaced with an empty string. So the string ss after this replacement is "vkcup" and the answer is "YES".

There is no wildcard character '*' in the third example and the strings "v" and "k" are different so the answer is "NO".

In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string tt so the answer is "NO".

 

模拟题,有点费时。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define INF 0x3f3f3f3f
 4 using namespace std;
 5 const int N = 2e5+10;
 6 char s[N], t[N];
 7 int main() {
 8     int n, m, id;
 9     cin >> n >> m;
10     cin >> s >> t;
11     bool flag = false;
12     for(int i = 0; i < n; i ++) {
13         if(s[i] == '*') {
14             flag = true;
15             id = i;
16             break;
17         }
18     }
19     if(!flag) {
20         if(n != m) return 0*printf("NO\n");
21         for(int i = 0; i < n; i ++) {
22             if(s[i] != t[i]) return 0*printf("NO\n");
23         }
24         printf("YES\n");
25     } else {
26         int l = 0, r = n-1, i = 0;
27         while(s[l] == t[l]) l ++;
28         while(s[r] == t[m-1-i]) {
29             r--;i++;
30         }
31         // printf("%d %d %d\n",l,r,id);
32         if(n <= m+1 && l == id && r == id) printf("YES\n");
33         else printf("NO\n");
34     }
35     return 0;
36 }

 

B. Pair of Toys

Tanechka is shopping in the toy shop. There are exactly nn toys in the shop for sale, the cost of the ii-th toy is ii burles. She wants to choose two toys in such a way that their total cost is kk burles. How many ways to do that does she have?

Each toy appears in the shop exactly once. Pairs (a,b)(a,b) and (b,a)(b,a) are considered equal. Pairs (a,b)(a,b), where a=ba=b, are not allowed.

Input

The first line of the input contains two integers nn, kk (1n,k10141≤n,k≤1014) — the number of toys and the expected total cost of the pair of toys.

Output

Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is kk burles.

Examples
input
Copy
8 5
output
Copy
2
input
Copy
8 15
output
Copy
1
input
Copy
7 20
output
Copy
0
input
Copy
1000000000000 1000000000001
output
Copy
500000000000
Note

In the first example Tanechka can choose the pair of toys (1,41,4) or the pair of toys (2,32,3).

In the second example Tanechka can choose only the pair of toys (7,87,8).

In the third example choosing any pair of toys will lead to the total cost less than 2020. So the answer is 0.

In the fourth example she can choose the following pairs: (1,1000000000000)(1,1000000000000), (2,999999999999)(2,999999999999), (3,999999999998)(3,999999999998), ..., (500000000000,500000000001)(500000000000,500000000001). The number of such pairs is exactly 500000000000500000000000.

数学题

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define INF 0x3f3f3f3f
 4 using namespace std;
 5 const int N = 110;
 6 
 7 int main() {
 8     ll n, k;
 9     cin >> n >> k;
10     printf("%lld\n",min(k-1,max(k/2,n))-k/2);
11     return 0;
12 }

 

C. Bracket Subsequence

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 ss and an integer number kk. Your task is to find a regular bracket sequence of length exactly kksuch that it is also a subsequence of ss.

It is guaranteed that such sequence always exists.

Input

The first line contains two integers nn and kk (2kn21052≤k≤n≤2⋅105, both nn and kk are even) — the length of ss and the length of the sequence you are asked to find.

The second line is a string ss — regular bracket sequence of length nn.

Output

Print a single string — a regular bracket sequence of length exactly kk such that it is also a subsequence of ss.

It is guaranteed that such sequence always exists.

Examples
input
Copy
6 4
()(())
output
Copy
()()
input
Copy
8 8
(()(()))
output
Copy
(()(()))

去掉相对应的括号
 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define INF 0x3f3f3f3f
 4 using namespace std;
 5 const int N = 2e5+10;
 6 char s[N];
 7 int a[N], vis[N];
 8 int main() {
 9        int n, k;
10        cin >> n >> k;
11        cin >> s+1;
12        stack<int> st;
13        for(int i = 1; i <= n; i ++) {
14            if(s[i] == '(') st.push(i);
15            else {
16                int cnt = st.top();
17                st.pop();
18                a[cnt] = i;
19                a[i] = cnt;
20            }
21        }
22        int l = 1;
23        for(int i = 0; i < (n-k)/2; i ++) {
24            while(vis[l])l++;
25           vis[l] = vis[a[l]] = 1;
26        }
27        for(int i = 1; i <= n; i ++) {
28            if(!vis[i])printf("%c",s[i]);
29        }printf("\n");
30     return 0;
31 }

 

D. Array Restoration

Initially there was an array aa consisting of nn integers. Positions in it are numbered from 11 to nn.

Exactly qq queries were performed on the array. During the ii-th query some segment (li,ri)(li,ri) (1lirin)(1≤li≤ri≤n) was selected and values of elements on positions from lili to riri inclusive got changed to ii. The order of the queries couldn't be changed and all qq queries were applied. It is also known that every position from 11 to nn got covered by at least one segment.

We could have offered you the problem about checking if some given array (consisting of nn integers with values from 11 to qq) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.

So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to 00.

Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.

If there are multiple possible arrays then print any of them.

Input

The first line contains two integers nn and qq (1n,q21051≤n,q≤2⋅105) — the number of elements of the array and the number of queries perfomed on it.

The second line contains nn integer numbers a1,a2,,ana1,a2,…,an (0aiq0≤ai≤q) — the resulting array. If element at some position jj is equal to 00then the value of element at this position can be any integer from 11 to qq.

Output

Print "YES" if the array aa can be obtained by performing qq queries. Segments (li,ri)(li,ri) (1lirin)(1≤li≤ri≤n) are chosen separately for each query. Every position from 11 to nn should be covered by at least one segment.

Otherwise print "NO".

If some array can be obtained then print nn integers on the second line — the ii-th number should be equal to the ii-th element of the resulting array and should have value from 11 to qq. This array should be obtainable by performing exactly qq queries.

If there are multiple possible arrays then print any of them.

Examples
input
Copy
4 3
1 0 2 3
output
Copy
YES
1 2 2 3
input
Copy
3 10
10 10 10
output
Copy
YES
10 10 10
input
Copy
5 6
6 5 6 2 2
output
Copy
NO
input
Copy
3 5
0 0 0
output
Copy
YES
5 4 2
Note

In the first example you can also replace 00 with 11 but not with 33.

In the second example it doesn't really matter what segments to choose until query 1010 when the segment is (1,3)(1,3).

The third example showcases the fact that the order of queries can't be changed, you can't firstly set (1,3)(1,3) to 66 and after that change (2,2)(2,2) to 55. The segment of 55 should be applied before segment of 66.

There is a lot of correct resulting arrays for the fourth example.

 

0.0  

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define INF 0x3f3f3f3f
 4 using namespace std;
 5 const int N = 2e5+10;
 6 int a[N], n, q, ans[N];
 7 int dp[2][N];
 8 int main() {
 9     scanf("%d%d",&n, &q);
10     memset(dp[0], INF, sizeof(dp[0]));
11     memset(dp[1], 0, sizeof(dp[1]));
12     for(int i = 1; i <= n; i ++) {
13         scanf("%d",&a[i]);
14         dp[0][a[i]] = min(dp[0][a[i]], i);
15         dp[1][a[i]] = max(dp[1][a[i]], i);
16     }
17     for(int i = q; i >= 1; i --) {
18         int l = dp[0][i], r = dp[1][i];
19         if(r == 0) continue;
20         for(int j = l,k = r;j <= k; j ++, k --) {
21             if(ans[j] && ans[k]) break;
22             if(a[j] == 0) a[j] = i;
23             if(a[k] == 0) a[k] = i;
24             if(a[j] == i) ans[j] = 1;
25             else if(a[j] < i) return 0*printf("NO\n");
26             if(a[k] == i) ans[k] = 1;
27             else if(a[k] < i) return 0*printf("NO\n");
28         }
29     }
30     if(dp[1][q] == 0) {
31         if(dp[1][0] == 0) return 0*printf("NO\n");
32         else {
33             a[dp[0][0]]=q;
34             for(int i = 1;i <= n; i ++)  if(a[i]==0) a[i]=a[i-1];
35             printf("YES\n");
36             for(int i = 1;i <= n; i ++) printf("%d%c",a[i]," \n"[i==n]);
37         }
38     } else {
39         int cnt = 0;
40         for(int i = 1; i <= n; i ++)
41             if(a[i]){
42                 cnt = i;
43                 break;
44             }
45         for(int i = cnt-1, j = cnt+1; i >= 1 || j <= n; i --, j ++) {
46             if(i >= 1 && a[i] == 0) a[i]=a[i+1];
47             if(j <= n && a[j] == 0) a[j]=a[j-1];
48         }
49         printf("YES\n");
50         for(int i = 1;i <= n; i ++) printf("%d%c",a[i]," \n"[i==n]);
51     }
52     return 0;
53 }

 

posted @ 2018-08-18 09:34  starry_sky  阅读(392)  评论(0编辑  收藏  举报