基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的Anigram,例如单词army和mary互为Anigram。现在给定一个字典,输入Q个单词,从给出的字典中找出这些单词的Anigram。

 
Input
第1行:1个数N,表示字典中单词的数量。(1 <= N <= 10000)
第2 - N + 1行,字典中的单词,单词长度 <= 10。
第N + 2行:查询的数量Q。(1 <= Q <= 10000)
第N + 3 - N + Q - 2行:用作查询的单词,单词长度 <= 10。
Output
共Q行,输出Anigram的数量,相同的2个单词不算Anigram,如果没有输出0。
Input示例
5
add
dad
bad
cad
did
3
add
cac
dda
Output示例
1
0
2 【分析】:一个map记录相同的(因为相同的2个单词不算Anigram,要减掉),一个记录排序后得到相同的单词。两者差值为Anigram的数量。
【代码】:
#include <bits/stdc++.h>

using namespace std;
#define inf 1e18+100
#define LL long long const int maxn = ; int main()
{
int n,nn,x,y;
char s[maxn],ss[maxn];
map<string,int>m1,m2;
cin>>n;
for(int i=;i<n;i++)
{
scanf("%s",s);
m1[s]++;
sort(s,s+strlen(s));
m2[s]++;
}
cin>>nn;
for(int i=;i<nn;i++)
{
scanf("%s",ss);
x=m1[ss];
sort(ss,ss+strlen(ss));
y=m2[ss];
cout<<y-x<<endl;
}
}

51nod 1095 Anigram单词【hash/map/排序/字典树】的更多相关文章

  1. 51Nod 1095 Anigram单词 | Hash

    Input示例 5 add dad bad cad did 3 add cac dda Output示例 1 0 2 题意:一系列字符串,查询字符串S,能通过其他字符串交换串内字符顺序得到的字符串个数 ...

  2. 51Nod 1095 Anigram单词

    熟练使用map即可,不然用vector会超时 #include <iostream> #include <cstring> #include <string> #i ...

  3. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

  4. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  5. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  6. [51NOD1095] Anigram单词(map)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1095 字典的单词在map中排序和不排序各存1次,查的时候相减. ...

  7. 1095 Anigram单词

      基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的Anigram,例如单词 ...

  8. I: Carryon的字符串排序(字典树/map映射)

    2297: Carryon的字符串 Time Limit: C/C++ 1 s      Java/Python 3 s      Memory Limit: 128 MB      Accepted ...

  9. 51nod 1295 XOR key 可持久化01字典树

    题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...

随机推荐

  1. PJMEDIA之录音器的使用(capture sound to avi file)

    为了熟悉pjmedia的相关函数以及使用方法,这里练习了官网上的一个录音器的例子. 核心函数: pj_status_t pjmedia_wav_writer_port_create ( pj_pool ...

  2. Kotlin的数据类:节省很多行代码(KAD 10)

    作者:Antonio Leiva 时间:Jan 25, 2017 原文链接:https://antonioleiva.com/data-classes-kotlin/ 在前面的文章中,我们已经见到了类 ...

  3. 第二章 Internet 地址结构

    注意: 这个系列的博客只是为了巩固我学习的知识,参考的价值不是很大,如果需要,请转到http://www.cnblogs.com/ZCplayground/p/7764436.html Interne ...

  4. hdu 2492 树状数组 Ping pong

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 3 ...

  5. html & email template

    html & email template inline style build tools https://templates.mailchimp.com/getting-started/h ...

  6. 解决IE浏览器中出现“Resource interpreted as Document but transferred with MIME type application/json”问题

    在上传图片时,使用ajax提交,返回的数据格式为json.在测试时发现IE浏览器中,上传图片后,没有显示图片,而是弹出一个提示:是否保存UploadImg.json文件:而在其他浏览器中正常. 在Ch ...

  7. 以太坊源码分析(52)以太坊fast sync算法

    this PR aggregates a lot of small modifications to core, trie, eth and other packages to collectivel ...

  8. 【POJ 2752 Seek the Name, Seek the Fame】

    Time Limit: 2000MSMemory Limit: 65536K Description The little cat is so famous, that many couples tr ...

  9. Linux wget 安装JDK失败

    windows 下安装的话,查看网络,就会发现,是带cookie,回调参数Authparam 验证的

  10. 云服务器基本环境配置(php5.6+mysql+nginx)

    1.安装nginx sudo apt-get install nginx nginx 基本配置 server{ listen ; server_name www.xxxxxx.cn; root /va ...