首页 > 其他 > 详细

PAT Advanced 1050 String Subtraction (20) [Hash散列]

时间:2020-01-28 19:33:23      阅读:75      评论:0      收藏:0      [点我收藏+]

题目

Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer 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.
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.
Output Specification:
For each test case, print S1 – S2 in one line.
Sample Input:
They are students.
aeiou
Sample Output:
Thy r stdnts.

题意

  1. 输入s1,s2,打印s=s1-s2(即:打印s1中未出现在s2中的字符)
  2. s1,s2长度均<=10^4,所有字符都是ASCII码表中的字符,换行符表示输入结束

题目分析

  1. 定义int asc[256],记录s2中字符出现次数。
  2. 遍历s1,若asc[s1[i]]>0,说明在s2中出现,不打印

Code

Code 01

#include <iostream>
using namespace std;
int main(int argc, char * argv[]) {
    string s1,s2;
    getline(cin,s1);
    getline(cin,s2);
    int asc[256]= {0};
    for(int i=0; i<s2.length(); i++) {
        asc[s2[i]]++;
    }
    for(int i=0; i<s1.length(); i++) {
        if(asc[s1[i]]>0)continue;
        printf("%c",s1[i]);
    }
    return 0;
}


PAT Advanced 1050 String Subtraction (20) [Hash散列]

原文:https://www.cnblogs.com/houzm/p/12238628.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!