<h1 style="color: rgb(26, 92, 200); text-align: center; font-family: 'Times New Roman';">统计难题</h1><span style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;"><strong><span style="font-family: Arial; font-size: 12px; color: green;">Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 21814 Accepted Submission(s): 9297 </span></strong></span><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Problem Description</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). </div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. </div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Output</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">对于每个提问,给出以该字符串为前缀的单词的数量. </div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Sample Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="font-family: 'Courier New', Courier, monospace;">banana band bee absolute acm ba b band abc</div>
Sample Output
2 3 1 0
Author
Ignatius.L
Recommend
Ignatius.L
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <string>using namespace std;const int M = 26;int n;#define maxn 1005char S[maxn][21];struct Node{ int v; Node *next[M];}*root;void Creat(char *s) //分配内存最好使用calloc 因为会进行初始化 而malloc不初始化
都是随机的数据垃圾{ int len = strlen(s); Node *p = root, *q; for(int i=0; i<len; i++) { int id = s[i] - ‘a‘; if(p->next[id] == NULL) { q = (Node *)calloc(1, sizeof(Node)); q->v = 1; p->next[id] = q; p = q; } else { p = p->next[id]; p->v++; } } //p->v = -1; //标志最后一个为-1}int
Find(char *s) //查找函数可更具需要修改{ int cnt = 0; int len = strlen(s); Node *p = root; for(int i=0; i<len; i++) { int id = s[i] - ‘a‘; p = p->next[id]; if(p == NULL) return 0; //不存在 //if(p->v == -1) // return -1;//已有串是此串前缀 } return p->v;// 此串是某已有串的前缀}char S1[12];int
main(){ n = 0; root = (Node *)calloc(1, sizeof(Node)); // 根节点 while(gets(S1) && S1[0] != ‘\0‘) Creat(S1); while(~scanf("%s", S1)) { printf("%d\n", Find(S1)); } ///while(gets(S) && S)}/*4abcdefghrr10*/