codeforces B. Dima and Text Messages 解题报告

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

题目意思:给出n个单词(假设为word1,word2、word3...wordn)和一句test message,需要判断的是,这个 test message在去除一系列随机插入的英文字符后,是否满足<3word1<3word2<3 ... wordn<3 的结构。

     首先要构造出一个参考序列,也就是<3word1<3word2<3 ... wordn<3的结构(总长度为 j )。

     接着用test message (假设指向它元素的指针为 i )跟这个参考序列(指针为 k)作左到右依次比较,如果有相同的字符,那么k 向右移动一位,最后当整个test message扫描完后,判断k的值是否等于j ,若是,则符合参考序列的结构。

方法一:没有用string

Time: 31ms

Memory:  1200KB

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxin = 1e5 + 10;
 8 const int maxsave = 1e6 + 10;
 9 char a[maxin], b[maxin];
10 char s[maxsave];
11 
12 int main()
13 {
14     int i, j, k, n, len1, len2;
15     while (scanf("%d", &n) != EOF)
16     {
17         j = 0;
18         s[j++] = '<';
19         s[j++] = '3';
20         while (n--)
21         {
22             scanf("%s", a);
23             len1 = strlen(a);
24             for (i = 0; i < len1; i++)
25                 s[j++] = a[i];
26             s[j++] = '<';
27             s[j++] = '3';     // 构造一条对照序列
28         }
29     //     for (i = 0; i < j; i++)
30      //       printf("%c", s[i]);
31     //    printf("

");
32         getchar();
33         gets(b);
34         len1 = strlen(b);
35         for (k = 0, i = 0; i < len1; i++)
36         {
37             if (b[i] == s[k])
38                 k++;
39         }
40         if (k == j)
41             puts("yes");
42         else
43             puts("no");
44     }
45     return 0;
46 }

方法二:用到string

Time:46ms

Memory:1000KB

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <string>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int i, j, n, len;
10     string str1, str2, tmp;
11     tmp = "<3";
12     while (scanf("%d", &n) != EOF)
13     {
14         str1.append(tmp);
15         for (i = 0; i < n; i++)
16         {
17             cin >> str2;
18             str1.append(str2);
19             str1.append(tmp);
20         }
21         cin >> str2;
22         for (j = 0, i = 0; i < str2.size(); i++)
23         {
24             if (str2[i] == str1[j])
25                 j++;  
26         }
27         if (j == str1.size())
28             puts("yes");
29         else
30             puts("no");
31         str1.clear();
32     }
33     return 0;
34 }


 

原文地址:https://www.cnblogs.com/windysai/p/3413256.html