However, for the password ABC, the string TRAGICBIRTHDAYCACEY is not a valid message.
While the A is the first of the set fA, B, Cg to appear in the string,
the next occurrence from the set fB, Cg is C rather than B.
Also, the string HAPPYBIRTHDAY is not a valid message for the password ABC because the C never appears.
As an example with duplicate letters in the password, consider the password SECRET. For this password,
the string SOMECHORESARETOUGH is a valid message. In contrast, the string SOMECHEERSARETOUGH
is not a valid message, because an extraneous E is found at the point when an R is first expected.
Input
The input consists of a single line containing two strings. The first string is the password, having length P,
with 3 P 8. The second string has length S, with 10 S 40. Both strings will consist solely of
uppercase letters. (That is, neither string can include whitespace, lowercase letters, digits, or other special
characters.)
Output
Output a single line with the word PASS if the second string is a valid message for the password, or FAIL
otherwise.
题目详情见Source的链接里
2016 UESTC ACM Summer Training Team Selection (1)
ACM-ICPC 2015 Mid-Central Regional Problem D: Hidden Password
My Solution
先用map记录下来password中每个字母出现的次数;
然后去历遍message,如果当前访问的不是正在查找的,并且在map中的记录里不是0,那么FAIL
如果是则,map里想要的字母--,然后查找password中的下一个字母
复杂度 O(n)
#include <iostream> #include <cstdio> #include <string> #include <map> using namespace std; map<char, int> pw; int main() { #ifdef LOCAL freopen("a.txt", "r", stdin); #endif // LOCAL string password, line; cin>>password; cin>>line; int sz1 = password.size(); int sz2 = line.size(); int ptr = 0, fall = 1; for(int i = 0; i < sz1; i++){ pw[password[i]]++; } for(int i = 0; i < sz2; i++){ if(line[i] == password[ptr]){ pw[password[ptr]]--; ptr++; if(ptr == sz1){ fall = 0; break; } } else{ if( pw[line[i]] != 0){ fall = 1; break; } } } if(fall) printf("FAIL"); else printf("PASS"); return 0; }
------from ProLights