自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

【字符串處理算法】字符串包含的算法設(shè)計(jì)及C代碼實(shí)現(xiàn)

開(kāi)發(fā) 開(kāi)發(fā)工具 算法
今天講一講字符串包含的算法設(shè)計(jì)及C代碼實(shí)現(xiàn)。

一、需求描述

給定一個(gè)長(zhǎng)字符串和一個(gè)短字符串,編寫(xiě)程序判斷短字符串中的所有字符是否都在長(zhǎng)字符串中。如果是,則長(zhǎng)字符串包含短字符串;反之,不包含。

為了盡量包含大多數(shù)情況,字符串中可以包含大小寫(xiě)英文字母、數(shù)字和各種標(biāo)點(diǎn)符號(hào),并且區(qū)分大小寫(xiě)字母。

下面舉幾個(gè)例子予以說(shuō)明:

1.如果長(zhǎng)字符串是“ABCDE”,短字符串是“ADC”,那么短字符串中的所有字符都在長(zhǎng)字符串中,即長(zhǎng)字符串包含了短字符串。

2.如果長(zhǎng)字符串是“ABCDE”,短字符串是“ADCF”,那么短字符串中不是所有字符都在長(zhǎng)字符串中,即長(zhǎng)字符串不包含了短字符串。

3.如果長(zhǎng)字符串是“ABCDE”,短字符串是“AAB”,那么短字符串中的所有字符都在長(zhǎng)字符串中,即長(zhǎng)字符串包含了短字符串。

[[180306]]

二、算法設(shè)計(jì)

我們都知道,就像人體是由一個(gè)個(gè)的細(xì)胞組成一樣,一個(gè)字符串是由一個(gè)個(gè)的字符組成。如果組成某個(gè)字符串的所有字符都在另一個(gè)字符串中,那么這個(gè)字符串就被另一個(gè)字符串包含。

因此,我們可以考慮先將兩個(gè)字符串中的所有字符都找出來(lái),再判斷較短的字符串中的所有字符是否都出現(xiàn)在了較長(zhǎng)的字符串中。如果是,那么兩個(gè)字符串是包含與被包含的關(guān)系;如果不是,那么兩個(gè)字符串則“形同陌路”。

程序的總體流程如圖1所示。

圖1 程序的總體流程

三、特殊流程考慮

在編寫(xiě)程序的過(guò)程中,我們要對(duì)輸入的兩個(gè)字符串的長(zhǎng)度及格式多做考慮,如:

1.如果輸入失誤,導(dǎo)致短字符串的長(zhǎng)度大于了長(zhǎng)字符串,那么程序直接返回,不進(jìn)行后續(xù)處理。

2.不允許在輸入字符串的中間出現(xiàn)空格,如果出現(xiàn)了,只能把空格前面的內(nèi)容作為輸入的字符串。

3.輸入字符串可以包含字母(區(qū)分大小寫(xiě))、數(shù)字、標(biāo)點(diǎn)符號(hào)等字符。

4.為了方便程序處理,設(shè)定較長(zhǎng)的字符串最長(zhǎng)為500個(gè)字節(jié),較短的字符串最長(zhǎng)為100個(gè)字節(jié)。

