1040 Longest Symmetric String (25分)
-
dp动态规划
-
\(dp[i][j]\)表示\(s[i] \to s[j]\)是否对称,是为\(1\),否为\(0\)
-
状态转移方程:\(dp[i][j] = dp[i+1][j-1] \&\& (s[i]==s[j])\)
-
边界条件:\(dp[i][i]=1,dp[i][i+1]=(s[i]==s[i+1])\)
-
-
读取字符串时,使用
cin.getline(char* s, streamsize n)
代替gets(char* str)
gets(char* str)
is unsafe. It's recommended to usefgets(char* str, int num, FILE* stream)
instead.[1]However, I think it is trivial to process the string after using
fgets()
.const int N = 200; char str[N]; fgets(str, N, stdin); int i = 0; while(str[i] != '\n') i++; str[i] = '\0'
Thus, I use
cin.getline()
for its concision.
👉 code