PAT A1084 Broken Keyboard

PAT A1084 Broken Keyboard(同B 1029)

在这里插入图片描述

Sample Input:

7_This_is_a_test
_hs_s_a_es

Sample Output:

7TI
wordmeaning
worn out用坏(磨损)
being detected被发现
  • 思路 1:打表法(hash)
    两个指针同步遍历两个字符串,遇到不一样的开始处理:先把小写转大写,再c - '0',将字符转化为int映射到数组has[](初始化为0),若has[i] == 0打印出来,对应下标++;
  • code 1:
#include <string>
#include <iostream>
#include <stdio.h>
#include <cctype>	// *
using namespace std;
const int maxn = 128;
int has[maxn] = {0};
int main(){
	string s1, s2;
	cin >> s1 >> s2;
	int i = 0, j = 0;
	while(i != s1.size()){
		if(s1[i] == s2[j]){
			i++;j++;
		}
		else{
			if(islower(s1[i])) s1[i] = toupper(s1[i]);	//TIPS 1:
			int x = s1[i] - '0';
			if(has[x] == 0) printf("%c", s1[i]); 
			has[x]++;
			i++;
		}
	}
}
  • TIPS 1:
    ctype.h中的函数:
函数功能用法
islower(c)判断c是否是小写字母if(islower(c))
isupper(c)判断c是否是大写字母if(isupper(c))
toupper(c)将c转化为大写(!!:返回的是int型)char c = toupper(c)
tolower(c)将c转化为小写char c = tolower(c)
isalpha(c)大小写字母-
isalnum(c)大小写字母+数字-
isblank(c)space、\t-
isspace(c)space、\t、\r、\n-
  • T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 255;
bool has[maxn];

int main(){
	string ori, real;
	cin >> ori >> real;
	for(int i = 0; i < real.size(); ++i){
		int x = (int)real[i];
		if(islower(real[i])) real[i] = toupper(real[i]);
		has[x] = true;
	}
	for(int i = 0; i < ori.size(); ++i){
		int x = (int)ori[i];
		if(islower(ori[i])) ori[i] = toupper(ori[i]);
		if(has[x] == false){
			printf("%c", ori[i]);
			has[x] = true;
		} 
	}
	return 0;
} 
  • T3 code:
#include <bits/stdc++.h>
using namespace std;
bool has[300];
int main()
{
    string s1, s2;
    cin >> s1 >> s2;
    for(int i = 0; i < s2.size(); ++i)
    {
        char tmp = islower(s2[i]) ? toupper(s2[i]) : s2[i];
        has[tmp] = true;
    }

    for(int i = 0; i < s1.size(); ++i)
    {
        char tmp = islower(s1[i]) ? toupper(s1[i]) : s1[i];
        if(has[tmp] == false)
        {
            printf("%c", tmp);
            has[tmp] = true;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值