HDU1251 统计难题(非字典树法)

来源:互联网 发布:三个网站域名 编辑:程序博客网 时间:2024/05/12 04:38

今天刚学字典树- -

但自己这指针水平短时间还做不出题,用字符串做了后各种TLE,

本来是用一个长串,TLE,然后用二维数组,TLE,然后最终百度出一个memcmp.

三者结果终于A过。

time 1014MSmemory 2272K

#include<stdio.h>#include<string.h>char str[28][11111][11];int main(){    char s[11],tem[11];    int i=0,j,num[28];    int count;    int len;    memset(num,0,sizeof(num));    while(gets(tem)&&strcmp(tem,"")!=0)    {     int r=tem[0]-96;   strcpy(str[r][num[r]++] , tem);    }    //for(i=0;i<num[1];i++) printf("%s ",str[1][i]);   while(scanf("%s",s)!=EOF)    {        count=0;int r=s[0]-96;        len=strlen(s);        for(j=0;j<num[r];j++)        {            if(memcmp(str[r][j],s,len)==0)                count++;        }        printf("%d\n",count);    }    return 0;}


0 0