[51NOD1095] Anigram单词(map)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1095

字典的单词在map中排序和不排序各存1次,查的时候相减。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn = 10100;
 5 int n, q;
 6 char tmp[12];
 7 map<string, int> d1;
 8 map<string, int> d2;
 9
10 int main() {
11   // freopen("in", "r", stdin);
12   while(~scanf("%d",&n)) {
13     d1.clear(); d2.clear();
14     for(int i = 0; i < n; i++) {
15       scanf("%s", tmp);
16       d1[tmp]++;
17       sort(tmp, tmp+strlen(tmp));
18       d2[tmp]++;
19     }
20     scanf("%d",&q);
21     while(q--) {
22       scanf("%s", tmp);
23       int a = d1[tmp];
24       sort(tmp, tmp+strlen(tmp));
25       int b = d2[tmp];
26       printf("%d\n", b-a);
27     }
28   }
29   return 0;
30 }
时间: 2024-08-05 02:07:50

[51NOD1095] Anigram单词(map)的相关文章

51Nod1095 Anigram单词

题目 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的Anigram,例如单词army和mary互为Anigram.另:相同的2个单词不算Anigram.现在给定一个字典,输入Q个单词,从给出的字典中找出这些单词的Anigram. 思路 一个map保存原串出现次数,一个map保存排序串出现次数,减一下 代码 #include<bits/stdc++.h> #define ll long long #define db double using namespace s

51nod 1095 Anigram单词【hash/map/排序/字典树】

1095 Anigram单词 基准时间限制: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

统计不同的单词(map应用)

题目描述: 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词.在判断是否满足条件时,字母不区分大小写,但在输出时应保留输入中的大小写,按字典序进行排列(所有大写字母在所有的小写字母的前面). #include<iostream> #include<vector> #include<string> #include<algorithm> #include<cctype> #include<map&g

1095 Anigram单词(51nod)

原题链接:http://www.51nod.com/onlineJudge/questionCode.html#problemId=1095&noticeId=20791 老实说这题,我是很不想用字典树,因为不熟~~~但后来没搞出来,就用了字典树..........当然得参考一下大神的代码. 解法:用map统计相同单词... 然后把字符串按字典序排序,然后把字符串插入到字典树里面去 #include<stdio.h> #include<string.h> #include&

51Nod 1095 Anigram单词 | Hash

Input示例 5 add dad bad cad did 3 add cac dda Output示例 1 0 2 #include <iostream> #include <string.h> #include <algorithm> #include <map> using namespace std; #define MAXN 10001 char s[2*MAXN][15]; char str[15]; map<string,int>

51nod 1095 Anigram单词

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1095 竟然错了10次,最后还是用字典树搞过去的,不过这题解法很多,二分,哈希,STL,排序 都可以搞. 字典树建树的时候保存节点出现的次数,因为可能大小写都有,所以开next[52]的数组足够了. 题目关键是相同字符串不能算,那么可以用map统计出现次数最后减去这个即可. 还有就是查找的时候,边查找边记数一直到字符串的结尾,并且如果还有分支的话那么需要减去分支的数,因为

STL语法——映射:map 反片语(Ananagrams,UVa 156)

Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their lett

最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)

一,问题描述 在英文单词表中,有一些单词非常相似,它们可以通过只变换一个字符而得到另一个单词.比如:hive-->five:wine-->line:line-->nine:nine-->mine..... 那么,就存在这样一个问题:给定一个单词作为起始单词(相当于图的源点),给定另一个单词作为终点,求从起点单词经过的最少变换(每次变换只会变换一个字符),变成终点单词. 这个问题,其实就是最短路径问题. 由于最短路径问题中,求解源点到终点的最短路径与求解源点到图中所有顶点的最短路径复

单词接龙

word_solitaire.h #ifndef _WORD_SOLITAIRE_H_ #define _WORD_SOLITAIRE_H_ #include <list> #include <map> #include <queue> #include <iostream> #include <stdlib.h> #include <limits.h> using namespace std; /* *@struct structW