首页 > 编程语言 > 详细

算法题11 所有对称子串

时间:2016-02-02 14:19:29      阅读:169      评论:0      收藏:0      [点我收藏+]

题目:

  给定一个字符串,求其中所有的对称子串

分析:

  对称字符串无非两种情况,一是以1个字符为中心对称,如"abcba",一是完全对称,如"abccba"。对于字符串对称的判断,从内往外查找比较方便

代码

  

 1 int SymmtricSubStrings(char* str,vector<string>& vSubStrs)
 2 {
 3     if (str==NULL)
 4     {
 5         return -1;
 6     }
 7     string s=str;
 8 
 9     //遍历字符串
10     char* p=str+1;
11     while (*p!=\0)
12     {
13         //odd nums
14         char* pre=p-1;
15         char* next=p+1;
16         int len=1;
17         while (pre>=str&&*next!=\0&&*pre==*next)
18         {
19             len=len+2;
20             pre--;
21             next++;
22         }
23         if (len>1)
24         {
25             vSubStrs.push_back(s.substr(pre-str+1,len));
26         }
27 
28         //even nums
29         pre=p-1;
30         next=p;
31         len=0;
32         while (pre>=str&&*next!=\0&&*pre==*next)
33         {
34             len=len+2;
35             pre--;
36             next++;
37         }
38         if (len>1)
39         {
40             vSubStrs.push_back(s.substr(pre-str+1,len));
41         }
42 
43         p++;
44     }
45 
46     return 0;
47 
48 }

 

算法题11 所有对称子串

原文:http://www.cnblogs.com/wangzaizhen/p/5177059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!