Topic describes
Given n strings, arrange n strings in lexicographic order.
The input describing
Enter the first action with a positive integer N (1≤n≤1000), and the following action with n strings (string length ≤100), which contain only upper and lower case letters.
Output description
The data outputs n lines of a lexicographical string.
The sample
Type :
9
cap
to
cat
two
too
up
boat
boot
boat
boot
cap
card
cat
to
too
two
up
Train of thought
The container Set can insert elements into the container at any time, quickly find elements at any time, and need to be traversed in some order. Multiset is an extension of set. Compared with set, multiset contains duplicate elements, that is, set considers key-like elements as the same element, and for counting +1, multiset considers key-like elements as equivalent elements and stores them in turn. Insert the input word into the container multiset in some order, and then print out the elements in the container.
#include <iostream>
#include <set>
using namespace std;
int main(){
int n;
cin>>n;
string input;
multiset<string> arr;
while(n--){
cin>>input;
arr.insert(input);
}
for(auto myarr:arr){
cout<<myarr<<endl;
}
return 0;
}
Using the container Vector, input strings are pressed into the container one by one, and the elements in the container are sorted after all are pressed.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(string str1,string str2){
return str1<str2;
}
int main(){
int n;
cin>>n;
string input;
vector<string> arr;
while(n--){
cin>>input;
arr.push_back(input);
}
sort(arr.begin(),arr.end(),compare);
for(int i=0;i<arr.size();i++){
cout<<arr[i]<<endl;
}
return 0;
}
Read More:
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- [Two Sigma OA] Longest Chain
- Module build failed: TypeError [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string.
- JavaScript realizes the longest substring of non repeated characters
- After introducing sass into Vue project, start to report error typeerror [err]_ INVALID_ ARG_ Type]: the “path” argument must be of type string
- invalid connection string format, a valid format is host:ip:port
- Error failed to compile with 1 errors after finding NPM run dev
- TSLint:object access via string literals is disallowedtslint(no-string-literal)
- Unity short string intercepts long string
- Error: /usr/bin/python: Error while finding module specification for ‘virtualenvwrapper.hook_loader’
- sys.path.append(), os.path.exists(), os.path.join() and os.makedirs() meaning
- Relative path and absolute path${ pageContext.request.contextPath }
- C++ foundation — clear/erase/pop of string class_back
- Codeworks educational round 96 [reverse pair] E. string reverse
- ClobberError: The package ‘xxx‘ cannot be installed due to a path collision for ‘xx‘ This path alre
- Chrome your connection is not a private connection solution
- Error:Connection activation Failed: no suitable device found for this connection solution
- hdfs 192.168.2.19:9000 failed on connection exception: java.net.ConnectException:Connection refused
- JDBC connection to Oracle: listener used the connection with the following error: ora-12505
- Windows_ Win7 broadband connection prompt remote access connection manager error