匹配字符串
#include <stdio.h>
#include <stdlib.h>
unsigned int str_len(char* a)
{
if(*a == 0)
{
return 0;
}
char *t = a;
for(; *t; ++t);
return (unsigned int)(t-a);
}
void str_copy(char *des,const char *src, unsigned int len)
{
for(;len>0; --len, ++des,++src)
{
*des = *src;
}
*des = 0;
}
char *str_join(char *des, const char* src, unsigned int len_src)
{
char *t=NULL;
if(des == 0)
{
t =(char*)malloc(sizeof(char)*(len_src+1));
str_copy(t,src,len_src);
return t;
}
else
{
unsigned int len_des = str_len(des);
t = (char*)malloc(sizeof(char)* (len_src+len_des+2));
str_copy(t,des,len_des);
*(t+len_des)=' ';
str_copy(t+len_des+1,src,len_src);
free(des);
return t;
}
}
//这里不太理解
int canMatch(char *src,char *rule)
{
if(*rule == 0)
{
return 0;
}
int r =-1,may;
if(*rule == '*')
{
r = canMatch(src,rule+1); //去掉*
if(*src)
{
may = canMatch(src+1,rule);
if((may>=0)&&(++may>r))
{
r = may;
}
}
}
if(*src == 0) //匹配完
{
return r;
}
if((*rule == '?')||(*rule == *src))
{
may = canMatch(src+1,rule+1);
if((may>=0)||(++may>r))
{
r = may;
}
}
return r;
}
/*
char *my_find(char *src, char *rule)
{
unsigned int len = str_len(src);
int *match = (int*)malloc(sizeof(int)*len);
int max_pos = -1;
char *output = 0;
for( int i = 0;i<(int)len;++i)
{
match[i] = canMatch(src+i,rule);
if((max_pos<0)||(match[i]>match[max_pos]))
{
max_pos = i;
}
}
if((max_pos<0)||(match[max_pos]<=0))
{
//不匹配
output = (char*)malloc(sizeof(char));
*output = 0;
return output;
}
for(unsigned int i = 0; i<len;)
{
if(match[i] == match[max_pos])
{
//找到匹配
output = str_join(output,src+i,(unsigned int)match[i]);
i+=(unsigned)match[i];
}
else
{
++i;
}
}
free(match);
return output;
}
*/
char *my_find(char* src,char* rule)
{ //计算字符串长度
int len1,len2;
for(len1 = 0;src[len1];len1++);
for(len2 = 0;rule[len2];len2++);
int MAX = len1 > len2? (len1+1):(len2+1);
int **dp;
//定义一个二维数组
dp = (int**)malloc(sizeof(int*)*(unsigned)(len1+1));
for(int i=0;i<len1;i++)
{
dp[i] = (int*)malloc(sizeof(int)*(unsigned)(len2+1));
}
dp[0][0] = 0;
for(int i = 1;i<=len2;i++)
dp[0][i] = -1;
for(int i = 1;i<=len1;i++)
dp[i][0] = 0;
for(int i =0;i<len1;i++)
{
for(int j=1;j<=len2;j++)
{
if(rule[j-1] == '*')
{
dp[i][j]=-1;//dp[0][0] = -1;
if(dp[i-1][j-1] !=-1)
{
dp[i][j] = dp[i-1][j-1]+1;
}
if(dp[i-1][j]!=-1&&dp[i][j]<dp[i-1][j]+1)
{
dp[i][j] = dp[i-1][j]+1;
}
}
else if(rule[j-1]=='?')
{
if(dp[i-1][j-1]!=-1)
{
dp[i][j] = dp[i-1][j-1]+1;
}
else dp[i][j] = -1;
}
else
{
if(dp[i-1][j-1]!=1&& src[i-1]==rule[j-1])
{
dp[i][j] = dp[i-1][j-1] +1;
}
else {
dp[i][j] = -1;
}
}
}
}
int m=-1;//记录最大的字符长度
int *ans = (int*) malloc(sizeof(int)*(unsigned)len1);
int count_ans = 0;//记录答案的个数
char *returnes = (char*)malloc(sizeof(char)*(unsigned)(len1+1));
int count = 0;
for(int i=1;i<=len1;i++)
if(dp[i][len2]>m)
{
m=dp[i][len2] ;
count_ans = 0;
ans[count_ans++] = i-m;
}
else if(dp[i][len2]!=-1&&dp[i][len2]==m)
{
ans[count_ans++] = i-m;
}
if(count_ans!=0)
{
int len = ans[0];
for (int i=0;i<m;i++)
{
printf("%c",src[i+ans[0]]);
returnes[count++]=src[i+ans[0]];
}
for(int j=1;j<count_ans;j++)
{
printf(" ");
returnes[count++] = ' ';
len = ans[j];
for(int i=0;i<m;i++)
{
printf("%c",src[i+ans[j]]);
returnes[count++] = src[i+ans[j]];
}
}
printf("\n");
returnes[count++] = '\0';
}
return returnes;
}
int main()
{
char des[]="hello";
char rule[]="*o";
char *buf = NULL;
buf = my_find(des,rule);
printf("%s\n",buf);
free(buf);
return 0;
}