Files
problemas-para-competicao-p…/decode-ways/src/alternative_ac.cpp

49 lines
829 B
C++

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;
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;
next %= MOD;
}
int code = stoi(aux);
if (10 <= code && code <= 26)
{
next += dp1;
next %= MOD;
}
swap(dp1, dp2);
swap(dp2, next);
}
return dp2;
}
int main()
{
int n;
cin >> n;
string code;
cin >> code;
cout << numDecodings(code) << endl;
return 0;
}