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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

统计字符串中单词个数

發(fā)布時(shí)間:2024/1/17 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 统计字符串中单词个数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?


要求:輸入一個(gè)字符串,統(tǒng)計(jì)每個(gè)單詞的個(gè)數(shù)。單詞間用空格隔開,可多個(gè)空格,寫出自己認(rèn)為高效的算法。

例如:輸入:I love love China
輸出為:
I:
1
love:
2
China:
1

?

?

首先想到的還是模擬的方法,就是用struct把出現(xiàn)過的單詞緩存起來,然后再輸入文本中遍歷到新單詞的時(shí)候,遍歷一次struct,看這個(gè)單詞是不是已經(jīng)存,做相關(guān)處理。
如果輸入文本中有n個(gè)字母,不重復(fù)的字母為m個(gè), 則算法復(fù)雜度為O(nm^2) 最好情況是m =1 ,最差情況是m=n 其實(shí)現(xiàn)代碼如下:

?

1 #include <stdio.h> 2 #include <string.h> 3 struct struct_words{ 4 char word[20]; 5 int count; 6 }; 7 int main(){ 8 char string[100]; 9 char c; 10 struct struct_words words[20]; 11 int i = 0, k = 0 , ws =0; 12 13 for(; i < 20; i++){ 14 words[i].word[0] = '\0'; 15 words[i].count = 0; 16 } 17 puts("please input words."); 18 gets(string); 19 puts("=============開始取詞================"); 20 21 i = 0; 22 do{ 23 c = string[i]; 24 if(c != ' ' && c !='\0'){ 25 words[k].word[ws] = c; 26 words[k].count = 1; 27 ws ++; 28 }else{ 29 words[k].word[ws] = '\0'; 30 ws = 0; 31 k ++; 32 } 33 i ++; 34 }while(c!='\0');lda 35 36 37 puts("=========== 合并相同的單詞 =============="); 38 for(i = 0; words[i].word[0] != '\0' ; i++){ 39 puts(words[i].word); 40 if( words[i].count >= 1) 41 for(k = i; words[k].word[0] != '\0'; k++){ 42 if(strcmp(words[i].word, words[k].word) == 0 43 && words[k].count == 1){ 44 words[k].count --; 45 words[i].count ++; 46 } 47 } 48 } 49 50 puts("=============== End =============="); 51 for(i = 0;words[i].word[0] != '\0' ;i++){ 52 if(words[i].count != 0 ) 53 printf("%s:\t\t%d\n",words[i].word, words[i].count); 54 } 55 return(0); 56 }

?

?

?


然后呢,做一下優(yōu)化,恩路是遍歷用戶的輸入文本是必須的,但是,單詞的緩存和出現(xiàn)次數(shù)的統(tǒng)計(jì)是可以使用hash算法來優(yōu)化的,借用hash算法的特性,使復(fù)雜度立刻就降低到了 O(n),實(shí)現(xiàn)代碼如下:

?

?

1 #include <stdio.h> 2 #include <string.h> 3 #define N 100 4 5 struct struct_words{ 6 char word[100]; 7 int count; 8 }; 9 10 int hash(char* key) 11 { 12 unsigned long h=0; 13 while(*key) 14 { 15 h=(h<<4)+*key++; 16 unsigned long g=h & 0xF0000000L; 17 if(g) 18 h^=g>>24; 19 h&=~g; 20 } 21 return h&N; 22 } 23 int main(){ 24 char string[1000]; 25 char current_word[100]; 26 char c; 27 struct struct_words words[200]; 28 int i = 0, k = 0 , ws =0 , key; 29 int keys[100]; 30 31 for(; i < 200; i++){ 32 words[i].word[0] = '\0'; 33 words[i].count = 0; 34 } 35 puts("=============輸入一些單詞,用空格隔開================"); 36 gets(string); 37 38 i = 0; 39 do{ 40 c = string[i]; 41 //如果第一個(gè)單詞前有空格,跳過去 42 if( ws == 0 && c == ' ') {i++ ; continue;} 43 if(c != ' ' && c !='\0'){ 44 current_word[ws] = c; 45 ws ++; 46 }else{ 47 current_word[ws] = '\0'; 48 key = hash(current_word); 49 if(words[key].count == 0){ 50 strcpy(words[key].word, current_word); 51 keys[k] = key; 52 k++; 53 } 54 words[key].count ++; 55 ws = 0; 56 } 57 i ++; 58 }while(c != '\0'); 59 60 printf("%d" ,k); 61 puts("===============打印結(jié)果 =============="); 62 for(i = 0 ; i < k ;i++){ 63 printf("%s:\t\t%d\n",words[keys[i]].word, words[keys[i]].count); 64 } 65 puts("=============== End =============="); 66 return 0; 67 }

?

?來源:http://www.cnblogs.com/amboyna/archive/2009/12/05/1617387.html

總結(jié)

以上是生活随笔為你收集整理的统计字符串中单词个数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。