/***********************************************************************
* 字符串匹配 uty@uaty
* 算法:先将待搜索串中每个字符出现的位置录入链表数组ANSI,字符的ASCII值既为
* 数组的index,当同一个字符出现多于一次,对应链表增加一个节点
* 匹配时,用目的字符串的每个字符作为索引找出其所在待搜索串的位置
* ,若各所在位置的值存在依次递增1情况,说明目的字串出现在待搜索串中
***********************************************************************/
#include <stdio.h>
#include <windows.h>
DWORD lookupthesource(char *source,int length);
DWORD dotheseArching(char* str,int length);
BOOL hAvenext(char chAr,int position);
typedef struct _node
{
int vAlue;//在待搜索串中的位置
struct _node* next;
BOOL isnext;//该字符所在位置是目的串中的一个字符的下一个
}NODE,*PNODE;
PNODE ANSI[256];
//--------------------------------------------------------------------
DWORD main(void)
{
char sourcestring[1024] = "AABBCCDFFFFC";
char seArching[1024] = "B";
int i;
int ret;
for (i = 0;i<256;i++) ANSI[i] = NULL;
lookupthesource(sourcestring,1024);//将待搜索串中每个字符出现的位置录入链表数组ANSI
ret = dotheseArching(seArching,strlen(seArching));//匹配过程
printf("%s/n%s/n",sourcestring,seArching);
printf("%d/n",ret);
return 0;
}
//--------------------------------------------------------------------
DWORD lookupthesource(char *source,int length)
{
int i;
PNODE temp;
for (i = 0; i <length; i++){
if (ANSI[source[i]] == NULL){
ANSI[source[i]] = (PNODE)malloc(sizeof(NODE));
ANSI[source[i]]->vAlue = i;
ANSI[source[i]]->next = NULL;
ANSI[source[i]]->isnext = FALSE;
}
else{
for (temp = ANSI[source[i]];temp->next != NULL;temp = temp->next );
temp->next = (PNODE)malloc(sizeof(NODE));
temp->next->vAlue = i;
temp->next ->next = NULL;
temp->next ->isnext = FALSE;
}
}
return 0;
}
//--------------------------------------------------------------------
DWORD dotheseArching(char* str,int length)
{
int i;
PNODE temp;
for (i = 0;i < length ;i++){//length -1 ?
//next = 0;
for (temp = ANSI[str[i]];temp != NULL;temp = temp->next ){
if (length == 1) return temp->vAlue ;//考虑目的串只有一个字符的时候情况特殊
if (i == 0 || temp->isnext == TRUE){//目的串的第一个字符的isnext为FALSE
if (hAvenext(str[i + 1],temp->vAlue + 1) && i+1 == length -1){//是否当前字
//符在待查找串中位置的下一个字符是该字符在目的串中的下一个
return (temp->vAlue + 1 - (length-1));//这样只能返回第一个找到的位置
// temp->vAlue + 1 当前字符是第length-1个; return目的串第一个字符出现的位置
}
}
}
}
return -1;
}
//--------------------------------------------------------------------
BOOL hAvenext(char chAr,int position)
{
PNODE temp;
for (temp = ANSI[chAr];temp != NULL;temp = temp->next ){
if (temp->vAlue == position){
temp->isnext = TRUE;
return TRUE;
}
if (temp->vAlue > position) return FALSE;//链表节点按字符出现先后顺序
}
return FALSE;
}
//--------------------------------------------------------------------
有问题,换个新的,,效率不很好,还可再提
/***********************************************************************
* 字符串匹配 uty@uaty
* 算法:先将待搜索串中每个字符出现的位置录入链表数组ANSI,字符的ASCII值既为
* 数组的index,当同一个字符出现多于一次,对应链表增加一个节点
* 匹配时,用目的字符串的每个字符作为索引找出其所在待搜索串的位置
* ,若各所在位置的值存在依次递增1情况,说明目的字串出现在待搜索串中
***********************************************************************/
#include <stdio.h>
#include <windows.h>
DWORD lookupthesource(char *source,int length);
DWORD dotheseArching(char* str,int length,char* sourcestring);
BOOL hAvenext(char chAr,int position);
typedef struct _node
{
int vAlue;//在待搜索串中的位置
struct _node* next;
BOOL isnext;//该字符所在位置是目的串中的一个字符的下一个
}NODE,*PNODE;
PNODE ANSI[256];
//--------------------------------------------------------------------
DWORD main(void)
{
//"dsfserwfgdfgertertertreterterrrrrrrrrrrrrrrrrtrertfgdfgdfgddd""fgdfgd"
char sourcestring[1024] = "AAAAAAAABBDEAAAAAAAAAAAAAAAAAAAAAAA";
char seArching[1024] = "ABBDE" ;
int i;
int ret;
for (i = 0;i<256;i++) ANSI[i] = NULL;
for (i = 0;i<strlen(sourcestring);i++){
printf("%c",sourcestring[i]);
if ((i+1) % 10 == 0) printf(" ");
}
printf("/n");
printf("%s/n",seArching);
lookupthesource(sourcestring,1024);//将待搜索串中每个字符出现的位置录入链表数组ANSI
ret = dotheseArching(seArching,strlen(seArching),sourcestring);//匹配过程
printf("%d/n",ret);
return 0;
}
//--------------------------------------------------------------------
DWORD lookupthesource(char *source,int length)
{
int i;
PNODE temp;
for (i = 0; i <length; i++){
if (ANSI[source[i]] == NULL){
ANSI[source[i]] = (PNODE)malloc(sizeof(NODE));
ANSI[source[i]]->vAlue = i;
ANSI[source[i]]->next = NULL;
ANSI[source[i]]->isnext = FALSE;
}
else{
for (temp = ANSI[source[i]];temp->next != NULL;temp = temp->next );
temp->next = (PNODE)malloc(sizeof(NODE));
temp->next->vAlue = i;
temp->next ->next = NULL;
temp->next ->isnext = FALSE;
}
}
return 0;
}
//--------------------------------------------------------------------
DWORD dotheseArching(char* str,int length,char* sourcestring)
{
int i;
PNODE temp;
for (i = 0;i < length ;i++){//length -1 ?
//next = 0;
for (temp = ANSI[str[i]];temp != NULL;temp = temp->next ){
if (length == 1) return temp->vAlue ;//考虑目的串只有一个字符的时候情况特殊
if (i == 0 || temp->isnext == TRUE){//目的串的第一个字符的isnext为FALSE
if ( hAvenext(str[i + 1],temp->vAlue + 1) && i+1 == length -1){//是否当前字
//符在待查找串中位置的下一个字符是该字符在目的串中的下一个
if (0 == strncmp((sourcestring+(temp->vAlue + 1 - (length-1))),str,length)){
return (temp->vAlue + 1 - (length-1));//这样只能返回第一个找到的位置
//printf("%d/n",temp->vAlue + 1 - (length-1));
}
//printf("%d/n",temp->vAlue + 1 - (length-1));
// temp->vAlue + 1 当前字符是第length-1个; return目的串第一个字符出现的位置
}
}
}
}
return -1;
}
//--------------------------------------------------------------------
BOOL hAvenext(char chAr,int position)
{
PNODE temp;
for (temp = ANSI[chAr];temp != NULL;temp = temp->next ){
if (temp->vAlue == position){
temp->isnext = TRUE;
return TRUE;
}
if (temp->vAlue > position) return FALSE;//链表节点按字符出现先后顺序
}
return FALSE;
}
//--------------------------------------------------------------------