1. string a;
对 a 进行排序:sort( a.begin(), a.end() );
2. string a[n];
对 a[n] 进行排序: sort(a, a+n) 。
因为 string 类对 '>' ,'==', '<' 这些比较运算符进行了重载。
下面一道题验证:
In Aramic language words can only represent objects.
Words in Aramic have special properties:
- A word is a root if it does not contain the same letter more than once.
- A root and all its permutations represent the same object.
- The root xx of a word yy is the word that contains all letters that appear in yy in a way that each letter appears once. For example, the root of "aaaa", "aa", "aaa" is "a", the root of "aabb", "bab", "baabb", "ab" is "ab".
- Any word in Aramic represents the same object as its root.
You have an ancient script in Aramic. What is the number of different objects mentioned in the script?
The first line contains one integer nn (1≤n≤1031≤n≤103) — the number of words in the script.
The second line contains nn words s1,s2,…,sns1,s2,…,sn — the script itself. The length of each string does not exceed 103103.
It is guaranteed that all characters of the strings are small latin letters.
Output one integer — the number of different objects mentioned in the given ancient Aramic script.
5 a aa aaa ab abb
2
3 amer arem mrea
1
In the first test, there are two objects mentioned. The roots that represent them are "a","ab".
In the second test, there is only one object, its root is "amer", the other strings are just permutations of "amer".
参考代码:
#include <iostream> #include <cstring> #include <algorithm> using namespace std; string a[1010]; string b[1010]; int main() { int n; cin >> n; for(int i=0; i<n; i++) { cin >> a[i]; int len = a[i].size(); sort(a[i].begin(), a[i].end()); int t = 0; b[i] += a[i][t]; for(int j=1; j<len; j++) { if(a[i][j] != a[i][j-1]) { b[i] += a[i][j]; } } // cout << b[i] << endl; } sort(b, b+n); int ans = 1; for(int i=1; i<n; i++) { // cout << b[i] << endl; if(b[i] != b[i-1]) ans ++; } cout << ans << endl; return 0; }