日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多单词匹配查找

發布時間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多单词匹配查找 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

來源:http://www.cnblogs.com/justinzhang/archive/2012/09/17/2689642.html????? 華為2013年招聘-上機題--多單詞匹配查找

在一個給定的源字符串中查找由若干個單詞組成的字串(模式串),要求如下:

1、? 源字符串與模式串組成格式為:單詞<空格>單詞<空格>單詞...,不用考慮標點符號;源字符串與模式串部字符長度≤64;

2、? 單詞由數字、字母組成,不區分大小寫;

3、? 優先匹配模式串中字符多的單詞,如模式串為”he hello hell”,源字符串為”hello world”,則源字符串中”hello”匹配模式串中的”hello”; 如果出現模式串中多個相同字符數的單詞匹配源串中同一個單詞,優先匹配模式串中先出現的,如模式串”he lo”,源串為”hello world”,則匹配模式串中的”he”匹配源串中的”hello”

4、? 源字符串中單詞被匹配順序是:優先匹配完全相同的單詞;如果有多個單詞可以被匹配,優先匹配第一個.如:源字符串為”hell hello world hell he”,如果模式串為”he”,那么匹配”he”;如果模式串為”hel”,那么匹配第一個”hell”;如果模式串為”hell”,那么匹配”第一個”hell”;

5、? 源字符串的單詞只能被匹配一次,如上例中”hello”已經匹配了,此時”he”和”hell”不能再部分匹配”hello world”中的”hello”,又如模式串為”he hello hell”,源字符串為”hello world hello world”,模式串的”hello”匹配了文本串的第一個”hello”,則模式串的”hell”可以匹配源字符串的第二個”hello”;

1) 實現函數:

int MultiWordMatch(char *Src, char *Match)

2) 參數說明:

輸入:源字符串,模式串(單詞<空格>單詞<空格>單詞...);

3)返回值:

類型:整型

錯誤或異常:-1

無匹配:0

完全匹配(模式串在源字符串中精確出現,次序也完全一樣):1

匹配(模式串的所有單詞在整個源字符串中可以找到,但次序打亂):2

部分匹配(模式串僅部分單詞在源字符串中可以找到):3

例如:

文本串:“Hello world he is a programmer What the hell”

查找模式串”test”,輸出0;

查找模式串”hello world”、”a prog”, 輸出1;

查找模式串”what world”、”prog hell”, 輸出2;

查找模式串”hello hello”、”nand ram”, 輸出3;


代碼:

約定輸入格式如下:(in.txt內容)

Hello world he is a programmer What the hell
test
hello world
a prog
what world
prog hell
hello hello
nand ram

1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 5 char substr[10][65]; 6 char str[65]; 7 8 char * mygets(char *buf, FILE *fp) //fgets會存儲最后的換行符,這個函數去掉換行符 9 { 10 char * temp = fgets(buf, 65, fp); 11 if(temp) 12 if(buf[strlen(buf)-1] == '\n') 13 buf[strlen(buf)-1] = '\0'; 14 return temp; 15 } 16 17 void tolower(char *str, int length) 18 { 19 for(int i = 0; i < length; i++) 20 if(str[i] >= 'A' && str[i] <= 'Z') 21 str[i] = str[i] + 32; 22 } 23 24 int MultiStrMatch(char *str, char *match) 25 { 26 if(!str || !match) 27 return -1; 28 29 int count[65], index = 0; 30 31 //準備源串和待比較的串 32 char temp_str[65]; 33 strncpy(temp_str, str, 65); 34 tolower(temp_str, strlen(temp_str)); 35 tolower(match, strlen(match)); 36 37 //判斷字符串是否存在 38 char *split = strtok(match, " "); //使用strtok函數來分隔字符串!!(話說C對字符串的處理真麻煩。。) 39 while(split!= NULL) 40 { 41 char * find = strstr(temp_str, split); //使用strstr函數來查找字符串是否存在 42 if(find) 43 { 44 memset(find, ' ', strlen(split)); 45 count[index++] = find - temp_str; //指針相減 46 } 47 else 48 count[index++] = -1; 49 50 split = strtok(NULL, " "); 51 } 52 53 //根據count數組進行輸出狀態判斷 54 bool has_no_exit = false; 55 bool has_exit = false; 56 for(int i = 0; i < index; i++) 57 { 58 if(count[i] == -1) 59 has_no_exit = true; 60 else 61 has_exit = true; 62 } 63 if(!has_exit && has_no_exit) 64 return 0; 65 else if(has_no_exit && has_exit) 66 return 3; 67 else 68 { 69 int pre = 0; 70 for(int j = 1; j < index; j++) 71 { 72 if(count[j] < count[pre]) 73 return 2; 74 } 75 return 1; 76 } 77 } 78 79 int main() 80 { 81 FILE * fp = fopen("in.txt", "r"); 82 mygets(str, fp); 83 int number = 0; 84 while(1) 85 { 86 if(mygets(substr[number], fp)) 87 number++; 88 else 89 break; 90 } 91 92 for(int i = 0; i < number; i++) 93 printf("%d\n", MultiStrMatch(str, substr[i])); 94 95 system("pause"); 96 return 0; 97 }

?

?

?

轉載于:https://www.cnblogs.com/dandingyy/archive/2012/09/27/2705656.html

總結

以上是生活随笔為你收集整理的多单词匹配查找的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。