哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)补题(补一下dp相关的)简单的e题匹配字符串

版权声明:小媛原创,转载请注明出处! https://blog.csdn.net/xingfushiniziji/article/details/84677677

可以通过这些内容先了解一下dp
题目链接

//状态定义就是每个子问题
//状态转移就是状态与状态之间的转移式 
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int step[1003][1003];//存一下状态数 
int main()
{
	string str1,str2;
	int coun=0,len;
	cin>>str1>>str2;
	for(int i=1;i<=str1.length();i++)
	{
		for(int j=1;j<=str2.length();j++)
		{
			if(str1[i-1]==str2[j-1])//如果某一步的序列相等了 
			{
				step[i][j]=step[i-1][j-1]+1;//那么就加1 
			}
			else
			step[i][j]=max(step[i-1][j],step[i][j-1]);//不相等就看看之前哪个大 就是哪个了 
		}
	}
	cout<<step[str1.length()][str2.length()];//最终要求的是这一步的状态解 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xingfushiniziji/article/details/84677677