A. Aramic script

A. Aramic script
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.
  • 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?

Input

The first line contains one integer nn (1n1031≤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

Output one integer — the number of different objects mentioned in the given ancient Aramic script.

Examples
input
Copy
5
a aa aaa ab abb
output
Copy
2
input
Copy
3
amer arem mrea
output
Copy
1
Note

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".



题意:输入n个字符串,在阿拉伯语言中对每个字符串去重后,就是一个根对象,让你输出有多少根对象?比如样例一,a,aa,aaa,ab,abb,去重后的是字符串是a,a,a,ab,ab。所以只有a,ab两个对象。还有就是题目给的字符串是可以重新排列的,排序后去重再算,比如样例二:按照字典序排序后都是aemr,所以只有一个。


题解:昨晚比赛的时候,用了map<string,int>标记去重后的字符,vis数组标记的是当前字符串重复的字母。其实还可以简化,STL中不是有set吗,加进去的东西会自动去重排序的。这样一来简化思路和代码,但是这样一个一个放,还是有点麻烦的,可以放整个string,有个去重的unique就更方便了。提供三种写法:代码长度一次递减

map<string,int>,vis数组标记代码:
#include<bits/stdc++.h>
using namespace std;
map<string,int>m;
int vis[1010];
int main()
{
    int n,ans=0;
    string s,b;
    cin>>n;
    while(n--)
    {
        memset(vis,0,sizeof(vis));
        cin>>s;
        b.clear();
        sort(s.begin(),s.end());
        for(int i=0; i<s.size(); i++)
            if(s[i]==s[i+1])
                vis[i]=1;
        for(int i=0; i<s.size(); i++)
            if(!vis[i])
                b+=s[i];
        if(!m[b])
            ans++;
        m[b]=1;
    }
    cout<<ans<<endl;
    return 0;
}

map标记加set用法,单个char插入set,会自动去重排序。不知道set用法的这里有传送门: https://blog.csdn.net/memory_qianxiao/article/details/79603977
#include<bits/stdc++.h>
using namespace std;
map<string,int>m;
set<char>se;
int main()
{
   int n,ans=0;
   string s,a;
   cin>>n;
   while(n--)
   {
       cin>>s;
       a.clear();
       se.clear();
       for(int i=0;i<s.size();i++)
        se.insert(s[i]);
       for(set<char>::iterator it=se.begin();it!=se.end();it++)
        a+=*it;
        if(!m[a])
            ans++;
        m[a]=1;
   }
   cout<<ans;
    return 0;
}

set用法+unique用法,unique,去重,但不是真正去重,而是把重复的放在后面返回重复第一个下标,如果没记错的话,还不知道unique去重原理问度娘。
#include<bits/stdc++.h>
using namespace std;
set<string>se;
int main()
{
    int n;
    string s;
    cin>>n;
    while(n--)
    {
        cin>>s;
        sort(s.begin(),s.end());
        s.erase(unique(s.begin(),s.end()),s.end());
        se.insert(s);
    }
    cout<<se.size();
    return 0;
}




 

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/80161640