알고리즘/문자열 기본구현
[B10751] - COW
tongnamuu
2018. 9. 23. 21:28
https://www.acmicpc.net/problem/10751
각 O를 기준으로 그 이전의 C의 개수와 기준이 되는 O보다 뒤에 나온 W의 개수를 곱하여 ans에 더해준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n; string s; cin >> n >> s; long long ans = 0; int cnt1 = 0, cnt2 = 0; vector<int>o; vector<int>w; for (int i = 0; i < n; i++) { if (s[i] == 'O') { o.push_back(i); } else if (s[i] == 'W'&&i>0) { w.push_back(i); } } int olen = o.size(); int pre = 0; for (int k = 0; k < olen; k++) { int temp = o[k]; cnt2 = 0; for (int i = pre; i <temp; i++) { if (s[i] == 'C') { cnt1++; } } for (auto &j : w) { if (j > temp) cnt2++; } pre = temp; ans += cnt1 * cnt2; } cout << ans << '\n'; } | cs |