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

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxin = 1e5 + ;
const int maxsave = 1e6 + ;
char a[maxin], b[maxin];
char s[maxsave]; int main()
{
int i, j, k, n, len1, len2;
while (scanf("%d", &n) != EOF)
{
j = ;
s[j++] = '<';
s[j++] = '';
while (n--)
{
scanf("%s", a);
len1 = strlen(a);
for (i = ; i < len1; i++)
s[j++] = a[i];
s[j++] = '<';
s[j++] = ''; // 构造一条对照序列
}
// for (i = 0; i < j; i++)
// printf("%c", s[i]);
// printf("\n\n");
getchar();
gets(b);
len1 = strlen(b);
for (k = , i = ; i < len1; i++)
{
if (b[i] == s[k])
k++;
}
if (k == j)
puts("yes");
else
puts("no");
}
return ;
}

方法二:用到string

Time:46ms

Memory:1000KB

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std; int main()
{
int i, j, n, len;
string str1, str2, tmp;
tmp = "<3";
while (scanf("%d", &n) != EOF)
{
str1.append(tmp);
for (i = ; i < n; i++)
{
cin >> str2;
str1.append(str2);
str1.append(tmp);
}
cin >> str2;
for (j = , i = ; i < str2.size(); i++)
{
if (str2[i] == str1[j])
j++;
}
if (j == str1.size())
puts("yes");
else
puts("no");
str1.clear();
}
return ;
}
上一篇:mysql 命令(一)


下一篇:ccui.ScrollView 扩展