엇 간단한 문제였다
같은 문자가 연속으로 있을 때 처음에는 26곱해주고 다음 문자부터는 25 곱해주면 된다.
문제 풀다가 mod로 나누는 거에 궁금한 게 생겼다.
ans = a * b * c; 라고 했을 때 a%mod * b%mod 이런 식으로 각각 해야하는지 아니면 (a * b) % mod 로 해야 하는지?
이건 상관없고 overflow가 날 것 같으면 각각 mod 취해주고 날 것 같지 않으면 다 계산 후 결과로 mod 취해주면 된다.
그런데 + 같은 경우는 int로 해도 터지지 않고 곱셈 같은 경우는 i64 쓰면 된다!
문제 해결 ~.~
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define xx first
#define yy second
#define mod 1000000009
using namespace std;
using i64 = long long;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
int main() {
string s;
cin >> s;
i64 ans = 1;
bool is_d = false, is_c = false;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == 'c' && !is_c)
{
is_c = true;
is_d = false;
ans = (ans * 26) % mod;
}
else if (s[i] == 'c' && is_c)
{
ans = (ans * 25) % mod;
}
if (s[i] == 'd' && !is_d)
{
is_c = false;
is_d = true;
ans = (ans * 10) % mod;
}
else if (s[i] == 'd' && is_d)
{
ans = (ans * 9) % mod;
}
}
cout << ans;
return 0;
}
'UCPC' 카테고리의 다른 글
[20/05/24] I 모노디지털 표현 (2287) (0) | 2020.05.30 |
---|---|
[20/05/24] E 제곱근 작도 (5389) (0) | 2020.05.30 |
[20/05/24] B 겉넓이 구하기 (16931) (0) | 2020.05.30 |
[20/05/24] J 피보나치 인버스 (10425) (0) | 2020.05.30 |
[20/05/24] I 팬케이크 쌓기(12744) (0) | 2020.05.30 |