答案 0 :(得分:0)
能够通过以下方式完成此任务。
enter code here
#include<stdio.h>
#include<stdlib.h>
int first(char *a[26],int low,int high,char *word,int n);
int last(char*a[26],int low,int high,char* word,int n);
int mycomp(char* a,char* b){
int i = 0;
while (a[i] == b[i] && a[i] != '\0')
i++;
if (a[i] > b[i])
return 2;
else if (a[i] < b[i])
return -2;
else
return 0;
}
int main(){
int n,count=0;
scanf("%d",&n);
char dictionery[n][50];
char word[50];
int i,j;
for(i=0;i<n;i++)
scanf("%s",dictionery[i]);
scanf("%s",word);
i = first(dictionery[n],0,n-1,word,n);
if(i==-1)
printf("No occurrances\n");
else{
j = last(dictionery[n],i,n-1,word,n);
count = j-i+1;
printf("%d\n",count);
}
return 0;
}
int first(char *a[26],int low,int high,char *word,int n){
if(high >= low){
int mid = (high+low)/2;
int d = mycomp(word,a[mid-1]);
int e = mycomp(word,a[mid]);
if((mid==0 || d>0)&& e==0)
return mid;
else if(e>0)
return first(*a[],mid+1,high,word,n);
else
return first(*a[],low,mid-1,word,n);
}
return -1;
}
int last(char* a[26],int low,int high,char* word,int n){
if(high>=low){
int mid = (high+low)/2;
int d = mycomp(word,a[mid+1]);
int e = mycomp(word,a[mid]);
if((mid==n-1 || d<0)&& e==0 )
return mid;
else if(e<0)
return last(*a[],low,mid-1,word,n);
else
return last(*a[],mid+1,high,word,n);
}
return -1;
}