首页 > 给一串数字,变成对应字母序列,dp
头像
会哥哥
编辑于 2020-08-20 19:33
+ 关注

给一串数字,变成对应字母序列,dp

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int numOfDecoding(string s,int len)
{
	if(!len || s[0]=='0')
		return 0;
	vector<int> nums(len,0);
	nums[0]=1;
	if(s[1]=='0'&& s[0]<='2')
		nums[1]=1;
	else if(s[0]=='1' ||(s[0]=='2'&& s[1]<='6'))
		nums[1]=2;
	else if(s[1]=='0')
		nums[1]=0;
	else
		nums[1]=1;
	for(int i=2;i<len;i++)
	{
		if(s[i]=='0' && s[i-1]<='2' && s[i-1]>'0')
			nums[i]=nums[i-2];
		else if(s[i-1]=='1' ||(s[i-1]=='2' && s[i-1]<='6'))
			nums[i]=nums[i-1]+nums[i-2];
		else if(s[i]=='0')
			nums[i]=0;
		else
			nums[i]=nums[i-1];
	}
	return nums[len-1];
}
int main()
{
	string s;
	while(cin>>s)
	{
		int len=s.length();
		cout<<numOfDecoding(s,len);
	}
	return 0;
 } 

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期精华帖

热门推荐