#include <iomanip>
#include <iostream>
#include <string>
unsigned count_occurrences(const std::string& source, char terminal, const std::string& allowed)
{
unsigned count = 0;
std::size_t offset = 0;
while ((offset = source.find(terminal, offset)) != std::string::npos)
{
std::size_t next_off = source.find_first_not_of(allowed, offset+1);
if (next_off != std::string::npos && source[next_off] == terminal)
++count;
offset = next_off;
}
return count;
}
std::string quoted(const std::string& s)
{
return '"' + s + '"';
}
int main()
{
std::string test_cases [] =
{
"+---",
"---+ +---",
"+--- ---+",
"---+",
"+--+--+",
"+-+",
"+--- +",
"+ -+"
};
std::cout << std::left;
for (auto& test : test_cases)
std::cout << std::setw(15) << quoted(test) << count_occurrences(test, '+', "-") << '\n';
}
I2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KCnVuc2lnbmVkIGNvdW50X29jY3VycmVuY2VzKGNvbnN0IHN0ZDo6c3RyaW5nJiBzb3VyY2UsIGNoYXIgdGVybWluYWwsIGNvbnN0IHN0ZDo6c3RyaW5nJiBhbGxvd2VkKQp7CiAgICB1bnNpZ25lZCBjb3VudCA9IDA7CiAgICBzdGQ6OnNpemVfdCBvZmZzZXQgPSAwOwogICAgd2hpbGUgKChvZmZzZXQgPSBzb3VyY2UuZmluZCh0ZXJtaW5hbCwgb2Zmc2V0KSkgIT0gc3RkOjpzdHJpbmc6Om5wb3MpCiAgICB7CiAgICAgICAgc3RkOjpzaXplX3QgbmV4dF9vZmYgPSBzb3VyY2UuZmluZF9maXJzdF9ub3Rfb2YoYWxsb3dlZCwgb2Zmc2V0KzEpOwogICAgICAgIGlmIChuZXh0X29mZiAhPSBzdGQ6OnN0cmluZzo6bnBvcyAmJiBzb3VyY2VbbmV4dF9vZmZdID09IHRlcm1pbmFsKQogICAgICAgICAgICArK2NvdW50OwoKICAgICAgICBvZmZzZXQgPSBuZXh0X29mZjsgCiAgICB9CgogICAgcmV0dXJuIGNvdW50Owp9CgpzdGQ6OnN0cmlu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