其他分享
首页 > 其他分享> > Leetcode-5150 Longest Chunked Palindrome Decomposition(段式回文)

Leetcode-5150 Longest Chunked Palindrome Decomposition(段式回文)

作者:互联网

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 
 3 bool judge(string s,int i,int j,int len)
 4 {
 5     _for(k,0,len)
 6         if(s[i]!=s[j])
 7             return false;
 8         else
 9             i ++,j ++;
10     return true;
11 }
12 class Solution
13 {
14     public:
15         int longestDecomposition(string text)
16         {
17             int i = 0;
18             int j = text.size()-1;
19             int lastj = j;
20             int rnt = 0;
21             while(i<j)
22             {
23                 if(text[i]==text[j] && judge(text,i,j,lastj-j+1))
24                 {
25                     rnt ++;
26                     i += lastj-j+1;
27                     lastj = j-1;
28                 }
29                 j --;    
30             }
31             rnt *= 2;
32             if(i<=j)
33                 rnt ++;
34             return rnt;
35         }
36 };

 

标签:Palindrome,return,string,int,5150,len,++,Decomposition,text
来源: https://www.cnblogs.com/Asurudo/p/11297896.html