-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1200.cpp
More file actions
69 lines (65 loc) · 1.28 KB
/
1200.cpp
File metadata and controls
69 lines (65 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<iostream>
using namespace std;
short flag[256];
int flag_cnt;
struct node_t{
int level;
struct node_t **child;
};
struct node_t **CmpP;
int CmpCnt;
int ans;
int SubStrLen;
int nChar;
void cmpnext(struct node_t *cur,char c){
int i;
i=flag[c];
if(cur->child[i]!=NULL){
cur=cur->child[i];
if(cur->level!=SubStrLen){
CmpP[CmpCnt++]=cur;
}
}
else{
cur->child[i]=(struct node_t *)malloc(sizeof(struct node_t));
cur->child[i]->child=(struct node_t **)malloc(nChar*sizeof(struct node_t *));
memset(cur->child[i]->child,0,nChar*sizeof(struct node_t *));
cur->child[i]->level=cur->level+1;
cur=cur->child[i];
if(cur->level!=SubStrLen){
CmpP[CmpCnt++]=cur;
}
else{
ans++;
}
}
}
int main()
{
struct node_t root;
char c;
int i;
int cnt;
scanf("%d %d\n",&SubStrLen,&nChar);
root.level=0;
root.child=(struct node_t **)malloc(nChar*sizeof(struct node_t *));
memset(root.child,0,nChar*sizeof(struct node_t *));
memset(flag,-1,sizeof(flag));
CmpP=(struct node_t **)malloc(SubStrLen*sizeof(struct node_t *));
scanf("%c",&c);
while(c!='\n'){
if(-1 == flag[c]){
flag[c]=flag_cnt++;
}
cnt=CmpCnt;
CmpCnt=0;
for(i=0;i<cnt;++i){
cmpnext(CmpP[i],c);
}
cmpnext(&root,c);
scanf("%c",&c);
}
cout<<ans<<endl;
return 0;
}