As Simple as One and Two (Codeforces 1276A)
Tags: Greedy, dp

You are given a non-empty string $$$s=s_1s_2\dots s_n$$$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $$$s$$$ if there is an integer $$$j$$$ ($$$1 \le j \le n-2$$$), that $$$s_{j}s_{j+1}s_{j+2}=$$$"one" or $$$s_{j}s_{j+1}s_{j+2}=$$$"two".For example:  Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"),  Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two"). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time.For example, if the string looks like $$$s=$$$"onetwone", then if Polycarp selects two indices $$$3$$$ and $$$6$$$, then "onetwone" will be selected and the result is "ontwne".What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be?

Input: The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Next, the test cases are given.Each test case consists of one non-empty string $$$s$$$. Its length does not exceed $$$1.5\cdot10^5$$$. The string $$$s$$$ consists only of lowercase Latin letters.It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $$$1.5\cdot10^6$$$.

Output: Print an answer for each test case in the input in order of their appearance.The first line of each answer should contain $$$r$$$ ($$$0 \le r \le |s|$$$) — the required minimum number of positions to be removed, where $$$|s|$$$ is the length of the given line. The second line of each answer should contain $$$r$$$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $$$1$$$ to the length of the string. If $$$r=0$$$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them.

Note: In the first example, answers are:  "onetwone",  "testme" — Polycarp likes it, there is nothing to remove,  "oneoneone",  "twotwo". In the second example, answers are:   "onetwonetwooneooonetwooo",  "two",  "one",  "twooooo",  "ttttwo",  "ttwwoo" — Polycarp likes it, there is nothing to remove,  "ooone",  "onnne" — Polycarp likes it, there is nothing to remove,  "oneeeee",  "oneeeeeeetwooooo". 

Sample Inputs:
4
onetwone
testme
oneoneone
twotwo

10
onetwonetwooneooonetwooo
two
one
twooooo
ttttwo
ttwwoo
ooone
onnne
oneeeee
oneeeeeeetwooooo

Sample Outputs:
2
6 3
0

3
4 1 7 
2
1 4

6
18 11 12 1 6 21 
1
1 
1
3 
1
2 
1
6 
0

1
4 
0

1
1 
2
1 11

Solution (Python):
from os import path
import sys, time
mod = int(1000000000.0 + 7)
from math import ceil, floor, gcd, log, log2, factorial, sqrt
from collections import defaultdict, Counter, OrderedDict, deque
from itertools import combinations, permutations
from string import ascii_lowercase, ascii_uppercase
from bisect import *
from functools import reduce
from operator import mul
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().strip('\n')
grid = lambda r: [lint() for i in range(r)]
localsys = 0
start_time = time.time()
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)

def ceill(n, x):
    return (n + x - 1) // x
T = 1

def solve():
    s = S() + '-1' * 2
    (ans, n, i) = ([], len(s), 0)
    while i < n:
        x = s[i:i + 3]
        if x == 'one':
            ans.append(i + 2)
            i += 1
        elif x == 'two':
            if s[i + 3] == 'n' and s[i + 4] == 'e':
                ans.append(i + 3)
                i += 3
            else:
                ans.append(i + 2)
                i += 1
        i += 1
    print(len(ans), ' '.join(map(str, ans)), sep='\n')

def run():
    if path.exists('input.txt'):
        sys.stdin = open('input.txt', 'r')
        sys.stdout = open('output.txt', 'w')
run()
T = I() if T else 1
for _ in range(T):
    solve()
if localsys:
    print('\n\nTime Elased :', time.time() - start_time, 'seconds')

Solution (Python):
def isTwo(s, i):
    return s[i] == 't' and s[i + 1] == 'w' and (s[i + 2] == 'o')

def isOne(s, i):
    return s[i] == 'o' and s[i + 1] == 'n' and (s[i + 2] == 'e')

def solve(s):
    positions = []
    i = 0
    while i < len(s) - 2:
        if isTwo(s, i):
            if i + 2 <= len(s) - 3 and isOne(s, i + 2):
                positions.append(i + 3)
                i += 4
            else:
                positions.append(i + 2)
                i += 2
        elif isOne(s, i):
            positions.append(i + 2)
            i += 2
        i += 1
    print(len(positions))
    for pos in positions:
        print(pos, end=' ')
    print()
n = int(input())
for i in range(n):
    solve(input())

Solution (Python):
def isTwo(s, i):
    return s[i] == 't' and s[i + 1] == 'w' and (s[i + 2] == 'o')

def isOne(s, i):
    return s[i] == 'o' and s[i + 1] == 'n' and (s[i + 2] == 'e')

def solve(s):
    positions = []
    i = 0
    while i < len(s) - 2:
        if isTwo(s, i):
            if i + 2 <= len(s) - 3 and isOne(s, i + 2):
                positions.append(i + 3)
                i += 4
            else:
                positions.append(i + 2)
                i += 2
        elif isOne(s, i):
            positions.append(i + 2)
            i += 2
        i += 1
    print(len(positions))
    for pos in positions:
        print(pos, end=' ')
    print()
n = int(input())
for i in range(n):
    solve(input())