http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2081&pid=15
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 #pragma comment(linker, "/STACK:102400000,102400000") 17 #define CL(arr, val) memset(arr, val, sizeof(arr)) 18 19 #define ll long long 20 #define inf 0x7f7f7f7f 21 #define lc l,m,rt<<1 22 #define rc m + 1,r,rt<<1|1 23 #define pi acos(-1.0) 24 25 #define L(x) (x) << 1 26 #define R(x) (x) << 1 | 1 27 #define MID(l, r) (l + r) >> 1 28 #define Min(x, y) (x) < (y) ? (x) : (y) 29 #define Max(x, y) (x) < (y) ? (y) : (x) 30 #define E(x) (1 << (x)) 31 #define iabs(x) (x) < 0 ? -(x) : (x) 32 #define OUT(x) printf("%I64d\n", x) 33 #define lowbit(x) (x)&(-x) 34 #define Read() freopen("a.txt", "r", stdin) 35 #define Write() freopen("b.txt", "w", stdout); 36 #define maxn 1010 37 #define maxv 1010 38 #define mod 1000000000 39 using namespace std; 40 41 typedef struct node 42 { 43 int count; 44 struct node *next[26]; 45 }*tree; 46 47 void insert(tree h,char *s) 48 { //每一个节点保存的都是每一个字母出现的次数。 49 tree p=h; 50 int len=strlen(s); 51 for(int i=0;i<len;i++) 52 { 53 int index=s[i]-‘a‘; 54 if(p->next[index]!=NULL) 55 { 56 p=p->next[index]; 57 p->count++; 58 } 59 else 60 { 61 tree tem=(tree)calloc(1,sizeof(node)); 62 tem->count=1; 63 p->next[index]=tem; 64 p=tem; 65 } 66 } 67 } 68 69 int find(tree h) 70 { //查找不为空的结点,加上相应的次数,如果count值大于1,则说明有分支,需继续查找下去 71 tree p=h; 72 int sum=0,i; 73 for(int i=0;i<26;i++) 74 { 75 if(h->next[i]!=NULL) 76 { 77 p=h->next[i]; 78 sum+=p->count; 79 if(p->count>1) sum+=find(p); 80 } 81 } 82 return sum; 83 } 84 85 char s[1000]; 86 87 int main() 88 { 89 //freopen("a.txt","r",stdin); 90 int t,n; 91 scanf("%d",&t); 92 while(t--) 93 { 94 scanf("%d",&n); 95 tree head=(tree)calloc(1,sizeof(node)); 96 while(n--) 97 { 98 scanf("%s",s); 99 insert(head,s); 100 } 101 printf("%d\n",find(head)); 102 free(head); 103 } 104 return 0; 105 }
时间: 2024-11-01 01:45:32