</pre>单调递增最长子序列</h2><div class="problem-ins" style="text-align:center">时间限制:<span class="editable highlight" id="problem[time_limit]" style="color:rgb(113,32,21)">3000</span> ms | 内存限制:<span class="editable highlight" id="problem[memory_limit]" style="color:rgb(113,32,21)">65535</span> KB</div><div class="problem-ins" style="text-align:center">难度:<span class="editable highlight" style="color:rgb(113,32,21)">4</span></div></div><div class="clr" style="clear:both; color:rgb(70,70,70); font-family:Tahoma,Arial,sans-serif,simsun; font-size:13px; line-height:19px"></div><dl class="problem-display" style="margin:0px; padding:0px; font-size:14px; color:rgb(70,70,70); font-family:Tahoma,Arial,sans-serif,simsun"><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">描述</dt><dd style="margin:0px; padding:0px">求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4<div class="clr" style="clear:both"></div><dl class="others" style="margin:0px; padding:0px"><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">输入</dt><dd style="margin:0px; padding:0px">第一行一个整数0<n<20,表示有n个字符串要处理随后的n行,每行有一个字符串,该字符串的长度不会超过10000</dd><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">输出</dt><dd style="margin:0px; padding:0px">输出字符串的最长递增子序列的长度</dd><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">样例输入</dt><dd style="margin:0px; padding:0px"><pre id="sample_input" style="margin-top:0px; margin-bottom:0px; padding:5px 10px; font-family:Consolas,'Courier New','DejaVu Sans Mono','Droid Sans Mono',monospace; background-color:rgb(239,239,239); border:1px solid rgb(204,204,204); min-height:20px; line-height:1.5em">3
aaa
ababc
abklmncdefg
样例输出
1
3
7
#include<stdio.h>
#include<string.h>
int G[10010][10010];
int len;
int dp(int i) {
// int &ans=d[i];
// if(ans>0) return ans; //记忆化
int ans;
ans=1;
for(int j=0; j<len; j++) {
if(G[i][j]) {
if(ans < dp(j) + 1) {
ans = dp(j)+1;
}
}
}
return ans;
}
int main()
{
int n;
int i, j;
int ans = 0;
char c[10010], c1[10010];
scanf("%d", &n);
memset(G, 0, sizeof(G));
while(n--)
{
scanf("%s",c);
strcpy(c1,c);
len = strlen(c);
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
{
if(c[i]<c1[j]) G[i][j] = 1;
}
for(i=0;i<len;i++)
{
if(dp(i)>ans)
ans = dp(i);
}
printf("%d\n",ans);
}
return 0;
}
http://blog.csdn.net/whjkm/article/details/38582411
</pre>单调递增最长子序列</h2><div class="problem-ins" style="text-align:center">时间限制:<span class="editable highlight" id="problem[time_limit]" style="color:rgb(113,32,21)">3000</span> ms | 内存限制:<span class="editable highlight" id="problem[memory_limit]" style="color:rgb(113,32,21)">65535</span> KB</div><div class="problem-ins" style="text-align:center">难度:<span class="editable highlight" style="color:rgb(113,32,21)">4</span></div></div><div class="clr" style="clear:both; color:rgb(70,70,70); font-family:Tahoma,Arial,sans-serif,simsun; font-size:13px; line-height:19px"></div><dl class="problem-display" style="margin:0px; padding:0px; font-size:14px; color:rgb(70,70,70); font-family:Tahoma,Arial,sans-serif,simsun"><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">描述</dt><dd style="margin:0px; padding:0px">求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4<div class="clr" style="clear:both"></div><dl class="others" style="margin:0px; padding:0px"><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">输入</dt><dd style="margin:0px; padding:0px">第一行一个整数0<n<20,表示有n个字符串要处理随后的n行,每行有一个字符串,该字符串的长度不会超过10000</dd><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">输出</dt><dd style="margin:0px; padding:0px">输出字符串的最长递增子序列的长度</dd><dt style="margin:1em 0px 0.2em; padding:0px; color:rgb(113,32,21); font-size:16px; font-weight:bold">样例输入</dt><dd style="margin:0px; padding:0px"><pre id="sample_input" style="margin-top:0px; margin-bottom:0px; padding:5px 10px; font-family:Consolas,'Courier New','DejaVu Sans Mono','Droid Sans Mono',monospace; background-color:rgb(239,239,239); border:1px solid rgb(204,204,204); min-height:20px; line-height:1.5em">3
aaa
ababc
abklmncdefg
1 3 7
http://blog.csdn.net/whjkm/article/details/38582411#include<stdio.h> #include<string.h> int G[10010][10010]; int len; int dp(int i) { // int &ans=d[i]; // if(ans>0) return ans; //记忆化 int ans; ans=1; for(int j=0; j<len; j++) { if(G[i][j]) { if(ans < dp(j) + 1) { ans = dp(j)+1; } } } return ans; } int main() { int n; int i, j; int ans = 0; char c[10010], c1[10010]; scanf("%d", &n); memset(G, 0, sizeof(G)); while(n--) { scanf("%s",c); strcpy(c1,c); len = strlen(c); for(i=0;i<len;i++) for(j=i+1;j<len;j++) { if(c[i]<c1[j]) G[i][j] = 1; } for(i=0;i<len;i++) { if(dp(i)>ans) ans = dp(i); } printf("%d\n",ans); } return 0; }