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 };

 

上一篇:LightOJ - Making Huge Palindromes(KMP&回文)


下一篇:LeetCode 234 Palindrome Linked List