四、程序代碼

  1. /********************************************************************** 
  2. * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。 
  3. * 文件名稱: StringContains.c 
  4. * 文件標(biāo)識(shí): 無(wú) 
  5. * 內(nèi)容摘要: 測(cè)試一個(gè)字符串是否是另一個(gè)字符串的子串 
  6. * 其它說(shuō)明: 例如, "ABC"是"ABCD"的子串 
  7. * 當(dāng)前版本: V1.0 
  8. * 作    者: Zhou Zhaoxiong 
  9. * 完成日期: 20160216 
  10. **********************************************************************/ 
  11. #include <stdio.h> 
  12. #include <stdlib.h> 
  13.  
  14. // 重新定義數(shù)據(jù)類型 
  15. typedef signed   char       INT8; 
  16. typedef unsigned short int  UINT16; 
  17. typedef          int        INT32; 
  18. typedef unsigned int        UINT32; 
  19.  
  20. // 存放字符串中的字符和格式的結(jié)構(gòu)體 
  21. typedef struct 
  22.     INT8   szStrCharArray[101][2];     // 字符串中不相同的字符的存放數(shù)組,***支持100個(gè) 
  23.     INT32  iStrCharCount;              // 字符串中不相同的字符的個(gè)數(shù) 
  24. } StrInfo_T; 
  25.  
  26. StrInfo_T gtLongerStrInfo  = {0}; 
  27. StrInfo_T gtShorterStrInfo = {0}; 
  28.  
  29.  
  30. // 函數(shù)聲明 
  31. void GetStrChar(INT8 *pszInputStr, INT32 iProcessFlag); 
  32. INT32 JudgeIfContainsStr(); 
  33.  
  34.  
  35. /********************************************************************** 
  36. * 功能描述: 主函數(shù) 
  37. * 輸入?yún)?shù): 無(wú) 
  38. * 輸出參數(shù): 無(wú) 
  39. * 返 回 值: 0-執(zhí)行成功   其它-執(zhí)行失敗 
  40. * 其它說(shuō)明: 無(wú) 
  41. * 修改日期        版本號(hào)       修改人          修改內(nèi)容 
  42. * ----------------------------------------------------------------- 
  43. * 20160216        V1.0     Zhou Zhaoxiong        創(chuàng)建 
  44. ***********************************************************************/ 
  45. INT32 main() 
  46.     INT8   szLongerStr[500]   = {0}; 
  47.     INT8   szShorterStr[100]  = {0}; 
  48.      
  49.     UINT32 iContainFlag = 1;     // 包含標(biāo)志, 1-包含, 0-不包含 
  50.      
  51.     printf("Please input the longer string: \n"); 
  52.     scanf("%s", szLongerStr); 
  53.     printf("LongerStr=%s\n", szLongerStr); 
  54.  
  55.     printf("Please input the shorter string: \n"); 
  56.     scanf("%s", szShorterStr); 
  57.     printf("ShorterStr=%s\n", szShorterStr); 
  58.  
  59.     // 如果ShorterStr的長(zhǎng)度大于LongerStr, 則直接返回 
  60.     if (strlen(szShorterStr) > strlen(szLongerStr)) 
  61.     { 
  62.         printf("%s is longer than %s, please check!\n", szShorterStr, szLongerStr); 
  63.         return -1; 
  64.     } 
  65.      
  66.     // 獲取較長(zhǎng)的字符串中的不同的字符 
  67.     GetStrChar(szLongerStr, 1); 
  68.  
  69.     // 獲取較短的字符串中的不同的字符 
  70.     GetStrChar(szShorterStr, 2); 
  71.  
  72.     iContainFlag = JudgeIfContainsStr(); 
  73.     if (iContainFlag == 0) 
  74.     { 
  75.         printf("%s doesn't contain %s!\n", szLongerStr, szShorterStr); 
  76.     } 
  77.     else 
  78.     { 
  79.         printf("%s contains %s!\n", szLongerStr, szShorterStr); 
  80.     } 
  81.      
  82.     return 0;             
  83.  
  84.  
  85. /********************************************************************** 
  86. * 功能描述: 獲取字符串中不相同的字符及其個(gè)數(shù) 
  87. * 輸入?yún)?shù): pszInputStr-輸入字符串 
  88.              iProcessFlag-處理標(biāo)志(1:處理長(zhǎng)字符串, 2:處理短字符串) 
  89. * 輸出參數(shù): 無(wú) 
  90. * 返 回 值: 無(wú) 
  91. * 其它說(shuō)明: 無(wú) 
  92. * 修改日期          版本號(hào)         修改人           修改內(nèi)容 
  93. * --------------------------------------------------------------- 
  94. * 20160216          V1.0       Zhou Zhaoxiong        創(chuàng)建 
  95. ***********************************************************************/ 
  96. void GetStrChar(INT8 *pszInputStr, INT32 iProcessFlag) 
  97.     INT32  iCharCount      = 0;                // 字符個(gè)數(shù) 
  98.     INT8   szInputStr[501] = {0}; 
  99.     INT8   szCharBuf[2]    = {0};              // 存放單個(gè)字符的緩存 
  100.     INT32  iRepeatFlag     = 0
  101.     UINT32 iStrPosFlag     = 0
  102.     UINT32 iLoopFlag       = 0
  103.     UINT32 iInputStrLen    = 0
  104.  
  105.     if (pszInputStr == NULL) 
  106.     { 
  107.         return; 
  108.     } 
  109.  
  110.     iInputStrLen = strlen(pszInputStr); 
  111.     if (iInputStrLen >= 500)  // ***支持100個(gè)字母 
  112.     { 
  113.         return; 
  114.     } 
  115.  
  116.     memcpy(szInputStr, pszInputStr, iInputStrLen); 
  117.  
  118.     iCharCount = 0
  119.  
  120.     for (iStrPosFlag = 0; iStrPosFlag < iInputStrLen; iStrPosFlag ++) 
  121.     { 
  122.         iRepeatFlag = 0
  123.          
  124.         // 判斷正要獲取的字符是否已經(jīng)存在了 
  125.         memset(szCharBuf, 0x00, sizeof(szCharBuf)); 
  126.         memcpy(szCharBuf, szInputStr+iStrPosFlag, 1); 
  127.  
  128.         // 若與之前已經(jīng)加入的字符重復(fù), 則忽略 
  129.         for (iLoopFlag = 0; iLoopFlag < iCharCount; iLoopFlag ++) 
  130.         { 
  131.             if (iProcessFlag == 1)    // 處理長(zhǎng)字符串 
  132.             { 
  133.                 if (0 == strncmp(gtLongerStrInfo.szStrCharArray[iLoopFlag], szCharBuf, 1)) 
  134.                 { 
  135.                     iRepeatFlag = 1;  // 有重復(fù)的, 直接忽略 
  136.                     break; 
  137.                 } 
  138.             } 
  139.             else                     // 處理短字符串 
  140.             { 
  141.                 if (0 == strncmp(gtShorterStrInfo.szStrCharArray[iLoopFlag], szCharBuf, 1)) 
  142.                 { 
  143.                     iRepeatFlag = 1;  // 有重復(fù)的, 直接忽略 
  144.                     break; 
  145.                 } 
  146.             } 
  147.         } 
  148.  
  149.         if (1 == iRepeatFlag) 
  150.         { 
  151.             continue; 
  152.         } 
  153.  
  154.         if (iProcessFlag == 1)    // 處理長(zhǎng)字符串 
  155.         { 
  156.             strncpy(gtLongerStrInfo.szStrCharArray[iCharCount], szCharBuf, 1); 
  157.         } 
  158.         else                      // 處理短字符串 
  159.         { 
  160.             strncpy(gtShorterStrInfo.szStrCharArray[iCharCount], szCharBuf, 1); 
  161.         } 
  162.  
  163.         iCharCountiCharCount = iCharCount + 1; 
  164.     } 
  165.  
  166.     if (iProcessFlag == 1)    // 處理長(zhǎng)字符串 
  167.     { 
  168.         gtLongerStrInfo.iStrCharCount = iCharCount
  169.     } 
  170.     else                      // 處理短字符串 
  171.     { 
  172.         gtShorterStrInfo.iStrCharCount = iCharCount
  173.     } 
  174.  
  175.     return; 
  176.  
  177.  
  178. /********************************************************************** 
  179. * 功能描述: 判斷長(zhǎng)字符串是否包含了短字符串 
  180. * 輸入?yún)?shù): 無(wú) 
  181. * 輸出參數(shù): 無(wú) 
  182. * 返 回 值: 1-包含了 0-沒(méi)有包含 
  183. * 其它說(shuō)明: 無(wú) 
  184. * 修改日期          版本號(hào)         修改人           修改內(nèi)容 
  185. * --------------------------------------------------------------- 
  186. * 20160216          V1.0       Zhou Zhaoxiong        創(chuàng)建 
  187. ***********************************************************************/ 
  188. INT32 JudgeIfContainsStr() 
  189.     UINT32 iLongerLoopFlag    = 0
  190.     UINT32 iShorterLoopFlag   = 0
  191.     UINT32 iCharIdenticalFlag = 0
  192.  
  193.     // 判斷較短的字符串中的字符是否全部都在較長(zhǎng)的字符串中的字符中 
  194.     for (iShorterLoopFlag = 0; iShorterLoopFlag < gtShorterStrInfo.iStrCharCount; iShorterLoopFlag ++) 
  195.     { 
  196.         iCharIdenticalFlag = 0
  197.         for (iLongerLoopFlag = 0; iLongerLoopFlag < gtLongerStrInfo.iStrCharCount; iLongerLoopFlag ++) 
  198.         { 
  199.             if (strcmp(gtShorterStrInfo.szStrCharArray[iShorterLoopFlag], gtLongerStrInfo.szStrCharArray[iLongerLoopFlag]) == 0) 
  200.             { 
  201.                 iCharIdenticalFlag = 1;    // 字符相同 
  202.                 break; 
  203.             } 
  204.         } 
  205.  
  206.         if (iCharIdenticalFlag == 0)     // 表示兩個(gè)字符串中有不相同的字符 
  207.         { 
  208.             return 0; 
  209.         } 
  210.     } 
  211.  
  212.     return 1; 

