洛谷—— P2543 [AHOI2004]奇怪的字符串

https://www.luogu.org/problem/show?pid=2543#sub

题目描述

输入输出格式

输入格式:

 

输入文件中包含两个字符串X和Y。当中两字符串非0即1。序列长度均小于9999。

 

输出格式:

 

X和Y的最长公共子序列长度。

 

输入输出样例

输入样例#1:
01010101010 00000011111
输出样例#1:
6
输入样例#2:
01011 010010101111111111
输出样例#2:
5


我的妈呀最长子序列、、、
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 const int N(10000);
 8 char s1[N],s2[N];
 9 int f[N>>1][N>>1];
10 
11 int main()
12 {
13     cin>>s1>>s2;
14     int n1=strlen(s1),n2=strlen(s2);
15     for(int i=1;i<=n1;i++)
16       for(int j=1;j<=n2;j++)
17         if(s1[i-1]==s2[j-1]) f[i][j]=f[i-1][j-1]+1;
18         else f[i][j]=max(f[i-1][j],f[i][j-1]);
19     cout<<f[n1][n2];
20     return 0;
21 }

 

posted @ 2017-08-12 21:45  Aptal丶  阅读(299)  评论(0编辑  收藏  举报