Title: given a string, please find out the length of the longest substring that does not contain duplicate characters.

Source: LeetCode

Link: https://leetcode-cn.com/problems/longest-palindromic-substring/

Train of thought:

This topic is a good topic for learning dynamic planning. You can learn a lot from the homepage of @ https://www.liwei.party/ recommended by leetcode, which also refers to his solution ideas. (it's so delicious. Worship the big man...)

Dynamic programming: decompose the original problem into sub problem solving and divide and conquer

There are two ideas:

1. Top down: you decompose the problem from the top until you see that the problem has been decomposed to the minimum and solved, and then just return the saved answer. It's called memory storage

2. Bottom up: you can start solving smaller sub problems directly to get the best solution. In this process, you need to make sure that you solve the sub problem before you solve it. This can be called a table filling algorithm.

For palindrome as a special condition, it's easy to think of the subproblem that the palindrome string is also a palindrome string after turning around and ending.

A two-dimensional array of tags is used to determine whether the string between i and j is a palindrome substring. If so, the state must be the same as that between i+1 and j-1. Finally, just get the longest substring marked 1.

Central diffusion

The central diffusion is a wonderful idea. i didn't think about the even number of substrings at the beginning. The form like abba is also a palindrome. Then i think it needs to be discussed according to the situation. It's OK to do so, but it's a little tedious. You can use two position markers, start and end. If you want to find odd substring, you can point to the same position at the beginning. If you want to judge even substring, you can point to i and i+1, and center diffusion is only implemented in python.

Dynamic planning: C language:

#include<stdio.h> #include<string.h> #include<stdlib.h> char *s="qcabcbad"; //point of char // char *longestPalindrome(char *s){ int len=strlen(s); //Initializes a two-dimensional pointer array int **flag; int start=0; flag = (int **)malloc(sizeof(int *) * len); for(int i=0; i<len; i++) { flag[i] = (int *)malloc(sizeof(int) * (len)); } int maxlen=0; for(int i=0;i<len;i++) { for(int j=0;j<len;j++) { flag[i][j]=0; if(i==j) { flag[i][j]=1; } } } for(int j =0; j<len;j++) { for(int i =0; i<j;i++) { if(s[i]==s[j]) { if(j-i<3) { flag[i][j]=1; } else { flag[i][j]= flag[i+1][j-1]; } } if(flag[i][j]==1) { if(maxlen<=j-i+1) { maxlen=j-i+1; start=i; } } } } s[start + maxlen] = '\0'; //Modify end position return s + start; } //main function void main(){ //longestPalindrome(s); printf("%s",longestPalindrome(s)); }

python

class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ slen=len(s) if slen < 2: return s flag=[[0 for _ in range(slen)] for _ in range(slen)] print(flag) start=0 maxlen=1 for i in range(slen): flag[i][i] =1 print(flag) for j in range(1,slen): for i in range(0,j): if s[i]==s[j] : if j-i<3 : flag[i][j]=1 else: flag[i][j] = flag[i+1][j-1] if(flag[i][j]==1): if(maxlen<j-i+1): maxlen=j-i+1 start=i return s[start:start+maxlen] #Centerdiffusionalgorithm def getlongetst(self,s): slen = len(s) if slen < 2: return s maxlen=1 maxstr=s[0] for i in range(len(s)): s1=self.centerspread(s,i,i) s2=self.centerspread(s,i,i+1) #print("11") if len(s1)>=len(s2) : longstr=s1 else: longstr=s2 if len(longstr)>maxlen: maxlen=len(longstr) maxstr=longstr return maxstr def centerspread(self,s,start,end): i=start j=end while i>=0 and j< len(s) and s[i]==s[j]: i-=1 j+=1 return s[i+1:j] if __name__ == '__main__': s="aca" solution=Solution() print(solution.getlongetst(s))