Codeforces Round #478 (Div. 2) A. Aramic script

A. Aramic script

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

题意:给你n个单词,这些单词中,只要含有相同的字母种类,且排列不限,这个种类即为词根,问你有多少个词根。

思路:开一个数组将该单词字母种类记录一下,然后构造出词根用map<string,int>标记即可,如果存在过这个词根,种类不需要加一,如果不存在过,先标记上然后加一。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define exp 1e-8
#define mst(a,k) memset(a,k,sizeof(a))
using namespace std;
string s;
map<string,ll>mp;
ll n,vis[33],ans;
int main()
{
    while(~scanf("%lld",&n))
    {
        mp.clear();
        ans=0;
        for(ll i=1;i<=n;i++)
        {
            cin>>s;
            mst(vis,0);
            for(ll i=0;i<s.size();i++)   //标记出字母种类
            {
                vis[s[i]-'a'+1]=1;  
            }
            string s1;
            ll p=0;
            for(ll i=1;i<=26;i++)
            {
                if(vis[i])
                {
                    s1.insert(p++,1,i+'a'-1);   //字符串函数,从p位置开始插入k个字符c
                }
            }
            if(mp[s1]==0)   //标记
            {
                mp[s1]=1;
                ans++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/s540239976/article/details/80283305