hdu1251 字典树or map

一道字典树的题,不过看起来用map更为简单

传送门

题意: 给出一堆字符串构成一个字典,求字典里以某字符串为前缀的字符串有几个

思路: 输入字符串时把字符串的前缀全部存进map并标记次数 查询时直接输出就可以了

AC代码:

 1 #include "stdio.h"
 2 #include "map"
 3 #include "string"
 4 #include "string.h"
 5 #include "iostream"
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     map<string,int>M;
12     char ss[100],s[100];
13     int k;
14     while(gets(ss))
15     {
16         int l = strlen(ss);
17         if(l==0) break;
18         s[0] = '',k=0;
19         for(int j=0; j<l; j++)
20         {
21             s[k++]=ss[j];
22             s[k] = '';
23             M[s]++;
24         }
25     }
26     while(gets(s))
27         cout<<M[s]<<endl;
28     return 0;
29 }
原文地址:https://www.cnblogs.com/max88888888/p/5877349.html