hdu 1251 统计难题
因为查询的前缀字典可能不含有,这点没考虑清楚就码了,太粗心了,因为case只有一个,很多初始化操作都不需要了。
1 #include<cstdio> 2 #include<cstring> 3 const int maxnode=500000; 4 int ch[maxnode][26]; 5 int val[maxnode]; 6 int sz; 7 void insert(char *s) 8 { 9 int u=0,len=strlen(s); 10 for(int i=0;i<len;i++) 11 { 12 int c=s[i]-'a'; 13 if(!ch[u][c]) 14 ch[u][c]=sz++; 15 u=ch[u][c]; 16 val[u]++; 17 } 18 } 19 int find(char *s) 20 { 21 int u=0,len=strlen(s); 22 for(int i=0;i<len;i++) 23 { 24 int c=s[i]-'a'; 25 if(ch[u][c]) 26 u=ch[u][c]; 27 else 28 return 0; 29 } 30 return val[u]; 31 } 32 int main() 33 { 34 char s[15]; 35 sz=1; 36 while(gets(s)&&s[0]) 37 { 38 insert(s); 39 } 40 while(scanf("%s",s)!=EOF) 41 { 42 printf("%d\n",find(s)); 43 } 44 return 0; 45 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步