Hdu 1513 & Poj 1159 (LCS) + Hdu 1025 (LIS)
2013-04-19 22:01
309 查看
LCS和LIS的水题,LCS那道用了滚动数组,貌似不用滚动数组会超内存
Hdu 1513 & Poj 1159
Hdu 1025
Hdu 1513 & Poj 1159
/*设原序列S的逆序列为S', 则 最少需要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度*/ #include <cstdio> #include <cstring> #define max(x,y) ((x)>(y)?(x):(y)) int dp[2][5005]; char str[5005],ch[5005]; int main () { int n,i; while (~scanf("%d",&n)) { scanf("%s",&str[1]); for (i=1;i<=n;i++) ch[n-i+1]=str[i]; memset(dp,0,sizeof(dp)); int k,max_len=0; for (int i=1;i<=n;i++) { k=i&1; for (int j=1;j<=n;j++) { if (str[i] == ch[j]) //如果数组从0开始存就改为str[i-1] == ch[j-1] dp[k][j]=dp[k^1][j-1]+1; else dp[k][j]=max(dp[k][j-1],dp[k^1][j]); } } printf("%d\n",n-dp[k] ); } return 0; } /* 10 2514785444 6 */
Hdu 1025
#include <cstdio> int stack[500005],data[500005]; int main () { int n,cas=1; while (~scanf("%d",&n)) { int a,b,i; for (i=1;i<=n;i++) { scanf("%d%d",&a,&b); data[a]=b; } int top=0; stack[0]=-1; for (i=1;i<=n;i++) { if (data[i]>stack[top]) stack[++top]=data[i]; else { int low=1,high=top,mid; while (low<=high) { mid=(low+high)>>1; if (data[i] > stack[mid]) low=mid+1; else high=mid-1; } stack[low]=data[i]; } } printf("Case %d:\n",cas++); if (top==1) printf("My king, at most 1 road can be built.\n\n"); //输出格式坑爹…… else printf("My king, at most %d roads can be built.\n\n",top); } return 0; }
相关文章推荐
- POJ 1159 && HDU 1513 Palindrome(LCS)
- poj 1159 && hdu 1513 Palindrome(LCS)
- HDU 1513 && POJ 1159 Palindrome (DP+LCS+滚动数组)
- HDU 1159 && POJ 1458【lcs】
- hdu 1159 && poj 1458 Common Subsequence (LCS)
- HDU 1159 & POJ 1458 Common Subsequence 【LCS】
- HDU1159 && POJ1458 Common Subsequence (LCS)
- hdu 1159 && POJ 1458Common Subsequence(LCS)
- HDU 1159 & POJ 1458 Common Subsequence(LCS 最长公共子序列O(nlogn))
- POJ 1458 && HDU 1159 Common Subsequence(LCS)
- HDU 1159 & POJ 1458 Common Subsequence (LCS)
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
- 【HDU】1025 - Constructing Roads In JGShining's Kingdom(LIS & 深坑)
- Poj 1631 && Hdu 1950 Bridging signals【LIS】
- HDU 1513 Palindrome (动态规划 & LCS)
- UVA 11368 & POJ 3636 & HDU 1677 Nested Dolls(贪心 + 二分LIS)
- HDU1159 && POJ1458:Common Subsequence(LCS)
- HDU1159 && POJ1458:Common Subsequence(LCS)
- POJ 1458 / HDU 1159 / Southeastern Europe 2003 Common Subsequence (DP&LCS)
- POJ 1631 && HDU 1950 Bridging signals(LIS)