链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> int max(int a,int b) { return a>b?a:b; } int main() { char s[105],t[105]; int i,j,k=0,m,n,dp[105][105]; while(gets(s)!=NULL){ if(strcmp(s,"#"…
uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int a[105], b[105], dp[105][105]; int main() { int n, m, Case = 1; while (scanf(&quo…
uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you really don't know which cities you should visit. So, you ask your parents for help. Your mother says "My son, you MUST visit Paris, Madrid, Lisboa an…
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做LCS处理,最后的结果即为所求. 核心状态转移方程: if(c1[i] == c2[j]) dp[i][j] =dp[i-1][j-1]+1; else dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 这里还有一个小技巧,当希望读取的字符数据,不是从字符数组的第0个元素…
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化为LCS(最长公共子序列)问题. 推荐一篇写的比较好的博文: 动态规划求解最长公共子序列(LCS) 核心的状态转移方程: if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1] +1; else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);…
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algorithm> #include<math.h> #include <string.h> #include<vector> #include<queue> using namespace std; ][]; ],map2[]; int main() { int…
Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an ancient Empire, there were two towers of dissimilar shapes in two different cities. The towers were built by putting circular tiles one upon another. Each…
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algorithm> #include<math.h> #include <string.h> #include<vector> #include<queue> #include<set> #include<iterator> using na…
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Ali is fascinated about twin towers. So, in this problem you are working as his assistant, and you have to help him making a large twin towers. For this…
The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing tall, though a building's lost our faith will never fall.Twin towers the world hears your call, though you're gone it only strengthens our resolve.We co…
The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing tall, though a building's lost our faith will never fall. Twin towers the world hears your call, though you're gone it only strengthens our resolve. We…
题目大意: 两题几何水题. 1.UVA 11646 - Athletics Track 如图,体育场的跑道一圈400米,其中弯道是两段半径相同的圆弧,已知矩形的长宽比例为a:b,求长和宽的具体数值. 2.UVA 11817 - Tunnelling the Earth 给出地球上起点和终点(均用度数的经纬度表示),从起点出发,可以沿着球面最短路径走.也可以钻隧道,走直线.求这两种方法的路程差. 题解: 1.UVA 11646 - Athletics Track http://uva.online…
题目大意:给长度N的A1.....An 求(Ai-Aj)MAX 枚举n^2 其实动态维护最大值就好了 #include<iostream> #include<cstdio> using namespace std; ],n,ans,MAX; int main() { scanf("%d",&t); while (t--) { scanf("%d",&n); ;i<=n;i++) scanf("%d",…
裸最长公共子序列,直接贴代码 #include<cstdio> #include<iostream> #include<algorithm> #include<string.h> using namespace std; ][]; ],str2[]; int main() { int len1,len2; while(gets(str1)&&gets(str2)) { memset(dp,,sizeof(dp)); len1=strlen(s…
 History Grading  Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order…
题目连接:111 - History Grading 题目大意:给出一个n 代表序列中元素的个数, 然后是一个答案, 接下来是若干个同学的答案(直到文件结束为止), 求出两个序列的最长公共子序列, 注意给出的答案均是以该事件处于第几个发生的, 例如 :2 3 4 1 即是 对应第1个事件在第2个发生,第2个事件在第3个发生 ...转换一下就是  4 1 2 3. 解题思路:最长公共子序列问题, 状态转移方程 d[i][j] = 0( i == 0 ||  j == 0) d[i - 1] [j…
input n,p,q 2<=n<=250 1<=p,q<=n*n 1 a1 a2 a3 ... ap 1<ai<n*n,ai!=aj 1 b1 b2 b3 ... bq 1<bi<n*n,bi!=bj output 最长公共子序列个数 做法:将b数组中的数变为a数组中数的下标,a中不存在的数可以去掉,然后求LIS即可 #include <cstdio> #include <queue> #include <cstring>…
  Virus  We have a log file, which is a sequence of recorded events. Naturally, the timestamps are strictly increasing. However, it is infected by a virus, so random records are inserted (but the order of original events is preserved). The backup log…
最长回文子序列可以用求解原串s和反转串rv的LCS来得到,因为要求回文串分奇偶,dp[i][j]保存长度, 要求字典序最小,dp[i][j]应该表示回文子序列的端点,所以边界为单个字符,即i+j=len+1. 这题最麻烦的地方在于字典序,我是写了个比较函数,有点暴力(常数大). 也可以反着定义,这时结点就要保存那个状态的字符串了(这样定义比较字典序的时候常数小) #include<bits/stdc++.h> using namespace std; #define MP make_pair…
又是一道比较复杂的模拟题.题中有两种队列,一种是总队列,从前向后.其他的是各个团体的小队列,因为入队的人如果有队友的话,会优先进入团体队列. 所以我们先设置两个队列和一个map,设置map倒是可以不用担心开多大数组这样的问题.然后开两个队列,一个是基本的q1,另一个是q2[1010] 一般的STL容器如string vector deque都是有两种直接初始化的方法(不严谨)一种是a(maxn)这个和一维数组差不多,直接赋给里面多少元素,另一种就是a[maxn]这个差不多是一个容器的数组,每一个…
题目大意 计算循环小数的位数,并且按照格式输出 怎么做 一句话攻略算法核心在于a=a%b*10,用第一个数组记录被除数然后用第二个数组来记录a/b的位数.然后用第三个数组记录每一个被除数出现的位置好去寻找循环节的位置. 我的代码(算法还是借鉴) #include <iostream> #include <cstring> using namespace std; int ar[300000]; int re[300000]; int lc[300000]; int main() {…
先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[i][j]代表在这个矩形中终点是到(i,j)这个点的满足题意的直线条数,那么,用dp的话就可以得出递推关系:由长和宽分别小1的左右两个矩形中满足题意的线的条数减去他们共有的矩形中满足的线的条数(容斥减去重复部分),之后还要判断从最左上角的点(1,1)到(i,j)是否可以组成一条线,这个条件是gcd(…
题目描述:求最长公共子序列 若给定序列X={x1,x2,...,xm},另一序列Z={z1,z2,...,zk},是X的子序列是指存在一个严格递增的下标序列{i1,i2,...,ik}使得对所以j=1,2,...,k有zj=x(ij) 例如Z={B,C,D,B}是序列X={A,B,C,B,D,A,B}的子序列,相应的递增下标序列为{2,3,5,7} 分析:DP的经典题 状态表示:d[i,j]记录序列x(i)和y(j)的最长公共子序列,其中x(i)={x1,x2,...,xi},y(j)={y1,…
题意: 输入n个序列,求出一个最大长度的字符串,使得它在超过一半的DNA序列中连续出现.如果有多解,按照字典序从小到大输出所有解. 分析:这道题的关键是将多个字符串连接成一个串,方法是用不同的分隔符把所有原串拼接起来.接下来,就可以求这个新串的后缀数组和 height 数组, 然后二分答案,没次只需判断是非有一个长度为p的串在超过一半的串中出现过,判断方法是扫描一遍height数组,把它分成若干段,每当height[i] < p时,开辟一个新段,然后判断之前段是否包含了超过 n/2个原串后缀,那…
第一次接触一个这最长公共上升子序列 不过其实搞清楚了跟最长公共子序列和 最长上升子序列如出一辙 两重循环,对于当前不相等的,等于前一个的值,相等的,等于比当前A[i]小的最大值+1.弄个临时变量记录最大值即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int dp[2][1010]; int A[1010…
题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上去,结果TLE+RE. 仔细一看:n<=250,那么二维数组就得开250*250*250*250了,显然RE了. 所以我想着用滚动数组来存储,但是TLE又拦住了我的去路. O(n^2)的效率就是62500*62500,大于10^8,所以1s之内完不成,所以要想别的办法. 偶然在网上找到了O(nlog…
双塔DP. dp[i][j]表示前i个物品,分成两堆(可以不全用),价值之差为j的时候,较小一堆的价值为dp[i][j]. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; ][ + ]; ]; int n, sum; void read() { ; i <= n; i++) scanf("%d",…
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][height_right],但是这样显然回boom内存,所以不妨直接考虑两座塔之间的差于是便有了dp[i][j]表示考虑到第几个时两座塔差值是多少,然后就是递推了,要么加积木嫁到高的塔上要么就嫁到底的塔上要么都不嫁,这样递推方程就好写了.如果这样还是boom内存的话可以考虑用滚动优化i这一维. #i…
UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿某一种排序算法(例如插入排序),递归输出答案. 时间复杂度:\(O(n!)\) UVA 111 题目描述:求两个数组的最长公共子串. solution DP 时间复杂度:\(O(n^2)\) UVA 112 题目描述:给出一棵树,判断是否存在一条从根到叶子节点的路径,路径上所有节点的权值和等于给定的…
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes…