The problem that the output of the local IDE and Nioke is different

 

 

 Some people say that it is the problem of while(cin>>s){}, and some people say that it is the problem of global variable initialization.

——————————————————————————————————————————————————————————————————————————————————————————————————————

Finally, the final code is easy to understand, but the initial code still does not know why it reports an error:

Final code:

#include<iostream>
#include<string>
#include<unordered_set>
//C/C++库函数 
// tolower() 实现字母的小写转换
// toupper() 实现字母的大写转换

// isalpha() 判断一个字符是否为字母,是返回非0,否返回0
// isalnum() 判断一个字符是否为数字或者字母,是返回非0,否返回0
// islower() 判断一个字符是否为小写字母,是返回非0,否返回0
// isupper() 判断一个字符是否为大写字母,是返回非0,否返回0
using namespace std;
void tranfer(string s,string keys)
{
	string s1;
	string result;
	unordered_set<char>str1;
	string str2 = "abcdefghijklmnopqrstuvwxyz";
	for (auto str : s)
	{
		if (str1.count(str))continue;
		str1.insert(str); s1 += str;
	}
	for (int i = 0; i < 26; i++)
	{
		if (str1.count(i + 'a'))continue;
			s1 += (i + 'a');
	}
	for (auto str : keys)
	{
		for (int i = 0; i < 26; i++)
		{
			if (str2[i] == tolower(str))
			{
				if (islower(str))
				{
					result += s1[i];
				}
					
				else
					result += toupper(s1[i]);
			}
		}	
	}
	cout << result;
}
int main()
{
	string s;
	string keys;
	while (cin>>s)
	{
        cin>>keys;
		tranfer(s,keys);
	}
	return 0;
}

Because the cases in Niu Ke are all lowercase, this step can be simplified

for (auto str : keys)
    {         for (int i = 0; i < 26; i++)         {             if (str2[i] == tolower(str))             {                 if (islower(str))                 {                     result += s1[i];                 }                 else                     result += toupper(s1[i]);             }         }         }












Simplified result:

 string res="";

for(int i=0;i<s.size();i++){ //遍历判断

  int j=s[i]-'a';

  res+=x[j];

  }

Initial code: verified not to be a problem with cin

#include<iostream>
#include<string>
#include<unordered_set>
using namespace std;
void tranfer(string s)
{
	string s1;
	string s2;
	string result;
	unordered_set<char>str1;
	string str2 = "abcdefghijklmnopqrstuvwxyz";
	for (auto str : str2)
		str1.insert(str);
	for (auto str : s)
	{
		if (str1.find(str) != str1.end())
		{
			s1 += str;
			str1.erase(str);
		}
	}
	for (auto str : str1)
		s1 += str;
	string keys;
	cin >> keys;
	for (auto str : keys)
	{
		for (int i = 0; i < 26; i++)
		{
			if (str2[i] == tolower(str))
			{
				if (islower(str))
					result += s1[i];
				else
					result += toupper(s1[i]);
			}
		}	
	}
	cout << result;
}
int main()
{
	string s;
	while (cin>>s)
	{
		tranfer(s);
	}
	return 0;
}

———————————————Demarcation line —————————————————————

I have a clue, it may be caused by unordered_set<char>str!

unordered_set<char>str The input data  is not repeated and unordered after output.

 It has nothing to do with the order of input, because the underlying implementation principle is a hash table, so the order of output is affected by the hash function.

 Among them, when encountering repeated character insertion, the result obtained by selecting skip check statement and not skipping is the same.

 

finally:

c++ stdThe difference between the middle setand the unordered_setdifference is similar to the difference between:mapunordered_map

  1. setBased on the implementation of the red-black tree, the red-black tree has the function of automatic sorting , so all the data inside the map is in order at any time.
  2. unordered_setBased on the hash table, the time complexity of data insertion and search is very low, almost constant time, but the cost is more memory consumption and no automatic sorting function . In the underlying implementation, an array with a relatively large subscript range is used to store elements, forming many buckets, and using hashfunction pairs keyto map them to different areas for storage.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324161164&siteId=291194637