PAT (Advanced Level) Practice 1050 String Subtraction (20 分) 凌宸1642
题目描述:
Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1−S2 for any given strings. However, it might not be that simple to do it fast.
译:给定两个字符串 S1 和 S2, ,S = S1−S2 被定义为从S1中取出S2中含有的字符后剩下的字符串 。你的任务就是简单的计算任意给定字符串的 S1−S2 。然而,要想 快速 做到这一点可能没那么简单。
Input Specification (输入说明):
Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.
译:每个测试文件包含一个测试用例,每个用例包含两行给定的 S1 和 S2。两个字符串的长度都不超过10 4。它保证所有字符都是可见的ASCII码和空白,并且一个换行字符表示字符串的结束。
Output Specification (输出说明):
For each test case, print S1−S2 in one line.
译:对于每个测试用例,在 M 行中打印相应那对出口之间的最短距离 。
Sample Input (样例输入):
They are students.
aeiou
Sample Output (样例输出):
Thy r stdnts.
The Idea:
其实就是简单的哈希,就是直接将 S 2 中的每一个字符用一个数组进行标记,然后遍历 S 1 ,若 S 1中的字符的标志是 true 的话,则说明在 S 2 中出现过,否则直接输出。
The Codes:
#include<bits/stdc++.h>
using namespace std;
string s1 , s2 ;
bool flag[200] ;
int main(){
getline(cin , s1) ;
getline(cin , s2) ;
for(int i = 0 ; i < s2.size() ; i ++) flag[s2[i]] = true ; // 标记 S2中的每一个自发的
for(int i = 0 ; i < s1.size() ; i ++) if(!flag[s1[i]]) cout<<s[i] ;//如果未被标记,则输出
return 0;
}