1040 Longest Symmetric String (25分)

  • dp动态规划

    1. \(dp[i][j]\)表示\(s[i] \to s[j]\)是否对称,是为\(1\),否为\(0\)

    2. 状态转移方程:\(dp[i][j] = dp[i+1][j-1] \&\& (s[i]==s[j])\)

    3. 边界条件:\(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 use fgets(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


  1. gets函数在pat里怎么用不了? ↩︎

posted @ 2020-08-02 09:28  Blind  阅读(94)  评论(0编辑  收藏  举报