现在的位置: 首页 > 综合 > 正文

hdu 1381 Crazy Search(hash/map的应用)

2013年06月01日 ⁄ 综合 ⁄ 共 2159字 ⁄ 字号 评论关闭

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1381

Crazy Search

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1178    Accepted Submission(s): 459

Problem Description
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.
Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.
As an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa", "aab", "aba", "bab", "bac". Therefore, the answer should be 5. 
Input
The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.
Output
The program should output just an integer corresponding to the number of different substrings of size N found in the given text.
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
3 4
daababac
Sample Output
5
 
分析:这里的NC没什么用,NC嘛,当然没什么用咯,你说脑残会有什么用呢  呵呵
         这题 主要任务就是 判断重复 的字符串   这里我们用到map  因为map可以自动覆盖相同的key
 
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<string>
 7 #include<ctime>
 8 #include<queue>
 9 #include<list>
10 #include<map>
11 #define INF 9999999
12 #define MAXN 10000
13 using namespace std;
14 //    priority_queue<int,vector<int>,greater<int> > pq;
15 
16 int main()
17 {
18     int T,N,NC,i;
19     string text;
20     map<string,int> Stat;
21     cin>>T;
22     while(T--)
23     {
24         cin>>N>>NC>>text;
25         Stat.clear();
26         const int Size = text.size();
27         for(i=0;i<Size-N+1;++i)
28         {
29             string tmp(text,i,N);  //string类的构造函数,很强大吧
30             Stat[tmp]=i;   //加入map之后 自动会对重复的key进行覆盖
31         }
32         cout<<Stat.size()<<endl;
33     }
34     return 0;
35 }

 

抱歉!评论已关闭.