Chinaunix首页 | 论坛 | 博客
  • 博客访问: 762289
  • 博文数量: 230
  • 博客积分: 6330
  • 博客等级: 准将
  • 技术积分: 2188
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-10 15:55
个人简介

脚踏实地

文章分类

全部博文(230)

文章存档

2017年(1)

2016年(7)

2015年(10)

2014年(32)

2013年(24)

2012年(33)

2011年(50)

2010年(30)

2009年(43)

分类: C/C++

2011-07-17 23:24:44


  1. 原帖地址:
  2. 采用 回溯加递归的方法,进行了实现。

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

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

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


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

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

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

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

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

上一篇:Voip相关

下一篇:一些关键字--volatile

给主人留下些什么吧!~~