1033 旧键盘打字 C++

1033 旧键盘打字

str1是坏掉的键盘,str2是本要输出的字符串
考虑到str2会很长的情况下,就只循环一次str2,先是用的for循环,发现str2的长度在erase的过程中会变短,就用while(!str2.empty())循环,str2[count]代表当前字符,若当前字符有被去掉,去掉后count不变,继续判断下一个字符,若没有去,则count++继续下一个判断。

#include<iostream>
#include <string>
// 补充  npos 是一个常数,用来表示不存在的位置 
using namespace std;
string remove(string str1,string str2)
{
	//先判断是否有"+"
	bool flag=false;
	int count=0;
	if((str1.find('+',0) != string::npos))
	{
		flag=true;
	}			
		bool iden=false;
		while(!str2.empty()) 
		{
			if(count==str2.size())  break;
			if(flag==true)
			{
				if((str2[count]>='A'&&str2[count]<='Z'))
				{
					str2.erase(count,1);
					iden=true;
				}
	            else{
	            	for(int j=0;j<str1.size();j++)
	            	{
	            		if(str2[count]==tolower(str1[j]))
	            		{
	            			str2.erase(count,1);
	            			iden=true;
						}
					}
				}
			}
			else{
				for(int j=0;j<str1.size();j++)
	            	{
	            		if(tolower(str2[count])==tolower(str1[j]))
	            		{
	            			str2.erase(count,1);
	            			iden=true;
						}
					}
			}
			if(iden==false)
			{
				count++;
			}
			iden=false;
		}
		return str2;
} 
int main(){
	string str1,str2,s1,s2,s;
	getline(cin,str1);  //坏掉 
	getline(cin,str2);  //应当出现的字符 
    if(!str1.empty())
    {
    	str2=remove(str1,str2);
	}
	cout << str2;
} 

这个代码虽然AC了,但是最后一个测试点用了100多ms,过于长了
我在牛客网这道题的下面找到了一个大佬的代码
这道题的下面第一个
侵删
附上代码:

#include <stdio.h>
#include <ctype.h>
int main(){
 char A[123] = {0}, ch;
 while ((ch = getchar()) != '\n')
  A[ch]++;
 while ((ch = getchar()) != '\n')
  if ((isupper(ch) && A[43]) || A[toupper(ch)]) 
  continue;
  else
  putchar(ch);
 return 0;
}

最后一个测试点也保持在10ms以内
今天又学到了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值