算法题11 字符串的所有对称子串
生活随笔
收集整理的這篇文章主要介紹了
算法题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
總結
以上是生活随笔為你收集整理的算法题11 字符串的所有对称子串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络安全架构
- 下一篇: jenkins发布docker项目 ha