分割字符串

来源:互联网 发布:飞行仿真软件 编辑:程序博客网 时间:2024/06/11 00:30
  1. 原帖地址:
  2. http://topic.csdn.net/u/20110526/16/e33f016b-f188-42b7-afce-b4eca9e3546d.html
  3. 采用 回溯加递归的方法,进行了实现。

  4. // test.cpp : Defines the entry point for the console application.
  5. //

  6. /*http://topic.csdn.net/u/20110526/16/e33f016b-f188-42b7-afce-b4eca9e3546d.html
  7. 给你一个没有间隔的字符串“thisisasentence”,如何将他分割成如下的句子:“this is a sentence”。
  8. 提供一个函数用来检验一个字符串是不是单词:bool dic(char* w)
  9. 完成下列的函数。要求效率尽可能快。
  10. bool Detect(char* str)
  11. {

  12. }
  13. */
  14. #include "stdafx.h"
  15. #include <iostream> 
  16. using namespace std;
  17. #include "string.h"


  18. #define MAX_WORD_COUNT 5
  19. #define MAX_DIVIDE 10
  20. #define MAX_WORD_LENGTH 30
  21. int num[MAX_WORD_COUNT]={0};
  22. int divide[MAX_DIVIDE] = {0};
  23. #define FALSE 0
  24. #define TRUE 1
  25. int ii=0, jj=0;
  26. /* If w is a word, store the number of characters in num[] Array.
  27.  * Number of the cases where w can stand for a word is stored in a int, which is marked by the pointer pCnt*/
  28. bool dic(char* w, int* pCnt)
  29. {
  30.     int i=0, j=0;
  31.     bool flag = FALSE;
  32.     for (i=0; i<MAX_WORD_COUNT; ++i)
  33.     {
  34.         num[i]=0;
  35.     }
  36.     int test=0;
  37.     if (!(strncmp(w, "this", strlen("this"))))
  38.     {
  39.         num[j++] = 4;
  40.         ++(*pCnt);
  41.         flag = TRUE;
  42.     }
  43.     if (!strncmp("is", w, strlen("is")))
  44.     {
  45.         num[j++] = 2;
  46.         ++(*pCnt);
  47.         flag = TRUE;
  48.     }
  49.     if (!strncmp("a", w, strlen("a")))
  50.     {
  51.         num[j++] = 1;
  52.         ++(*pCnt);
  53.         flag = TRUE;
  54.     }
  55.     if (!strncmp("sentence", w, strlen("sentence")))
  56.     {
  57.         num[j++] = 8;
  58.         ++(*pCnt);
  59.         flag = TRUE;
  60.     }
  61.     if (!strncmp("sent", w, strlen("sent")))/*test case*/
  62.     {
  63.         num[j++] = 4;
  64.         ++(*pCnt);
  65.         flag = TRUE;
  66.     }
  67.     return flag;
  68. }

  69. bool Detect(char* str)
  70. {
  71.     
  72.     int cnt_tmp = 0;

  73.     if (*str == '\0')
  74.     {
  75.         return TRUE;
  76.     }
  77.     if (dic(str, &cnt_tmp))
  78.     {
  79.         divide[jj++] = num[ii];
  80.         for (ii=0; ii < cnt_tmp; ++ii)
  81.         {
  82.             if (Detect(&str[num[ii]]))
  83.             { 
  84.                 return TRUE;
  85.             }
  86.             else
  87.             {
  88.                 divide[--jj] = 0;
  89.             }
  90.         }
  91.     }
  92.     return FALSE;
  93. }

  94. int main()
  95. {
  96.     /* print*/
  97.     int* pDiv=divide;
  98.     char* pCh="thisisasentence";
  99.     char tmp[MAX_WORD_LENGTH];
  100.         
  101.     memset(divide, 0, MAX_DIVIDE*sizeof(int));

  102.     if (Detect(pCh))
  103.     {
  104.         while (*pDiv)
  105.         {
  106.             memset(tmp, 0, MAX_WORD_LENGTH);
  107.             strncpy(tmp, pCh, *pDiv);
  108.             printf("%s ", tmp);
  109.             pCh = pCh+(*pDiv);
  110.             ++pDiv;
  111.         }
  112.         printf("\n");
  113.     }
  114.     return 0;
  115. }



0 0