五、程序測(cè)試

我們將編寫(xiě)好的程序“StringContains.c”上傳到Linux機(jī)器,并使用“gcc -g -o StringContainsStringContains.c”命令對(duì)該程序進(jìn)行編譯,生成“StringContains”文件。下面對(duì)程序進(jìn)行詳細(xì)的測(cè)試。

1.輸入較長(zhǎng)字符串為“ABCDF”、較短字符串為“AF”時(shí),程序運(yùn)行情況如下:

  1. Please input the longer string: 
  2. ABCDF 
  3. LongerStr=ABCDF 
  4. Please input the shorter string: 
  5. AF 
  6. ShorterStr=AF 
  7. ABCDF contains AF! 

2.輸入較長(zhǎng)字符串為“AB”、較短字符串為“ABC”時(shí),程序運(yùn)行情況如下:

  1. Please input the longer string: 
  2. AB 
  3. LongerStr=AB 
  4. Please input the shorter string: 
  5. ABC 
  6. ShorterStr=ABC 
  7. ABC is longer than AB, please check! 

3.輸入較長(zhǎng)字符串為“awe”、較短字符串為“rf”時(shí),程序運(yùn)行情況如下:

  1. Please input the longer string: 
  2. awe 
  3. LongerStr=awe 
  4. Please input the shorter string: 
  5. rf 
  6. ShorterStr=rf 
  7. awe doesn't contain rf! 

