Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Created August 6, 2014 11:46
Show Gist options
  • Save mizuhara/edd5e0060ad260b0fcac to your computer and use it in GitHub Desktop.
Save mizuhara/edd5e0060ad260b0fcac to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int size = 1100;
vector<int> sqrt_nums(const vector<bool> &v, int to)
{
vector<int> ret;
for(int i = 1; i * i < to; ++i) {
if(v[i * i]) ret.push_back(i * i);
}
return ret;
}
vector<int> cubic_nums(const vector<bool> &v, int to)
{
vector<int> ret;
for(int i = 1; i * i * i < to; ++i) {
if(v[i * i * i]) ret.push_back(i * i * i);
}
return ret;
}
void drop_next(vector<bool> &v, bool is_sqrt)
{
const vector<int> nums = is_sqrt ? sqrt_nums(v, size) : cubic_nums(v, size);
for(const auto &num : nums) {
for(int i = num + 1;; ++i) {
if(v[i]) {
v[i] = false;
break;
}
}
}
}
void drop_prev(vector<bool> &v, bool is_sqrt)
{
const vector<int> nums = is_sqrt ? sqrt_nums(v, size) : cubic_nums(v, size);
for(const auto &num : nums) {
for(int i = num - 1; 1 <= i; --i) {
if(v[i]) {
v[i] = false;
break;
}
}
}
}
void drop_multiple(vector<bool> &v, int base)
{
for(size_t i = 1, count = 0; i < v.size(); ++i) {
if(v[i]) ++count;
if(count % base == 0) v[i] = false;
}
}
void drop(vector<bool> &v, int n)
{
for(size_t i = 1, count = 0; i < v.size(); ++i) {
if(v[i]) {
v[i] = false;
++count;
}
if(count == n) break;
}
}
vector<int> take(const vector<bool> &v, size_t size)
{
vector<int> ret;
for(size_t i = 1, count = 0; count != size; ++i) {
if(v[i]) {
ret.push_back(i);
++count;
}
}
return ret;
}
string join(const vector<int> &v, const string &sep)
{
string ret = "";
for(const auto &e : v) {
ret += to_string(e) + sep;
}
return ret.substr(0, ret.length() - 1);
}
string solve(const string &src)
{
vector<bool> v(size, true);
v[0] = false; // 0は使用しないためfalseにする
for(const auto &e : src) {
switch(e) {
case 'S':
drop_next(v, true);
break;
case 's':
drop_prev(v, true);
break;
case 'C':
drop_next(v, false);
break;
case 'c':
drop_prev(v, false);
break;
case 'h':
drop(v, 100);
break;
default:
drop_multiple(v, e - '0');
break;
}
}
return join(take(v, 10), ",");
}
void test(const string &src, const string &expected)
{
const string actual = solve(src);
if(actual == expected) {
cout << "ok" << endl;
} else {
cout << "actual-> " << actual << " expected -> " << expected << endl;
}
}
int main()
{
/*0*/ test( "ss6cc24S", "1,9,21,30,33,37,42,44,49,56" );
/*1*/ test( "h", "101,102,103,104,105,106,107,108,109,110" );
// 略
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment