On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.
Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.
Input Specification:
Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or _
(representing the space). It is guaranteed that both strings are non-empty.
Output Specification:
For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.
Sample Input:
7_This_is_a_test
_hs_s_a_es
Sample Output:
7TI
解题思路:双指针法,当两字符串对应字符相同,则两指针同时加一,若不同则s1的指针加一;当不同时,用visit进行hash映射判断是否已经判断过。另外在开始处理是s2多加了一个特殊字符,是为了s1后面存在s2没有的字符。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
string s1,s2;
cin>>s1>>s2;
transform(s1.begin(), s1.end(),s1.begin(),::toupper);
transform(s2.begin(), s2.end(),s2.begin(),::toupper);
int i = 0, j = 0;
std::vector<bool> visit(256,false);
s2 +='#';
while(i<s1.length()){
if(s1[i] != s2[j]){
if(visit[s1[i]] == false)
printf("%c",s1[i]);
visit[s1[i]] = true;
}
else
j++;
i++;
}
}