刘汝佳《算法竞赛入门经典(第二版)》第三章习题(3-9~3-12)
习题3-9 子序列(UVa10340)
输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变)。得到字符串s。例如,abcde可以得到bce,但无法得到dc。
Input Specification
The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace. Input is terminated by EOF.
Output Specification
For each test case output, if s is a subsequence of t.
Sample Input
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter
Sample Output
Yes
Yes
解析
原题中规定时间限制2s,内存限制32MB,但没说输入字符串的长度限制,暂且按100000来做。这道题直接对s和t扫一遍即可。
#include <iostream>
#include <string>
using namespace std;
int main (void)
{
string s,t;
int i,j;
while (cin >> s >> t)
{
for (i = 0, j = 0; i < s.size() && j < t.size();)
{
if (s[i] == t[j])
{
i++;
j++;
}
else
j++;
}
(i == s.size())?cout << "yes":cout << endl;
}
return 0;
}
习题3-10 盒子(ACM/ICPC NEERC 2004,UVa1587)
给定6个矩形的长和宽wi和hi(1≤wi,hi≤1000),判断它们能否构成长方体的6个面。
Input file contains several test cases. Each of them consists of six lines. Each line describes one pallet and contains two integer numbers w and h ( 1≤w