浙大pat | 1058 Broken Keyboard (20)字符串

题目描述

On a broken keyboard, some of the keys are worn out. So when youtype 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 youactually type out, please list those keys

which are for sure worn out.



输入描述:

Each input file contains one test case. For each case, the 1stline contains the original string, and the 2nd line contains the typed-outstring. 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.




输出描述:

For each test case, print in one line the keys that are wornout, 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 atleast one worn out key.



输入例子:

7_This_is_a_test

_hs_s_a_es



输出例子:

7TI

 

这题使用两个指针,一个指向原数组,一个指向打印出来的数组,每到第一个数组中的对应值和第二个数组中的对于值不相等的时候,说明第一个数组直到第一个和第二个数组对应值相等的值的所有按键都损坏了,第一个数组的指针一直向后移动直到找到第一个和第二个数组中指针对应的值相等的位置

当第一个数组和第二个数组中的对应值相等的时候,说明这个按键没有损坏,两个指针都向后移动一位

这一题有好几点需要注意

1)’_’按键可能损坏,这个时候需要特别处理

2)同一个字母的大小写算作是同一个按键

3)原始数组可能从某一个值开始,后面的所有按键全部损坏

以后在做题的时候要记住,不要把while写成if!!

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string theStringStart;
	string theStringAfter;
	cin >> theStringStart >> theStringAfter;
	int indexOne=0, indexTwo=0;
	int p;
	bool checkItem[37] = { false };
	vector<char> whatLost;
	while (indexOne < (int)theStringStart.size() && indexTwo < (int)theStringAfter.size())
	{
		if (theStringStart[indexOne] != theStringAfter[indexTwo])
		{
			if (theStringStart[indexOne] == '4')
			{
				int j_makr = 1;
			}
			if (isdigit(theStringStart[indexOne]))
			{
				p = theStringStart[indexOne] - '0';
			}
			else if(isupper(theStringStart[indexOne]))
			{
				p = theStringStart[indexOne] - 'A' + 10;
			}
			else if(islower(theStringStart[indexOne]))
			{
				p = theStringStart[indexOne] - 'a' + 10;
			}
			else
			{
				p = 36;
			}
			if (!checkItem[p])
			{
				checkItem[p] = true;
				if(p<=9)
				whatLost.push_back(p+'0');
				else if(p>9&&p<36)
				{
					whatLost.push_back(p-10 + 'A');
				}
				else
				{
					whatLost.push_back('_');
				}
			}
			indexOne++;
		}
		else
		{
			indexOne++;
			indexTwo++;
		}
	}
	while (indexOne < (int)theStringStart.size())
	{
		if (isdigit(theStringStart[indexOne]))
		{
			p = theStringStart[indexOne] - '0';
		}
		else if (isupper(theStringStart[indexOne]))
		{
			p = theStringStart[indexOne] - 'A' + 10;
		}
		else if (islower(theStringStart[indexOne]))
		{
			p = theStringStart[indexOne] - 'a' + 10;
		}
		else
		{
			p = 36;
		}
		if (p == 3) {
			int uis = 1;
		}

		if (!checkItem[p])
		{
			checkItem[p] = true;
			if (p <= 9)
				whatLost.push_back(p + '0');
			else if (p>9 && p<36)
			{
				whatLost.push_back(p - 10 + 'A');
			}
			else
			{
				whatLost.push_back('_');
			}
		}
		indexOne++;
	}
	for (int i = 0; i < (int)whatLost.size(); i++)
	{
		cout << whatLost[i];
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值