题目大意:给你n个字符串,如果一个字符串可以通过重新排列变成另一个字符串,则认为这两个字符串相等。每输入一个字符串,输出这个字符串和与它相等的之前出现了几次。
解题思路:我们可以用map保存一个字符串出现的次数,对于每个读进来的串,先把它排序一遍即可。由于字符串长度最大只有40,排序时间几乎可以忽略。
于是时间复杂度就只有map的$O(n\log n)$了。
C++ Code:
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
using namespace std;
map<string,int>p;
int n;
int main(){
ios::sync_with_stdio(0);
cin>>n;
string s;
while(n--){
cin>>s;
sort(s.begin(),s.end());
printf("%d\n",p[s]++);
}
return 0;
}