字符串2题

新字典序

题目描述
卷子上的每道题都是一样的内容:给你两个英语单词,和一份新的字典序,让你判断一下两个英语单词的大小。

example: 两个单词在比较大小时,会从首字母开始比起,如果两个字母相等则继续比较下一个,如果不等则按照字典序判断大小。举例来说 world 和 wolf 比较:第一次比较首字母,两个单词的首字母都是 w,故继续比较第二个字母。两个单词的第二个字母都是 o,故继续比较第三个字母。r 在字典序中是大于 l 的,因此 world 是大于 wolf 的。

输入格式:
第一行有一个整数 n ,表示卷子上题目的数量。
第二行有26个不同的英文小写字母(a - z)(a−z),之间以空格分割,表示新的字典序。
接下来的 n 行,每行有两个字符串。
输出格式:
对于每一道题,在一行输出其答案: 如果 s1 <s2 输出 “<”;
如果s1>s2 输出 “>”,否则输出 “=”。
在这里插入图片描述
p.s.:1≤n≤1000
1 x 1 x 2 1000 1≤ |x_1|、|x_2| ≤ 1000

嘤!?没坑啊……

#include<bits/stdc++.h>
using namespace std;
map<char,char>arr;
int main()
{
	int n;
	char c;
	cin>>n;
  	for(int i = 'a';i <= 'z';i++)
  	{
  		cin>>c;
  		arr[c] = i;//注意这个循环是用的a - z的循环,不是1 - 26,我写的时候用的后者,当与s字符串有关的时候 后面的s1[i]+-的时候有问题
	}
	for(int j = 0;j < n;j++)
	{
		string s1,s2;
		cin>>s1>>s2;
		for(int i = 0;i < s1.size();i++)
		{
			s1[i] = arr[s1[i]];
		}
		for(int i = 0;i < s2.size();i++)
		{
			s2[i] = arr[s2[i]];
		}
		if(s1 > s2)
		{
			cout<<">"<<endl;
		}
		else if(s1 == s2)
		{
			cout<<"="<<endl;
		}
		else
		{
			cout<<"<"<<endl;
		}
	}
  	return 0;
}

mdltxdy

在这里插入图片描述
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s,t;
	while(getline(cin,s))
	{
		int len = s.size();
		t = s;
		for(int i = 0;i < len;i++)
		{
			s[i] = tolower(s[i]);//除了mdltxdy别的不能换成小写字母,所以要用一个另一个字符串进行改变
		}
		int pos = 0;
		int cnt = 0;
		while((pos = s.find("mdltxdy",pos)) != string::npos)
		{
			t.replace(pos,7,"ldltxdy");
			cnt++;
			pos += 7;
		}
		cout<<t<<endl;
	}
	return 0;
}

暴力解法:
我以为和txdy四个字母没关系
在这里插入图片描述

发布了60 篇原创文章 · 获赞 22 · 访问量 6982

猜你喜欢

转载自blog.csdn.net/weixin_43786143/article/details/88982636
今日推荐