4.Coincidence
问题描述:find a longest common subsequence of two strings.(找到两个字符串的最长公共子序列)
输入:first and second line of each input case contain two strings of lowercase character a...z. there are no spaces before, inside or after the strings. length of the strings do not exceed 100.(第一行第二行分别输入两个包含a...z的字符串,不包含空格,字符串长度不超过100)
输出:for each case, output k--the length of a longest common subsequence in one line(每一种情况,输出最长公共子序列 的长度)
//最长公共子序列长度
#include<stdio.h>
#include<string.h>
int buf[101][101];
int max(int a, int b){return a>b?a:b;}
char first[101],second[101];
int main(){
while(scanf("%s%s",first,second)!=EOF){
int l1=strlen(first);
int l2=strlen(second);
for(int i=0;i<l1;i++)buf[i][0]=0;
for(int j=0;j<l2;j++)buf[0][j]=0;
for(int i=1;i<l1;i++){
for(int j=1;j<l2;j++){
if(first[i]==second[j])buf[i][j]=buf[i-1][j-1]+1;
else buf[i][j]=max(buf[i-1][j],buf[i][j-1]);
}
}
printf("最长公共字符串长度为:%d\n",buf[l1-1][l2-1]);
}
return 0;
}
背包问题——0-1背包、完全背包、多重背包
5.采药问题(0-1背包问题:每种草药有两种状态,在背包中;不在背包中)问题描述:在固定的时间里采一些草药,使得草药的价值最大
输入:输入的第一行有两个整数T(1<=T<=1000)和M(1<=M<=100),T代表总共能够用来采药的时间,M代表山洞里草药的数目。接下来的M行每行包括两个在1到100之间的整数,分别表示采摘某株草药的时间和这株草药的价值。
输出:可能有多组测试数据,对于每组数据,输出只包括一行,这一行只包含一个整数,表示在规定的时间内,可以采到的草药的最大总价值。
//0-1背包问题
#include<stdio.h>
int buf[101][1001];//总时间不超过j的情况下前i个草药所能达到的最大价值
/*
若第一棵草药所需时间超过总时间则不采,若某一棵草药时间