保研面试 英文算法题 临时抱佛脚训练思路系列——动态规划

专题一 动态规划


A1007 Maximum Subsequence Sum

Given a sequence of K integers { N 1 N_1 N1 , N 2 ​ N_2​ N2​ , …, N ​ K ​ N​_K​ NK​ }. A continuous subsequence is defined to be { N ​ i N​_i Ni​​ , N ​ i + 1 ​ N​_{i+1​} Ni+1​ , …, N ​ j ​ N​_j​ Nj​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

题意

给一个数字序列 a 1 a_1 a1, a 2 a_2 a2,…, a n a_n an,求最大连续子序列和,并输出最大值以及子序列两端的数值。

思路

动态规划

  • dp[i]表示以a[i]作为结尾的连续子序列的和的最大值;
  • s[i]表示以a[i]作为结尾的连续子序列和最大时子序列开头的位置;
  • 状态转移方程
if dp[i-1]+a[I]>dp[i]
	dp[i]=dp[i-1]+a[i],s[i]=s[i-1];
else 
	dp[i]=a[i],s[i]=i;

dp[0]=a[0]
  • 单独判断全都是负数的情况

A1045 Favorite Color Stripe

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva’s favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva’s favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

题意

给m种颜色作为喜欢的颜色(同时是有一定的顺序的),然后给出一串长度为L的颜色序列。现在要在这一L长的颜色序列里面找到喜欢的颜色,同时要满足给定的喜欢顺序(不要求每一种喜欢的颜色都出现),找出满足这样的要求的最长的字串的长度。

思路
  • 首先,对于这个有一定顺序的喜欢的颜色的处理,将其映射到一个数组中去,按照顺序分别对应0,1,2 ,3,4,5…;
memset(HashTable,-1,sizeof(HashTable));

for(int i=0;i<m;i++){
	scanf("%d",&x);
	HashTable[x]=i;
}	
  • 其次,对这一L长的颜色串遍历,用一个标志数组hashtable[i],喜欢则为true,不喜欢为false,同时将所有属于喜欢的颜色按照顺序存入新的数组A[]中去
num=0;

for(int i=0;i<L;i++){
	scanf("%d",&x);
	if(HashTable[x]>=0)
		A[num++]=HashTable[x];
}		
  • 在A[]数组中找最长的不连续不下降子序列
for(int i=0;i<num;i++){
	dp[i]=1;
	for(int j=0;j<i;j++){
		if(A[j]<=A[i]&&dp[j]+1>dp[i])
			dp[i]=dp[j]+1;
	}
}
  • 最后找dp[]的最大值即可

A1040 Longest Symmetric String

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.

题意

找出字符串中的最长回文子串,输出长度

思路

动态规划法

  • dp[i][j]表示s[i]至s[j]所表示的字串是否是回文子串,是则为1,不是则为0;
  • 状态转移方程
if(s[i]==s[j])
	dp[i][j]=dp[i+1][j-1];
else
	dp[i][j]=0;

边界:dp[i][i]=1;dp[i][i+1]=(s[i]==s[i+1])?1:0
  • 对子串的长度从2开始讨论
for(int i=0;i<len;i++){
	dp[i][i]=1;
	if(i<len-1){
		if(s[i]==s[i+1]){
			dp[i][i+1]=1;
			ans=2;//记录最长回文串的长度
		}
	}
}

for(int L=3;L<=len;L++){
	for(int i=0;i+L-1<len;i++){
		int j=i+l-1;
		if(s[i]==s[j]&&dp[i+1][j-1]==1){
			dp[i][j]=1;
			ans=L;
		}
	}
}

A1068 Find More Coins

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 1 0 4 10^4 104​​ coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

题意

有N枚硬币,给出每种硬币的面值,现在要用这些硬币去支付价值为M的东西。
能否找到满足的方案,如果没有,则输出No solution;如果有则按照面值从小到大输出硬币的面值大小,如果有多种方案,则输出字典序最小的那一个。

思路

完全背包的问题。
用choice[][]数组来记录方案的路径,最后倒着查看采用了哪些面值

w[]//钱币的面值

sort(w+1,w+n+1,cmp);//逆序排列

for(int i=1;i<=n;i++){
	for(int v=m;v>=w[i];v--){
		if(dp[v]<=dp[v-w[i]]+w[i]){
			dp[v]=dp[v-w[i]]+w[i];
			choice[i][v]=1;
		}
		else
			choice[i][v]=0;
	}
}

if(dp[m]!=m)
	printf("No solution!\n");
else{
	//记录最优路径
	int k=n,num=0,v=m;
	while(k>=0){
		if(choice[k][v]==1){
			flag[k]=true;
			v-=w[k];
			num++;//用来标记总共用到多少种不同的面值
		}
		else
			flag[k]=false;
		k--;
	}
	//最后输出方案,之前将面值已经逆序排序过了,所以上面k从n开始是没有问题的,就是字典序最小的情况。
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值