问题 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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=1e6;
string a;
map<string,ll>mp;
int n;
void inint(){
cin>>n;
for(int i=;i<n;i++){
cin>>a;
sort(a.begin(),a.end());
mp[a]++;
}
}
int main(){
inint();
ll sum=;
for(auto i=mp.begin();i!=mp.end();i++){
ll s=i->second;
sum+=(s*(s-))/;
}
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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=1e6;
string a;
map<string,ll>mp;
int main(){
cin>>a;
mp[a]=;
auto i=mp.begin();
cout<<i->first<<endl;//输入的字符串
cout<<i->second;//下标
}

输出:

mp的更多相关文章

  1. [公告]Senparc.Weixin.MP v14.2.1 升级说明

    在Senparc.Weixin.MP v14.2.1中,所有Senparc.Weixin.MP下的Container,命名空间已经从 Senparc.Weixin.MP.CommonAPIs 改为了  ...

  2. Senparc.Weixin.MP SDK 微信公众平台开发教程(十八):Web代理功能

    在Senparc.Weixin.dll v4.5.7版本开始,我们提供了Web代理功能,以方便在受限制的局域网内的应用可以顺利调用接口. 有关的修改都在Senparc.Weixin/Utilities ...

  3. Senparc.Weixin.MP SDK 微信公众平台开发教程(十七):个性化菜单接口说明

    前不久微信上线了个性化菜单接口,Senparc.Weixin SDK也已经同步更新. 本次更新升级Senparc.Weixin.MP版本到v13.5.2,依赖Senparc.Weixin版本4.5.4 ...

  4. 微信小程序 不在以下合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html

    微信小程序  不在以下合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html 友情提示: 大家 ...

  5. 微信公众账号 Senparc.Weixin.MP SDK 开发教程 索引

    Senparc.Weixin.MP SDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到比较稳定的版本,这个过程中我觉得有必要整理一些思路和经验,和大家一起分享. ...

  6. Senparc.Weixin.MP SDK 微信公众平台开发教程(三):微信公众平台开发验证

    要对接微信公众平台的"开发模式",即对接到自己的网站程序,必须在注册成功之后(见Senparc.Weixin.MP SDK 微信公众平台开发教程(一):微信公众平台注册),等待官方 ...

  7. Senparc.Weixin.MP SDK 微信公众平台开发教程(四):Hello World

    =============  以下写于2013-07-20 ============= 这一篇文章其实可以写在很前面,不过我还是希望开发者们尽多地了解清楚原理之后再下手. 通过上一篇Senparc.W ...

  8. Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK

    Senparc.Weixin.MP SDK已经涵盖了微信6.x的所有公共API. 整个项目的源代码以及已经编译好的程序集可以在这个项目中获取到:https://github.com/JeffreySu ...

  9. Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler

    上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK>我们讲述了如何使用Senparc.Weixin.MP SDK ...

  10. Senparc.Weixin.MP SDK 微信公众平台开发教程(七):解决用户上下文(Session)问题

    从这篇文章中我们已经了解了微信公众平台消息传递的方式,这种方式有一个先天的缺陷:不同用户的请求都来自同一个微信服务器,这使得常规的Session无法使用(始终面对同一个请求对象,况且还有对方服务器Co ...

随机推荐

  1. IIS7配置asp程序

    Windows 中IIS7.IIS7.5是默认不安装的,所以在安装完Windows Vista/windows 7/windows 2008之后如果需要安装IIS7/iis7.5的话,就要自己动手了. ...

  2. openlayers显示区域

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. codechef Counting D-sets

    难度 \(hard\) 题意 官方中文题意 做法 为避免数重,强制每维至少有一个\(0\),这个可以简单容斥 直径恰好为\(d\),不好求,也容斥一下

  4. PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) (找最值)

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  5. 浅识mysql主键

    primary key:主键,又叫主键约束. primary key在表中是唯一代表一条记录的.primary key可以是1列,或者多列组合而成的. 如何查看一个表的主键是什么,举个例子: desc ...

  6. 7-5 A除以B(10 分)

    真的是简单题哈 —— 给定两个绝对值不超过100的整数A和B,要求你按照“A/B=商”的格式输出结果. 输入格式:输入在第一行给出两个整数A和B(−100≤A,B≤100),数字间以空格分隔. 输出格 ...

  7. STL中_Rb_tree的探索

    我们知道STL中我们常用的set与multiset和map与multimap都是基于红黑树.本文介绍了它们的在STL中的底层数据结构_Rb_tree的直接用法与部分函数.难点主要是_Rb_tree的各 ...

  8. laravel封装返回json信息工具类

    1.工具类可以一次写入多方多方调用,很大程度的节约开发时间得到想要的信息 这里演示一个json接口的工具类(文件定义在App\Http\Controllers\Tools)中 <?php /** ...

  9. centos 安装桌面

    centos7.*安装 1,安装 yum groupinstall "KDE Plasma Workspaces" 2.启动 startx

  10. CTF入门 |“男神”背后的隐写术

    刚刚开始学CTF,记录一下做的第一道隐写题 ~ 附件下载 题目背景(我自己瞎编的): Luyu是CPPU的校草,一直以来他的写真照被各届校友广泛流传,最近江湖上流传着拿到这些照片就能知道Luyu的QQ ...