4.輸入較長(zhǎng)字符串為“`11245”、較短字符串為“45”時(shí),程序運(yùn)行情況如下:

  1. Please input the longer string: 
  2. `11245 
  3. LongerStr=`11245 
  4. Please input the shorter string: 
  5. 45 
  6. ShorterStr=45 
  7. `11245 contains 45! 

5.輸入較長(zhǎng)字符串為“123”、較短字符串為“123 45”時(shí),程序運(yùn)行情況如下:

  1. Please input the longer string: 
  2. 123 
  3. LongerStr=123 
  4. Please input the shorter string: 
  5. 123 45 
  6. ShorterStr=123 
  7. 123 contains 123! 

可見(jiàn),對(duì)于上面考慮到的幾種特殊情況,程序均能做出正確的處理。

六、需求擴(kuò)展

基于本文中的需求和程序,我們可考慮對(duì)需求進(jìn)行以下擴(kuò)展:

1.限制輸入的字符串中只能包含字母,如果包含了其它字符,則直接退出而不進(jìn)行處理。

 

2.如果較短的字符串中的所有字符雖然都在較長(zhǎng)的字符串中,但某個(gè)字符在較短的字符串中出現(xiàn)的次數(shù)大于了在較長(zhǎng)的字符串中出現(xiàn)的次數(shù),那么就認(rèn)為較長(zhǎng)的字符串不包含較短的字符串。

【本文是51CTO專欄作者周兆熊的原創(chuàng)文章,作者微信公眾號(hào):周氏邏輯(logiczhou)】

戳這里,看該作者更多好文

責(zé)任編輯:趙寧寧 來(lái)源: 51CTO專欄
相關(guān)推薦

2016-12-30 13:16:51

字符串算法代碼

2016-12-29 17:14:41

回文串算法代碼

2016-12-29 17:07:59

字符算法代碼

2016-12-30 13:37:50

字符串算法代碼

2016-12-29 15:58:00

字符串子串算法

2023-02-26 22:33:32

字符串排列算法

2009-08-11 10:26:49

C#算法C#字符串反轉(zhuǎn)

2013-05-06 10:54:08

字符串字符串匹配KMP算法

2023-12-15 10:27:01

暴力匹配算法Python字符串

2021-09-03 09:41:36

字符串時(shí)間復(fù)雜度

2010-11-26 09:51:54

MySQL字符串

2023-04-11 08:54:57

字符串匹配算法

2013-05-06 10:49:21

Boyer-Moore算法字符串匹配

2021-09-10 08:31:54

翻轉(zhuǎn)字符串單詞

2024-07-03 11:23:14

2016-12-29 16:25:32

字符串算法代碼

2010-07-14 16:35:52

Perl字符串處理函數(shù)

2010-11-26 10:43:48

MySQL分割字符串

2024-01-09 16:43:49

Shell腳本開(kāi)發(fā)

2010-09-09 11:48:00

SQL函數(shù)字符串
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)