算法题11 字符串的所有对称子串

寻找对称子串算法
本文介绍了一种用于找出字符串中所有对称子串的有效算法。通过对字符串进行内外双向扫描,该算法能够识别两种类型的对称子串:一种是以单个字符为中心的对称子串,另一种是完全对称的子串。文章提供了详细的实现代码。

题目

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

分析

  对称字符串无非两种情况,一是以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 }

 

转载于:https://www.cnblogs.com/wangzaizhen/p/5177059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值