CSU 1060 Nearest Sequence

题意:求三个序列的最长公共子序列。

思路:一开始以为只要求出前两个的LCS,然后和第三个再求一遍LCS就是答案了。但是样例就对我进行啪啪啪打脸了。实际上就跟两个序列的差不多,换成三维的就行了。

代码:需要注意的是max速度比较慢,最后改成if

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 using namespace std;
 5 const int N=111;
 6 int dp[N][N][N];
 7 char a[N],b[N],c[N];
 8 void LCS()
 9 {
10     int la=strlen(a),lb=strlen(b),lc=strlen(c),i,j,k;
11     memset(dp,0,sizeof(dp));
12     for(i=1;i<=la;i++)
13     {
14         for(j=1;j<=lb;j++)
15         {
16             for(k=1;k<=lc;k++)
17             {
18                 if(a[i-1]==b[j-1]&&a[i-1]==c[k-1])
19                     dp[i][j][k]=dp[i-1][j-1][k-1]+1;
20                 else
21                 {
22                     if(dp[i-1][j][k]>dp[i][j][k])    dp[i][j][k]=dp[i-1][j][k];
23                     if(dp[i-1][j-1][k]>dp[i][j][k])    dp[i][j][k]=dp[i-1][j-1][k];
24                     if(dp[i-1][j][k-1]>dp[i][j][k])    dp[i][j][k]=dp[i-1][j][k-1];
25                     if(dp[i][j-1][k]>dp[i][j][k])    dp[i][j][k]=dp[i][j-1][k];
26                     if(dp[i][j-1][k-1]>dp[i][j][k])    dp[i][j][k]=dp[i][j-1][k-1];
27                     if(dp[i][j][k-1]>dp[i][j][k])    dp[i][j][k]=dp[i][j][k-1];
28                 }
29             }
30         }
31     }
32 }
33 int main()
34 {
35     while(scanf("%s%s%s",a,b,c)!=EOF)
36     {
37         LCS();
38         printf("%d\n",dp[strlen(a)][strlen(b)][strlen(c)]);
39     }
40     return 0;
41 }
时间: 2024-10-22 01:39:04

CSU 1060 Nearest Sequence的相关文章

CSU 1060

1060: Nearest Sequence Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 370  Solved: 118[Submit][Status][Web Board] Description Do you remember the "Nearest Numbers"? Now here comes its brother:"Nearest Sequence".Given three sequences of

STL or 线段树 --- CSU 1555: Inversion Sequence

Inversion Sequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一个序列a[n],要你按照要求去构造一个序列b. 序列a[i]表示序列b中的i前面有a[i]个数比i大. 转换一下就是: 已知一个连续的序列(1,2,3,4,...),然后告诉了我们这个序列中每个数前面比本身大的个数,根据这些条件将这个序列调整顺序,找到满足条件的序列. analyse: STL大法好

csu 1555: Inversion Sequence(vector)

1555: Inversion Sequence Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 360  Solved: 121 [Submit][Status][Web Board] Description For sequence i1, i2, i3, - , iN, we set aj to be the number of members in the sequence which are prior to j and greater

CSU 1555 Inversion Sequence 给出逆序数求排列 splay

题目链接:点击打开链接 题意: 给出逆序数的值,求原序列(一个1-N的排列) 1, 2, 0, 1, 0 表示1的逆序数是1,2的逆序数是2,3的逆序数是0··· 思路: 从最后一个数开始插,每次插到当前序列的第a[i]个数.. splay模拟 == 这个方法比较直(wu)观(nao),别的方法并没有想出来.. #include <cstdio> #include <iostream> #include <cstring> #include <queue>

CSU 2005 Nearest Maintenance Point(最短路+bitset)

https://vjudge.net/problem/CSU-2005 题意:给出带权值的图,图上有一些特殊点,现在给出q个询问,对于每个询问,输出离该点最近的特殊点,如果有多个,则按升序输出. 思路:因为有多次查询,不可能对于每个询问都去跑一遍最短路.必须以特殊点为起点跑一遍最短路,但是这样路径的记录就是问题了.正解是用bitset来记录状态,在最短路松弛更新状态时,继承前驱节点即可. 1 #include<iostream> 2 #include<algorithm> 3 #i

CSU-ACM2014年校队选拔赛指导赛解题报告

•Problem A  CSU 1065                               贪心 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1000010; 6 struct Node{ 7 int a,b; 8 bool operator < (const Node& rhs)

Inversion Sequence(csu 1555)

Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence which are prior to j and greater to j at the same time. The sequence a1, a2, a3, … , aN is referred to as the inversion sequence of the original sequen

CSU 1515 Sequence (莫队算法)

题意:给n个数,m个询问.每个询问是一个区间,求区间内差的绝对值为1的数对数. 题解:先离散化,然后莫队算法.莫队是离线算法,先按按询问左端点排序,在按右端点排序. ps:第一次写莫队,表示挺简单的,不过这题之前乱搞一气一直TLE,莫队还是很强大的. 代码: #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstring>

CSU 1515 Sequence

莫队算法+map #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<algorithm> using namespace std; const int maxn=200000+10; int n,t,a[maxn],cnt[maxn*10],pos[maxn]; struct X { int l,r,id; } s[maxn]; int L,R; i