45 lines
753 B
C++
45 lines
753 B
C++
#include <bits/stdc++.h>
|
|
|
|
typedef long long ll;
|
|
using namespace std;
|
|
|
|
ll numDecodings(string s)
|
|
{
|
|
if (s[0] == '0')
|
|
return 0;
|
|
|
|
int N = s.size();
|
|
ll dp1 = 1, dp2 = 1;
|
|
for (int i = 2; i <= N; i++)
|
|
{
|
|
ll next = 0;
|
|
char current = s[i - 1], last = s[i - 2];
|
|
string aux = "";
|
|
aux += last;
|
|
aux += current;
|
|
if (current != '0')
|
|
{
|
|
next = dp2;
|
|
}
|
|
|
|
int code = stoi(aux);
|
|
if (10 <= code && code <= 26)
|
|
{
|
|
next += dp1;
|
|
}
|
|
swap(dp1, dp2);
|
|
swap(dp2, next);
|
|
}
|
|
|
|
return dp2;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int n;
|
|
cin >> n;
|
|
string code;
|
|
cin >> code;
|
|
cout << numDecodings(code) << endl;
|
|
return 0;
|
|
} |