HDU 2859 Phalanx (dp)
生活随笔
收集整理的這篇文章主要介紹了
HDU 2859 Phalanx (dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2859
給你一個n*n的矩陣,問你最大的對稱度是多少(左下右上為對稱線)
dp[i][j]表示i行j列元素的最大對稱度
每到一個元素的時候,往上邊和右邊擴展看字符最優的對稱長度 與dp[i - 1][j - 1]進行比較取最優即可。
1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <vector> 8 #include <cmath> 9 #include <ctime> 10 #include <list> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL; 15 typedef pair <int, int> P; 16 const int N = 1e3 + 5; 17 int dp[N][N]; 18 char str[N][N]; 19 20 int main() 21 { 22 int n; 23 while(~scanf("%d", &n) && n) { 24 for(int i = 1; i <= n; ++i) { 25 scanf("%s", str[i] + 1); 26 } 27 memset(dp, 0, sizeof(dp)); 28 int res = 1; 29 for(int i = 1; i <= n; ++i) { 30 for(int j = n; j >= 1; --j) { 31 int len = 1; 32 while(str[i - len][j] == str[i][j + len] && i - len > 0 && j + len <= n) { 33 ++len; 34 } 35 dp[i][j] = max(1, min(dp[i - 1][j + 1], len - 1) + 1); 36 res = max(res, dp[i][j]); 37 } 38 } 39 printf("%d\n", res); 40 } 41 return 0; 42 }?
轉載于:https://www.cnblogs.com/Recoder/p/5781948.html
總結
以上是生活随笔為你收集整理的HDU 2859 Phalanx (dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu中的launcher
- 下一篇: 基础 - jQuery选项卡