Cattle practice 17 B-good position

portal

The meaning of the question: Originally, the Chinese question is not explained, but some people do not understand the meaning of the question. In short, each character of each s1 can form a substring == s2 with other characters.   Forget it, it is better to manage Chinese questions without explaining them.

Solution: In fact, when I wrote the nowcoder competition before, I was already shocked by the data of nowcoder, but yesterday I found that some people have handed in random numbers, some people have handed in Java sets of data, sweat, and then at noon someone urged someone to write about it. this topic.

          In fact, it is very simple. For s1, we match the first s2 substring from the beginning, and record L[i] at the corresponding position, match the last s2 substring from the end, and record the corresponding position R[ i]. where i is represented as the ith character of s2.

     Then for the ith character c, all positions of c between L[i] and R[i] are legal, because we can take the first half of the substring of L[i], add c, and Adding the second half substring of R[i] can form the string of s2.

     Then a little bit of greed and effort, just mark the difference.

          Finally, traverse all the positions of s1 to see if it is legal.

Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 2e5+10;
17 char s1[N];
18 char s2[N];
19 int L[N], R[N];
20 int sum[N][26];
21 int main(){
22     ///Fopen;
23     scanf("%s%s", s1, s2);
24     int len1 = strlen(s1), len2 = strlen(s2);
25     int j = -1, id ;
26     for(int i = 0; i < len2; i++){
27         j++;
28         while(j < len1 && s1[j] != s2[i]) j++;
29         if(j == len1) {puts("No"); return 0;}
30         id = s2[i] - 'a';
31         sum[j][id]++;
32     }
33     j = len1;
34     for(int i = len2-1; i >= 0; i--){
35         j--;
36         while(j >= 0 && s1[j] != s2[i]) j--;
37         if(j == -1) {puts("No"); return 0;}
38         id = s2[i] - 'a';
39         sum[j+1][id]--;
40     }
41     for(int i = 1; i < len1; i++)
42         for(int j = 0; j < 26; j++)
43             sum[i][j] += sum[i-1][j];
44     for(int i = 0; i < len1; i++){
45         id = s1[i] - 'a';
46         if(!sum[i][id]){puts("No"); return 0;}
47     }
48     puts("Yes");
49     return 0;
50 }
View Code

 

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388640&siteId=291194637