zoukankan      html  css  js  c++  java
  • mp

    问题 G: Green Bin

    时间限制: 1 Sec  内存限制: 128 MB
    [提交] [状态]

    题目描述

    We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.
    For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.
    Given are N strings s1,s2,…,sN. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i,j (1≤i<j≤N) such that si is an anagram of sj.

    Constraints
    ·2≤N≤105
    ·si is a string of length 10.
    ·Each character in si is a lowercase English letter.
    ·s1,s2,…,sN are all distinct.

    输入

    Input is given from Standard Input in the following format:

    N
    s1
    s2
    :
    sN

    输出

    Print the number of pairs of integers i,j (1≤i<j≤N) such that si is an anagram of sj.

    样例输入 Copy

    【样例1】
    3
    acornistnt
    peanutbomb
    constraint
    【样例2】
    2
    oneplustwo
    ninemodsix
    【样例3】
    5
    abaaaaaaaa
    oneplustwo
    aaaaaaaaba
    twoplusone
    aaaabaaaaa

    样例输出 Copy

    【样例1】
    1
    【样例2】
    0
    【样例3】
    4

    提示

    样例1解释
    s1= acornistnt is an anagram of s3= constraint. There are no other pairs i,j such that si is an anagram of sj, so the answer is 1.
    样例2解释
    If there is no pair i,j such that si is an anagram of sj, print 0.
    样例3解释
    Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.
    解析:

    题意概括:

    找出有这些单词所含的字母种类和数目都相等的对数。注意单词之间可以随意组合,因此要用到排列的知识。

    解题思路:

    首先将字符串内的字符进行从小到大排列,这样就很容易比较两个字符串是否满足题意要求。使用map容器记录相同单词数,最后使用排列知识算出总个数。给你一些字符串,判断其中有多少对字符串调换字符位置后会得到一样的字符串(组成字母及数目一样)
    由组合数知识知n个相同组成的字符串可以构成C2n对,即n*(n-1)/2

    如果是这样定义

    mp[99] = 1000000;

    it->first就是下标,即99
    it->second是值,即1000000

    AC代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<map> 
    #include<bits/stdc++.h> 
    using namespace std;
    typedef long long ll; 
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int maxn=1e6;
    string a;
    map<string,ll>mp;
    int n;
    void inint(){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a;
            sort(a.begin(),a.end());
            mp[a]++;
        }    
    } 
    int main(){
        inint();
        ll sum=0;
        for(auto i=mp.begin();i!=mp.end();i++){
            ll s=i->second;
            sum+=(s*(s-1))/2;
        }
        printf("%lld",sum);
    }

    mp的解析:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<map> 
    #include<bits/stdc++.h> 
    using namespace std;
    typedef long long ll; 
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int maxn=1e6;
    string a;
    map<string,ll>mp;
    int main(){
        cin>>a;
        mp[a]=1;
        auto i=mp.begin(); 
        cout<<i->first<<endl;//输入的字符串 
        cout<<i->second;//下标 
    }

    输出:

  • 相关阅读:
    [Ceoi2016|BZOJ4936] Match
    下载/拷贝下来的压缩包打开内容为空解决方案
    [POI2012]OKR-A Horrible Poem
    [SNOI2017]礼物
    LuoguP3398 仓鼠找sugar
    转:看图说话Image Caption之评价指标、NIC(Neural Image Caption)模型和attention
    转:Cascade R-CNN,一个使你的检测更加准确的网络
    图像理解之物体检测object detection,模型rcnn/fastrcnn/fasterrcnn原理及概念
    深度残差网络
    转:图像分类、物体检测、物体分割、实例分割、语义分割
  • 原文地址:https://www.cnblogs.com/lipu123/p/12288394.html
Copyright © 2011-2022 走看看