CRB and String

                        

           CRB and String

题目抽象:给你两个字符串s,t;  每次你可以从s中任选一个字符,再其后插入一个与该字符不相等的字符。经过一定的操作,是否可以是s变成t.

分析:插入一些字符,s要变成t,那么一定要满足s是t的子序列。在此基础上, 如果s[i] != t[j], 那么如果s[i-1] != t[j]或者 k>= 1, s[k -1 ] != s[k], s[k] == s[k+1] == ... == s[i]  ,那么可以插入相应的字符。如果k < 1,那么就不能在第二种情况中插入,那么这就要求,k  t[0] == t[1] == ... ==t[k], s[0->k] ==t[0->k]

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #include <string>
 7 #include 
 8 #include <set>
 9 #include 
10 #include 
11 #include 
12 #include 
13 #include 
14 using namespace std;
15 typedef long long LL;
16 const LL INF = 0x5fffffff;
17 const double EXP = 1E-9;
18 const LL MOD = (LL)1E9+7;
19 const int MS = 100005;
20 
21 char s[MS], t[MS];
22 
23 //  s can convert to t
24 // 1: s is subsequence of t
25 // 2: k t[0] == t[1] == ……== t[k]  s[0->k] == t[0->k]
26 
27 int main() {
28     int T;
29     scanf("%d",&T);
30     while (T--) {
31         scanf("%s%s", s, t);
32         int len1 = (int)strlen(s);
33         int len2 = (int)strlen(t);
34         if (len1 >= len2) {
35             if (strcmp(s, t) == 0)
36                 printf("Yes\n");
37             else
38                 printf("No\n");
39             continue;
40         }
41         int k = 0;
42         while (k < len2 && t[k] == t[k + 1])
43             k++;
44         int ok = 1;
45         for (int i = 0; i <= k; i++) {
46             if (i > len1 -1 || s[i] != t[i]) {
47                 ok = 0;
48                 break;
49             }
50         }
51         if (!ok) {
52             printf("No\n");
53             continue;
54         }
55         int s1 = k;
56         int s2 = k;
57         while (s1 < len1 && s2 < len2) {
58             while (s2 < len2 &&s[s1] != t[s2]) {
59                 s2++;
60             }
61             if (s2 < len2) {
62                 s1++;
63                 s2++;
64             }
65             else {
66                 ok = 0;
67                 break;
68             }
69         }
70         if (ok)
71             printf("Yes\n");
72         else
73             printf("No\n");
74     }
75     return 0;
76 }

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/4746220.html

你可能感兴趣的:(CRB and String)