hdu1381 Crazy Search(hash map)

题目意思:

给出一个字符串和字串的长度,求出该字符串的所有给定长度的字串的个数(不相同)。

题目分析:

此题为简单的字符串哈hash map问题,可以直接调用STL里的map类。map<string,int> snum;

AC代码:

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
    int t,n,nc;
    cin>>t;
    while(t--){
        string s;
        map<string,int> snum;
        cin>>n>>nc>>s;
        int k=0;
        int len=s.length();
        for(int i=0;i<=len-n;i++){
            string ss=s.substr(i,n);
            if(snum[ss]==0){
                k++;
                snum[ss]=1;
            }
        }
        cout<<k<<endl;
        if(t>1) cout<<endl;
    }
    return 0;
}
时间: 2024-10-06 05:35:15

hdu1381 Crazy Search(hash map)的相关文章

POJ-1200 Crazy Search(hash)

Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27336   Accepted: 7641 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 gi

[poj1200]Crazy Search(hash)

Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 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

$Hdu1381\ Crazy\ Search$

前置芝士 :string 的 基本用法 string s = "hello world" ; string tmp(s,0,5) ; cout << tmp << endl ; 上面这一段代码 可以复制粘贴 然后改变 数字.(试试效果 \(string\ tmp(s,i,n);\) 赋初值 也就是说从 $s[i]?- To - ?s[i+n-1] $ 所以既然是这样 应该就有一种比较优秀的做法 \(O(S.length())\) 可海星. \[STL大法好啊\]

POJ 1200 Crazy Search (字符串hash)

题目大意: 分析长度为n的子串有多少种. 思路分析: 对于没出现的字符,将其分配一个数字. 然后将子串看做一个nc进制的数. 然后hash判断. #include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <cstring> #include <string> using namespace std; bool vis[26666

POJ 1200 Crazy Search(Hash)

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 t

Crazy Search

poj1200:http://poj.org/problem?id=1200 题意:给你一个有m种字符串,求长度为n的连续子串由多少种. 题解:网上的代码都是hash,但是本人觉得hash有问题,就是n,m稍微大点,hash的值都会爆出int,无法开数组来记录该串是否被记录.可能数据弱,结果hash竟然A了 .正确的解法是用set来存,把所有的hash值放进set,set有去重效果,最后求一下set的大小.但是这样结果是T了.不知道这题怎么解.一下是第一种代码.于是换别的,trie树来搞. Cr

poj 1200 Crazy Search

题目: 链接:点击打开链接 题意: 输入n和nc,以及字符串s,输出长度为n的不同字串的个数. 算法: 思路: 用hash判重(hash值......),看了大牛的代码,对hash还是不甚理解.... 代码: #include<iostream> #include<cstring> #include<cstdio> #include<cstdio> using namespace std; #define MAXN 16000010 const int MA

[LeetCode] Repeated DNA Sequences hash map

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long seq

Perl hash map操作,定义,插入,删除,hash函数

#!/usr/bin/perl -w ######################################################################### # File Name: test3.pl ######################################################################### print "hash map\n"; my %some_hash = ("key1", 1