WOW Factor
Recall that string aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly zero or all) characters. For example, for the string aa="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".
The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead.
Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w":
- "vvvv"
- "vvvv"
- "vvvv"
For example, the wow factor of the word "vvvovvv" equals to four because there are four wows:
- "vvvovvv"
- "vvvovvv"
- "vvvovvv"
- "vvvovvv"
Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.
For a given string ss, compute and output its wow factor. Note that it is not guaranteed that it is possible to get ss from another string replacing "w" with "vv". For example, ss can be equal to "vov".
The input contains a single non-empty string ss, consisting only of characters "v" and "o". The length of ss is at most 106106.
Output a single integer, the wow factor of ss.
vvvovvv
4
vvovooovovvovoovoovvvvovovvvov
100
The first example is explained in the legend.
刚开始忘了两个int相加还是int,只用了一个longlong的sum,导致溢出。
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <unordered_set> #include <unordered_map> #include <xfunctional> #define ll long long #define mod 998244353 using namespace std; int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} }; const int maxn = 1e5 + 5; const long long inf = 0x7f7f7f7f7f7f7f7f; int main() { string s; cin >> s; int v = 0, maxn = 0, initial = 0, firsto = 0; vector<ll> dp; for (int i = 0; s[i] == 'v'; i++) initial++; if (initial > 0) dp.push_back(initial - 1); else dp.push_back(0); for (int i = initial+1; i < s.size(); i++) { if (s[i] == 'v') maxn++; else { if (maxn > 1) dp.push_back(dp.back() + maxn - 1); else dp.push_back(dp.back()); maxn = 0; } if (i == s.size() - 1 && maxn>1) { dp.push_back(dp.back() + maxn - 1); } } ll sum = 0; for (int i = 0; i < dp.size(); i++) { sum += (dp.back() - dp[i])*dp[i]; } cout << sum; return 0; }