I did it once before, because I didn't know what happened to the one-line input in ACM mode, and then I looked at the answer and gave up, because the form of one-line input for several questions was different, and I was very tired~
It has been a long time since I practiced the test questions, and I took it out and wrote it again. I suddenly found it to be very simple, and summarized the format of a line of input:
1. getline(cin,s) is generally applicable to all strings and will read spaces;
2.while(cin>>s) is generally suitable for data of different types, especially the beginning and end of a line are numbers. At this time, you need to select multiple inputs, first cin>>num and then while(cin>>s) and then cin>>num, each space or carriage return will be used as a terminator to terminate this input.
Another uncomfortable point in this ACM is that the output must have line breaks and spaces!
Here is the code:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
void isBrother(vector<string>vec,string ss, int num)
{
vector<string>vv;
string ssss = ss;
sort(ssss.begin(), ssss.end());
for (int i = 0; i < vec.size(); i++)
{
if (vec[i].length() != ss.length()) continue;
string sss = vec[i];
sort(sss.begin(), sss.end());
if (sss != ssss||vec[i]==ss)continue;
vv.push_back(vec[i]);
}
sort(vv.begin(), vv.end());
if (vv.size()==0) cout << vv.size();
else
{
cout << vv.size()<<endl;
cout << vv[num - 1];
}
}
int main()
{
int num;
cin >> num;
vector<string>v;
string s;
string ss;
int num1;
for(int i=1;i<=num;i++)
{
cin >> s;
v.push_back(s);
}
cin >> ss;
cin >> num1;
isBrother(v, ss, num1);
return 0;
}
The naming of the strings here is a bit messy, and the code also needs to be improved, but it will not be changed for the time being. After all, the road to brushing questions is endless~~
ps:
- In C++, strings can be directly compared in size, that is, "ABC" and "ACD" - first than A, A is equal to B, C is larger than B ==> ACD> ABC, but in C language, characters need to call library functions strcmp(str1, str2), the string can be added with a loop, equal to return 0, the front is larger than the back and returns 1, and the front is smaller than the back and returns -1;
- C language judges equality with strncmp ( const char * str1, const char * str2, n);
- sort() is directly lexicographical sorting, and must include the header file #include<alogorithm>
The first two points may be designed to distinguish between MFC, ATL, and STL. I probably looked at it and read it carefully when I have time~