求對稱字串的最大長度
阿新 • • 發佈:2019-02-08
程式碼還有很多改進的地方,留給讀者自由發揮//返回最長的對稱字串的長度,如google,對稱字串是goog,最大長度為4 int symStr(char* source) { if('\0' == *source) return 0; int maxLength = 1; char* current = source; while('\0' != *current) { int max1 = 0; int symIndex = 0; // 對稱遊標 //(1) googllgox,對稱字元個數為偶數,比如這樣返回6,此時current + i + 1 while('\0' != *(current - symIndex - 1) && '\0' != *(current + symIndex) && *(current - symIndex - 1) == *(current + symIndex)) { max1 += 2; symIndex++; } maxLength = max(max1, maxLength); max1 = 1; symIndex = 1; //(2)baidudiac,對稱字元個數為奇數,比如這樣返回7 while('\0' != *(current - symIndex) && '\0' != *(current + symIndex) && *(current - symIndex) == *(current + symIndex)) { max1 += 2; symIndex++; } maxLength = max(max1, maxLength); ++current; } return maxLength; }