Email from Polycarp (模拟)

题目链接:http://codeforces.com/problemset/problem/1185/B

题目大意:Methodius想发一封邮件给他的朋友(应还要打印出来),但是它的键盘坏了,请你判断从键盘输入的内容,是否能按照他的想法打印出来(打印规则是可以输入重复的字符,打印机会过滤掉多余的重复字符,但是不能有不同的字符出现,这也是判断正确与否的关键),第一行输入从键盘输入的内容,第二行输入想打印的内容,能正确打印输出“YES”,否则输出“NO”

思路:

首先就是先去匹配str1 和 str2 看它们有没有出现多余的字符    如果出现了肯定是不可以的

再去看 str2 中有没有都出现 str1 的字符  

要注意 如果 str1 都匹配完了 ,如果 str2 还有剩余的话 ,我们要去判断这剩余的部分和不和str1的最后一个字符相等

 

别用数组  用string! 用数组会超时

具体的还是看代码吧:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdlib.h>
 4 #include <string>
 5 #include <string.h>
 6 #include <set>
 7 #include <queue>
 8 #include <math.h>
 9 #include <stdbool.h>
10 
11 #define LL long long
12 #define inf 0x3f3f3f3f
13 using namespace std;
14 const int MAXN=1000005;
15 
16 
17 string str1,str2;
18 
19 int main()
20 {
21 #ifndef ONLINE_JUDGE
22     freopen("../in.txt","r",stdin);
23 #endif
24     ios_base::sync_with_stdio(0);
25     cin.tie(NULL);
26     int n;
27     cin >> n;
28     while (n--)
29     {
30         int flag = 0;
31         str1="";
32         str2="";
33         cin >> str1 >> str2;
34         int len1 = str1.length();
35         int len2 = str2.length();
36         if (str2[0]!=str1[0] || len2<len1 || str2[len2-1]!=str1[len1-1]) // 减少搜索
37         {
38             printf("NO
");
39             continue;
40         }
41         int i=0,j=0,cnt=0;
42         for (i=0;i<len1;i++)
43         {
44             while (j<len2)
45             {
46                 if (str1[i] == str2[j])  //如果匹配到了,计数器加一
47                 {
48                     cnt++;
49                     j++;
50                     break;
51                 }
52                 else if (str1[i-1]!=str2[j]) // str1[i]!=str2[j]  那么我们判断是不是可能重复出现了
53                 {
54                     flag = 1;
55                     break;
56                 }
57                 j++;
58             }
59             if (flag) // 如果出现了多余的
60                 break;
61             if (j>=len2)
62                 break;
63         }
64         if (flag) //出现了多余的直接NO
65         {
66             printf("NO
");
67             continue;
68         }
69         if (!flag && cnt!=len1)   // 如果没出现多余的字母,但是str1出现的,str2并没有都出现  例如 str1:abaa str2:aaba
70         {
71             printf("NO
");
72             continue;
73         }
74         if (cnt == len1)
75         {
76             while (j<len2)  //判断str2之后还有没有字符,如果有必须和str1[len1-1]一样
77             {
78                 if (str1[len1-1] != str2[j])
79                 {
80                     flag = 1;
81                     break;
82                 }
83                 j++;
84             }
85         }
86         if (flag)
87             printf("NO
");
88         else
89             printf("YES
");
90     }
91     return 0;
92 }
原文地址:https://www.cnblogs.com/-Ackerman/p/11220853.html