-
Notifications
You must be signed in to change notification settings - Fork 0
/
week2-9.c
64 lines (60 loc) · 1.6 KB
/
week2-9.c
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
#include <stdio.h>
#include <string.h>
int n;
int first_occurence(char names[][50],int l,int r,char target[]){
while(r>=l){
int mid=(l+r)/2;
if((mid==0||(strcmp(target,names[mid-1])>0))&&(strcmp(target,names[mid])==0)){
return mid;
}
else if(strcmp(target,names[mid])>0){
return first_occurence(names,(mid+1),r,target);
}
else{
return first_occurence(names,l,(mid-1),target);
}
}
}
int last_occurence(char names[][50],int l,int r,char target[]){
if(r >= l)
{
int mid = (l + r)/2;
if( ( mid == n-1 || (strcmp(target,names[mid+1]))<0) &&strcmp(target,names[mid])==0)
return mid;
else if(strcmp(target,names[mid])<0)
return last_occurence(names,l, (mid -1), target);
else
return last_occurence(names, (mid + 1),r,target);
}
}
int main()
{
int i,j;
scanf("%d",&n);
char names[n][50];
for(i=0;i<n;i++){
scanf("%s",names[i]);
}
int index;
char temp[50];
for(i=0;i<n-1;i++){
index=i;
for(j=i+1;j<n;j++){
if(strcmp(names[j],names[index])<0){
index=j;
}
}
strcpy(temp,names[i]);
strcpy(names[i],names[index]);
strcpy(names[index],temp);
}
/*for(i=0;i<n;i++){
printf("%s\n",names[i]);
}*/
char target[50];
scanf("%s",target);
int first=first_occurence(names,0,n-1,target);
int last=last_occurence(names,0,n-1,target);
printf("%d",last-first+1);
return 0;
}