1033 旧键盘打字(20 分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?

输入格式:

输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 10​5​​ 个字符的串。可用的字符包括字母 [a-zA-Z]、数字 0-9、以及下划线 _(代表空格)、,.-+(代表上档键)。题目保证第 2 行输入的文字串非空。

注意:如果上档键坏掉了,那么大写的英文字母无法被打出。

输出格式:

在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。

输入样例:

7+IE.
7_This_is_a_test.

输出样例:

_hs_s_a_tst

分析:原来写了一个程序,测试样例通过了,但是其余的例子都没有通过;

// B33.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
    string a;
    string b;
    cin >> a >> b;
    for (int i = 0; i < a.length(); i++)
    {
        if (a[i] >= 'A' && a[i] <= 'Z')
        {
            b.erase(std::remove(b.begin(), b.end(), a[i]), b.end());
            b.erase(std::remove(b.begin(), b.end(), a[i] + 32), b.end());
        }
        else if (a[i] >= '0' && a[i] <= '9')
        {
            b.erase(std::remove(b.begin(), b.end(), a[i]), b.end());
        }
        else if (a[i] = '+')
        {
            for (int  i = 0; i < 26; i++)
            {
                b.erase(std::remove(b.begin(), b.end(), 'A' + i), b.end());
            }
        }
        else if (a[i] = '.')
        {
            b.erase(std::remove(b.begin(), b.end(), '.'), b.end());
        }
        else
        {
            b.erase(std::remove(b.begin(), b.end(), a[i]), b.end());
        }
    }
    cout << b << endl;
    system("pause");
    return 0;
}

 1.首先百度到:

这题和之前一套题的旧键盘很相似,也可以用哈希映射的思路来写,但是有一个很细节的1分,是测试点2,要考虑第一行不输入,也就是没有坏掉的键。

所以这里不能用scanf,要用gets来一行一行的读,否则一直回车是不会被存入s1的

2.tolower/toupper

需要引用的函数是ctype.h

函数原型:

int tolower(int c)
{
	if ((c >= 'A') && (c <= 'Z'))
		return c + ('a' - 'A');
	return c;
}
 
int toupper(int c)
{
	if ((c >= 'a') && (c <= 'z'))
		return c + ('A' - 'a');
	return c;
}

AC代码:


#include <iostream>
#include <string>
#include <algorithm>
 
using namespace std;
 
int ToUpper(int n){
	return toupper(n);
}
 
int ToLower(int n){
	return tolower(n);
}
 
int main(int argc, const char * argv[]) {
	string first, second, third;
	string upCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	getline(cin, first);
	getline(cin, second);
	third = second;
 
	if (first.find('+') == second.npos){
		transform(second.begin(), second.end(), third.begin(), ToUpper);
		upCase = "";
	}
	else{
		transform(first.begin(), first.end(), first.begin(), ToLower);
	}
 
	for (int i = 0; i<third.size(); ++i){
		if (first.find(third[i]) == first.npos && upCase.find(third[i]) == upCase.npos){
			cout << second[i];
		}
	}
 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/piaoliangjinjin/article/details/81480581