We are given a problem: "Short Phrase". We have to find a subsequence? Wait careful: The condition is: The sequence of words can be divided into five sections such that the total number of letters in the words of the first section is 5, second 7, third 5, fourth 7, fifth 7. So it's like a Tanka: 5-7-5-7-7 syllables, but here we count letters.
We have a sequence of words w1...wn. Some irrelevant texts may be added at beginnings and ends. So the actual Short Phrase is a contiguous subsequence? Let's read: "Your mission is to write a program that finds the Short Phrase from a sequence of words that may have an irrelevant prefix and/or a suffix." So the whole sequence is the contributed Short Phrase with some extra words prepended (prefix) and/or appended (suffix). So the Short Phrase is a contiguous subsequence (a contiguous block) of the words that satisfies the condition. We need to output the index i of the first word of that Short Phrase. If multiple Short Phrases occur, output the first one (i.e., the one with smallest i). Each dataset includes at least one Short Phrase.
Constraints: n up to 40, each word length 1 to 10.
Thus we can brute force: try every possible starting index i and ending index j (inclusive) and check if the sum of letters in that contiguous block can be partitioned into sections of lengths [5,7,5,7,7] exactly, where each section is a contiguous sub-block of words? The condition: "The sequence of words can be divided into five sections such that the total number of the letters in the word(s) of the first section is five, that of the second is seven, and those of the rest are five, seven, and seven, respectively." Typically, sections are contiguous partitions of the sequence. So we have to partition the contiguous subsequence into five contiguous parts (non-empty? Possibly empty? In poetry, each section could be made of one or more words; but we need total letter counts exactly 5,7,5,7,7. The sections are contiguous; we need to find breakpoints that sum to those totals. So for a given candidate block of words from i to j, we have a list of word lengths. We need to see if we can cut it into 5 contiguous segments whose total lengths are exactly 5,7,5,7,7 in that order. This is a simple check: we can compute prefix sums and then find positions where cumulative sum equals 5, 5+7=12, 12+5=17, 17+7=24, and total sum should be 31 (since 5+7+5+7+7 = 31). So total letter sum must be 31. If total sum is 31, we then need to verify that we can exactly achieve those breakpoints: that is, there must exist indices p1,p2,p3,p4 such that sum of lengths from i to p1 =5, from p1+1 to p2 =7, from p2+1 to p3 =5, from p3+1 to p4 =7, from p4+1 to j =7. Since words lengths are integers, it's equivalent to checking that the cumulative sum crosses these thresholds exactly at word boundaries? Actually we can have that the 5 sum may span multiple words. The breakpoints are between words, i.e., after some number of words such that the sum of those words' lengths equals exactly the required total. So we need to check if we can split the sequence into 5 groups (non-empty? Possibly a group could be empty if the sum is 0? But the required sum is non-zero, so each group must contain at least one word because the sum of letters >0. But it's possible that a group could consist of a single word if that word's length equals the required sum. But we don't need to enforce non-emptiness explicitly; if the sum requirement is met, it automatically implies there is at least one word if we require that each word's length >=1, but it's possible that the sum requirement could be met by zero words? No, sum=0 not allowed. So groups will be non-empty because the sums are positive. However, we must check that the breakpoints are at word boundaries. The condition does not state that each section must be non-empty? But it's natural.
Thus algorithm: For each dataset, compute word lengths array len[1..n]. Then for each start index i from 1 to n, for each end index j from i to n, compute total sum = sum_{k=i}^j len[k]. If total sum != 31, skip. If total sum == 31, we need to check if it's possible to partition into exactly the required sums. This is straightforward: we can traverse from i, accumulating sum, and see if we can exactly hit the milestones: first target T1=5, after reaching exactly 5, next target T2=12, T3=17, T4=24, T5=31. If at the end we have exactly matched all milestones, then it's valid. If at any point the accumulated sum exceeds the target before finishing a word, then it's invalid. Actually we just need to check that each milestone is exactly equal to some prefix sum. So we can compute cumulative sums of the block and see if the set of milestones (5,12,17,24,31) are all present among the cumulative sums (starting from sum=0 at position before first word). Since the cumulative sums are monotonic increasing, if total sum=31, then we can check if 5,12,17,24 are present exactly. If all present, it's a valid Short Phrase.
But careful: If there are multiple ways to partition, but existence of at least one is enough. But if total sum=31, does existence of those prefix sums guarantee a valid partition? Not exactly: because the milestones must be achieved exactly after some number of words. It's possible that total sum=31, but the prefix sum might skip over a milestone (e.g., a word length 6 would make cumulative sum jump from 4 to 10, skipping 5). In that case, there is no way to split to have exactly 5 as first section sum because you can't break mid-word. So we need to check that the milestones are exactly equal to some cumulative sum. Since we start at 0 and add lengths, if at some point cumulative sum equals a milestone exactly, then we can cut there. So check that 5,12,17,24 are present among cumulative sums (including the final sum 31 which is guaranteed if total=31). So the condition: total sum = 31 AND the set {5,12,17,24} are among the prefix sums of the block. That's necessary and sufficient.
But also need to consider that the sections should be five sections; that implies there are exactly 5 groups. If total sum=31 and the milestones are present, then we have exactly 5 groups (since we cut at those points). However, is it possible that a milestone appears more than once? Since sums are increasing and lengths positive, it can't appear more than once (except maybe if a word length is zero, but length is at least 1). So fine.
Thus we can iterate over all i and j where sum from i to j = 31 and milestones present. Among all such (i,j) blocks, we need the one with smallest i (the first word of the Short Phrase). If multiple have same i? But we need the first Short Phrase, i.e., smallest i. If there are multiple with same i (different j)? Possibly, but we output i. If two different blocks both start at same i, they are both Short Phrases? But the condition: "When multiple Short Phrases occur in the dataset, you should output the first one." So we need the one with smallest i; if same i, then presumably the first occurrence in order? Since they start at same i but maybe different j, which one is "first"? The phrase is defined by its start. If they both start at the same i, they are both occurrences starting at same word. But are they distinct? The phrase is a contiguous block; two different blocks with same start but different end are both valid Short Phrases. But which one is "first"? Typically, scanning from left, the first occurrence would be the one with smallest start index; if start indices equal, perhaps the one that appears earlier? But since start index same, they occur at the same time? However, the input is a sequence, so if we have two blocks both starting at i, they are different lengths. Which one is considered first? Usually, the problem expects a unique answer because there is exactly one Short Phrase? But the statement says "When multiple Short Phrases occur, you should output the first one." That implies there could be multiple overlapping. We need to define "first" in terms of the starting index? Probably the one with smallest start index, and if multiple have same smallest start index, then perhaps the one with smallest end index? Or the first one encountered if we scan in natural order? Usually, "first" means the one that appears earliest, i.e., its starting index is smallest; if tie, then perhaps the one that ends earlier? But let's see sample: The second dataset: n=14 with words: oh, yes, by, far, it, is, wow, so, bad, to, me, you, know, hey. Output is 2. Let's test to see possible Short Phrases. We'll compute lengths: oh->2, yes->3, by->2, far->3, it->2, is->2, wow->3, so->2, bad->3, to->2, me->2, you->3, know->4, hey->3. Sum? Possibly the Short Phrase starts at index 2? Let's compute totals from i=2 (yes). We need to find a contiguous block sum=31 and milestones present. Let's try manually: Starting at yes (3). If we try to build. The pattern: 5-7-5-7-7. It may be something like: 3+2=5 (yes+by?) That yields first section 5: yes (3) and by (2). Then need 7: far (3) + it (2) + is (2) =7 (3+2+2=7). Then need 5: wow (3) + so (2) =5 (3+2=5). Then need 7: bad (3) + to (2) + me (2) =7 (3+2+2=7). Then need 7: you (3) + know (4) =7? 3+4=7. So that works: words from index 2 to index 12? Let's list indices:
1: oh (2)
2: yes (3)
3: by (2)
4: far (3)
5: it (2)
6: is (2)
7: wow (3)
8: so (2)
9: bad (3)
10: to (2)
11: me (2)
12: you (3)
13: know (4)
14: hey (3)
If we start at i=2 (yes) and go to j=12 (you), the words are: yes(3), by(2), far(3), it(2), is(2), wow(3), so(2), bad(3), to(2), me(2), you(3). That's 11 words. Sum = 3+2+3+2+2+3+2+3+2+2+3 = let's compute: (3+2)=5, +3=8, +2=10, +2=12, +3=15, +2=17, +3=20, +2=22, +2=24, +3=27? Actually let's sum carefully: 3+2=5, +3=8, +2=10, +2=12, +3=15, +2=17, +3=20, +2=22, +2=24, +3=27. Total = 27? But we need 31. Wait, I missed some? Count words: from index2 to index12 inclusive, that's indices:2,3,4,5,6,7,8,9,10,11,12 = 11 words. The lengths: 3,2,3,2,2,3,2,3,2,2,3. Sum: 3+2=5; +3=8; +2=10; +2=12; +3=15; +2=17; +3=20; +2=22; +2=24; +3=27. So sum=27. Not 31.
We need 31. Possibly include index13 (know) as well? Then from 2 to 13: add know(4) -> total 31. So words: yes(3),by(2),far(3),it(2),is(2),wow(3),so(2),bad(3),to(2),me(2),you(3),know(4) = 12 words. Sum: previous 27 +4 =31. So block i=2, j=13. Let's check milestones: cumulative sums: after yes:3; after by:5 (hit 5) -> first section = words 2-3 (yes+by) sum=5. Next target 12. Continue: after far: 3+2+3=8? Actually cumulative: after far (index4) sum=3+2+3=8; after it: +2=10; after is: +2=12 -> hit 12 after is (index6). So second section = words 4-6 (far,it,is) sum=7 (3+2+2=7). Next target 17. After wow: +3=15; after so: +2=17 -> hit 17 after so (index8). Third section = words 7-8 (wow,so) sum=5. Next target 24. After bad: +3=20; after to: +2=22; after me: +2=24 -> hit 24 after me (index11). Fourth section = words 9-11 (bad,to,me) sum=7. Remaining words: you (3) + know (4) =7 -> sum 7, last section. So indeed milestones at 5,12,17,24 present. So that's a valid Short Phrase starting at index2. Also there might be a valid one starting at index1? Let's test i=1: oh(2). Need sum=31. If start at 1, maybe? The output says 2, so either there is no valid phrase starting at 1 or there is but the first one is at 2? We need the first occurrence. Perhaps there is also a valid phrase starting at 1, but the phrase is from 1 to something. Let's test i=1: words lengths: oh(2), yes(3), by(2), far(3), it(2), is(2), wow(3), so(2), bad(3), to(2), me(2), you(3), know(4), hey(3). We need to check any contiguous block from i=1 that sum to 31 and milestones present. Possibly from 1 to 13? That would be 2+3+2+3+2+2+3+2+3+2+2+3+4 = let's compute: 2+3=5; +2=7; +3=10; +2=12; +2=14; +3=17; +2=19; +3=22; +2=24; +2=26; +3=29; +4=33. So sum=33 >31. From 1 to 12: up to you: 2+3+2+3+2+2+3+2+3+2+2+3 = 2+3=5; +2=7; +3=10; +2=12; +2=14; +3=17; +2=19; +3=22; +2=24; +2=26; +3=29. So sum=29. Not 31. 1 to 14 sum = 33. So none with sum 31. What about 1 to something else? Perhaps from 1 to something skipping? Only contiguous. So no block starting at 1 sums to 31. Also maybe 1 to 14 sum 33. Could there be block starting at 1 with length that sums 31 but not necessarily include all words? We need contiguous block; we could have start=1, end=?? We can try other combos: But the only contiguous blocks that sum to 31 might be those we found. Also there might be a block starting at 6? But output is 2, so the first valid start is 2. Good.
Now third dataset: n=15 with repeated lengths: abcde (5), fghijkl (7), mnopq (5), rstuvwx (7), yzz (3). Then again abcde (5), fghijkl (7), mnopq (5), rstuvwx (7), yz (2). Then again abcde (5), fghijkl (7), mnopq (5), rstuvwx (7), yz (2). Wait actual input lines:
15
abcde
fghijkl
mnopq
rstuvwx
yzz
abcde
fghijkl
mnopq
rstuvwx
yz
abcde
fghijkl
mnopq
rstuvwx
yz
So lengths:
1:5
2:7
3:5
4:7
5:3 (yzz length 3)
6:5
7:7
8:5
9:7
10:2 (yz)
11:5
12:7
13:5
14:7
15:2
The output is 6. So the Short Phrase starts at index 6. Let's check: Starting at 6, we need sum=31. Words 6-? Let's compute sums:
6:5
7:7 -> cumulative 12
8:5 -> 17
9:7 -> 24
10:2 -> 26 (not 31)
So need more words. Continue:
11:5 -> 31 (if we go 6 to 11: words 6-11 inclusive: lengths 5+7+5+7+2+5 = 31? Actually 5+7=12, +5=17, +7=24, +2=26, +5=31. So total 31. Check milestones: cumulative sums: after 6:5 (hit 5), after 7:12 (hit 12), after 8:17 (hit 17), after 9:24 (hit 24), after 11:31. So that works. The block is words 6 through 11: abcde(5) word6, fghijkl(7) word7, mnopq(5) word8, rstuvwx(7) word9, yz(2) word10, abcde(5) word11. But careful: word10 is yz (length 2). Then word11 is abcde (5). So the partitions: first section: word6 (5) exactly. second: word7 (7) exactly. third: word8 (5) exactly. fourth: word9 (7) exactly. fifth: word10 (2) + word11 (5) =7. Yes. So that's valid. But why is start index 6 the first? Could there be a valid phrase starting earlier? Let's test start index 1: sum of words 1-5: 5+7+5+7+3=27. Not 31. 1-6: +5 =>32. 1-7: +7=>39. 1-? Try 1-? sum 31? maybe 1-? 1-? Not exactly. 1-4 sum 24; need 7 more, next word 5 is 3, total 27; word6 is 5 ->32 overshoot. So no. Start index 2: 2-? 7+5+7+3=22? Actually 2:7,3:5,4:7,5:3 sum=22. Need 9 more, but next word6 is 5 =>27, word7 7=>34. Not 31. 2-? 2-? maybe 2-6: 7+5+7+3+5=27; 2-7: +7=34; 2-8: +5=39; none. Start index 3: 3:5,4:7,5:3 sum=15, need 16; next 6:5 =>20, 7:7 =>27, 8:5=>32, 9:7=>39... Not 31. Start index 4: 4:7,5:3=10, need 21; 6:5 =>15, 7:7=>22, 8:5=>27, 9:7=>34, 10:2=>36, 11:5=>41... not 31. Start index 5: 5:3,6:5=8,7:7=15,8:5=20,9:7=27,10:2=29,11:5=34... not 31. So indeed first valid start is 6.
So algorithm seems straightforward: iterate start i from 1 to n; compute prefix sums from i to n, keep a running total; when total reaches >31 break? But n <=40, so O(n^2) is fine. But we need to check milestones. For each (i, j) with total=31, check if milestones exist.
Alternative: For each start i, we can try to partition greedily to see if we can achieve the sections. Since the order is fixed, we can attempt to take words from i onward, accumulate sum until we reach each target exactly. If at any point we exceed a target, then this start i is not valid. If we complete all five sections exactly at word boundaries and we consumed some words (maybe not all words up to n), then we have a valid phrase from i to that ending word. But we must ensure that the phrase is contiguous and we don't skip words. So we can simply for each i, set pos = i, need = [5,7,5,7,7], for each need, accumulate sum from pos, if at some point sum exceeds need, break and try next i; if sum equals need exactly after some word, then set pos to next word and continue. If we finish all five needs and pos <= n+1 (since after last word we have consumed some words), then we have found a valid phrase starting at i. This method will find the first (lowest i) valid phrase that starts at i. Since we check i in increasing order, we output the first i for which the greedy works. But careful: Is greedy always correct? Since we require that the phrase is exactly a contiguous block of words that can be partitioned into sections with exact sums. Greedy approach: for a given start i, we try to take the smallest prefix that sums to the first target (5). There could be multiple ways to achieve the sum, e.g., could take more words as long as the sum equals 5? But since words are positive, the cumulative sums are strictly increasing. So there is at most one index where the cumulative sum equals exactly 5 (if it ever hits). If it never hits exactly, then impossible. So greedy is deterministic: we move forward until cumulative sum >= target; if > target, fail; if == target, we cut and move to next target. This yields a unique partition if any exists. This is correct because we are forced to take exactly the words that bring cumulative sum to target. So we don't need to consider other partitions; if it's possible, the cumulative sums must hit each target exactly at some word boundaries. So the greedy approach will find it.
Thus simpler: For each start i from 1 to n:
idx = i
for each target in [5,7,5,7,7]:
sum = 0
while idx <= n and sum < target:
sum += len[idx]
idx += 1
if sum != target: break (fail)
if all targets satisfied:
output i and break.
But we must also ensure that we don't have extra words after the phrase? The phrase is a contiguous block that satisfies the condition; the irrelevant text can be at beginning and end, so the phrase itself is a sub-block. In our greedy, we consume exactly the words that sum to targets. After finishing all five targets, we have consumed some number of words; that's the phrase. But there might be more words after the phrase (suffix) and before (prefix). Since we start at i and consume words until we meet the targets, we don't care if there are extra words after; we just need to find some contiguous block that works. However, the phrase is exactly the words from i to idx-1 (where idx is after consuming). That's a valid phrase. But does the problem require that the phrase is exactly the block, and there may be extra words before and after? Yes. So greedy finds a block starting at i that satisfies the condition, regardless of what comes after. But we must also consider that maybe for a given start i, there could be multiple blocks that satisfy the condition? For example, it might be possible to partition the words from i to some j in one way, and also from i to some larger j in another way? But because the cumulative sums are increasing, if we have two different endings, that would imply that the total sum is 31 for both. But if there are two blocks starting at i both summing to 31, they would have different lengths, so different sets of words. But can that happen? Suppose the words after the block include negative lengths? No, lengths positive. The cumulative sum from i to j is strictly increasing with j. So there can be at most one j such that sum(i,j)=31. So for a given start i, there is at most one j that gives total sum 31. So greedy will either succeed or fail; if it fails (sum not 31 or milestones not hit), then no block starting at i works. So unique.
But wait: Is it possible that the block starting at i sums to 31 but the milestones are not met exactly because the cumulative sums skip some target? Then it's invalid. So the greedy will fail because when trying to achieve first target 5, it will either overshoot or reach exactly 5 but later overshoot. However, what if it reaches exactly 5 at some point, then for second target 7, it might overshoot? Then the greedy would fail. But maybe there is an alternative partition that still satisfies the condition? For example, could we assign words to sections in a non-greedy way? The sections must be contiguous and in order, but the partition points are after some words. The first section must be a contiguous prefix that sums to 5. The only way to sum to 5 is to take the first k words such that their total sum equals 5. There is exactly one such k if it exists (since cumulative sums increase and can only hit 5 once). If there is no k where prefix sum equals 5, then it's impossible to have a partition because the first section must sum to 5 and be a prefix. So indeed the partition is forced if it exists. So greedy is fine.
Thus algorithm is straightforward.
Edge cases: n up to 40, so simple loops fine.
Now consider sample 1: 9 words. We test i=1: start with "do"(2), need 5: sum=2<5, next "the"(3) sum=5 exactly, idx becomes 3. Need 7: start at idx=3 "best"(4) sum=4<7, next "and"(3) sum=7 exactly, idx=5. Need 5: "enjoy"(5) sum=5 exactly, idx=6. Need 7: "today"(5) sum=5<7, next "at"(2) sum=7 exactly, idx=8. Need 7: "acm"(3) sum=3<7, next "icpc"(4) sum=7 exactly, idx=10 which is beyond n (9+1). All targets met, so output i=1. Works.
Second dataset: i=1: start oh(2), need 5: sum=2<5, next yes(3) sum=5 exactly idx=3. Need 7: next by(2) sum=2<7, next far(3) sum=5<7, next it(2) sum=7 exactly idx=6. Need 5: next is(2) sum=2<5, next wow(3) sum=5 exactly idx=8. Need 7: next so(2) sum=2<7, next bad(3) sum=5<7, next to(2) sum=7 exactly idx=11. Need 7: next me(2) sum=2<7, next you(3) sum=5<7, next know(4) sum=9 >7 => fail (exceeds). So i=1 fails. Then i=2: we already tested works. So output 2.
Third dataset: i=1 fails; ... i=6 works.
Thus solution is correct.
Now we need to implement reading input until n=0.
We also need to consider that there is always at least one Short Phrase. So algorithm will always find some i.
Complexities: O(n^2) worst-case, but with n <= 40 it's trivial.
Now potential pitfalls: words lengths up to 10, total sum 31, so for each start, we might run out of words before completing all sections. In that case break and fail.
Implementation steps:
For each dataset:
- Read integer n.
- If n==0, break.
- Read n lines each containing a word (strip whitespace).
- Compute array lens of length n.
- For i from 0 to n-1 (0-indexed):
pos = i
success = True
for each target in targets = [5,7,5,7,7]:
if pos >= n: success = False; break
s = 0
while pos < n and s < target:
s += lens[pos]
pos += 1
if s != target:
success = False
break
if success:
print(i+1) (since output uses 1-indexed)
break
That's all.
But wait: Need to consider that after finishing all targets, there might be no requirement that the phrase ends exactly at the last consumed word? Actually that's the phrase. And the irrelevant suffix can be after that. So it's okay if pos <= n (i.e., there are leftover words). However, we must ensure that the phrase is exactly the words from i to pos-1. There's no requirement that we consume all words. So success condition is just that we were able to achieve all targets without overshoot or running out of words. So after the loop, if success, we output i+1 and break.
But is there any case where the greedy would succeed but the block sum is not exactly 31? Actually if we succeed, the sum of the consumed words is 5+7+5+7+7=31, so total sum is 31 automatically.
Thus solution works.
But we might also consider that the phrase could be not exactly at the start if the irrelevant prefix is empty, but our algorithm will find the earliest i.
Now about multiple occurrences: If there are multiple valid i, we output the smallest. Our loop goes from 0 to n-1, so we output the first found, which is smallest.
Edge Cases: Could there be a case where for a given i, the greedy fails but there exists a different partition? As argued, no, because the partition is forced by cumulative sums. However, consider if a section sum can be achieved by a different grouping that is not the shortest prefix? For example, suppose first few words lengths: [2,2,1] and need target 5. The cumulative sums: after first word:2, after second:4, after third:5. So the only way to get sum 5 is to take all three words. There's no alternative because the sections are contiguous; you can't skip the second word to combine first and third to get 5 (since that would be non-contiguous). So indeed the first section must be a prefix. So the prefix that sums to exactly target must be the shortest prefix that meets or exceeds the target? Actually if the cumulative sum jumps over the target, e.g., lengths: [3,3] target 5. Cumulative after first:3 (<5), after second:6 (>5). There is no exact sum 5, so impossible. So no alternative.
Thus greedy is correct.
Potential nuance: The problem statement: "the sequence of words can be divided into five sections such that the total number of the letters in the word(s) of the first section is five, that of the second is seven, and those of the rest are five, seven, and seven, respectively." It does not explicitly say sections are contiguous? In typical Tanka, the sections are just groups of words that may not be contiguous? But they said "divided into five sections", which normally means you partition the sequence into consecutive segments. Also the example: they divided the nine words into five sections with contiguous groups. So it's safe.
Thus solution is simple.
Now we need to implement reading input. Input format: first line n, then n lines each with a word, then possibly next dataset until 0. There is no blank lines. So we read line by line.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().split()
# But careful: words may be lines with spaces? No, each word is on its own line and contains only lowercase letters, no spaces. So splitting works.
# However, we need to parse integer and words sequentially.
# Use iterator.
it = iter(data)
out_lines = []
while True:
try:
n = int(next(it))
except StopIteration:
break
if n == 0:
break
lens = []
for _ in range(n):
w = next(it)
lens.append(len(w))
# find first start
found = False
for i in range(n):
pos = i
ok = True
for t in (5,7,5,7,7):
if pos >= n:
ok = False
break
s = 0
while pos < n and s < t:
s += lens[pos]
pos += 1
if s != t:
ok = False
break
if ok:
out_lines.append(str(i+1))
found = True
break
# As per problem guarantee, found will be True.
print("\n".join(out_lines))
That's it.
But we should check if there is any scenario where the greedy might incorrectly succeed because it consumes words beyond the phrase but still meets targets? Actually if after meeting all targets we have consumed exactly the phrase, it's fine. However, what if there is a scenario where we could meet the targets but using a different grouping (e.g., first target could be satisfied by taking a longer prefix that also sums to 5? That's impossible because lengths are positive, so cumulative sums are strictly increasing. So only one prefix can sum to exactly 5 (if any). So no ambiguity.
One more nuance: Could there be a case where we need to skip some words at the beginning because the prefix is irrelevant? But we are iterating start indices, so we consider all possible starts.
Thus solution is complete.
Let's double-check sample outputs: sample input has three datasets, outputs 1,2,6. Works.
Now we should consider potential off-by-one: In the greedy loop, after we finish the last target, pos points to the next word after the phrase. That's fine.
Time complexity: O(n^2) at worst (n up to 40). Good.
Thus answer is simple.
We'll produce final solution in Python.
Now think about any tricky cases:
Case: n=5 and the words lengths exactly [5,7,5,7,7] -> then start i=1 works. Good.
Case: n=5 but with extra words before or after? Works.
Case: n=40, words random, but guarantee at least one phrase.
Potential bug: If for a start i, while accumulating for a target, we may overshoot and break. That's correct.
But what if the sum exactly equals the target after some words, but we have more words before the next section? Actually that's fine.
Now consider a scenario: start i, we have words: [1,4] target 5: after first word sum=1<5, second word sum=5 exactly; pos moves to next word. Good.
Now scenario: target 5, words: [2,2,1] works as described.
All fine.
Thus finalize.
However, we should consider if there might be multiple valid phrases that start at the same i? It can't because sum(i,j) is monotonic; only one j yields total 31. But could there be two different partitions with the same total 31? Possibly if the cumulative sums hit the milestones at multiple points? For a given block with total 31, the milestones are fixed: 5,12,17,24,31. If the cumulative sums at some positions equal these numbers, the partition is determined: the cuts are at the points where cumulative sum equals those numbers. If there are multiple positions with the same cumulative sum? That can't happen because cumulative sums strictly increase. So partition is unique.
Thus no issues.
Now I'll produce final answer.
But the problem might be from an online judge, need to handle multiple test cases.
I'll write the solution in a clear manner.
Now some additional considerations: Could the phrase be exactly the whole input (i.e., no irrelevant text)? That's allowed.
What if there are multiple phrases, we output the first starting index. Our algorithm finds the smallest i.
Now testing with sample:
First dataset: n=9, lens: [2,3,4,3,5,5,2,3,4]? Actually let's list: do(2), the(3), best(4), and(3), enjoy(5), today(5), at(2), acm(3), icpc(4). Starting i=0:
target5: sum 2+3=5 pos=2.
target7: lens[2]=4 sum=4<7, lens[3]=3 sum=7 pos=4.
target5: lens[4]=5 sum=5 pos=5.
target7: lens[5]=5 sum=5<7, lens[6]=2 sum=7 pos=7.
target7: lens[7]=3 sum=3<7, lens[8]=4 sum=7 pos=9. All good -> output 1. Yes.
Second dataset: lens: [2,3,2,3,2,2,3,2,3,2,2,3,4,3] Starting i=0:
target5: 2+3=5 pos=2.
target7: lens[2]=2 sum=2<7, lens[3]=3 sum=5<7, lens[4]=2 sum=7 pos=5.
target5: lens[5]=2 sum=2<5, lens[6]=3 sum=5 pos=7.
target7: lens[7]=2 sum=2<7, lens[8]=3 sum=5<7, lens[9]=2 sum=7 pos=10.
target7: lens[10]=2 sum=2<7, lens[11]=3 sum=5<7, lens[12]=4 sum=9 >7 fail. So i=0 fails.
i=1: lens[1]=3 target5: 3+? lens[2]=2 ->5 pos=3.
target7: lens[3]=3 sum=3<7, lens[4]=2 sum=5<7, lens[5]=2 sum=7 pos=6.
target5: lens[6]=3 sum=3<5, lens[7]=2 sum=5 pos=8.
target7: lens[8]=3 sum=3<7, lens[9]=2 sum=5<7, lens[10]=2 sum=7 pos=11.
target7: lens[11]=3 sum=3<7, lens[12]=4 sum=7 pos=13. All good -> output 2. Works.
Third dataset: as earlier -> output 6.
All good.
Now, we should consider if there is any case where the greedy approach could succeed but the block is not contiguous? The greedy uses contiguous words, so it's fine.
Now one more nuance: The problem says: "the sequence of words can be divided into five sections such that the total number of the letters in the word(s) of the first section is five, that of the second is seven, and those of the rest are five, seven, and seven, respectively." It doesn't explicitly state that the sections are contiguous, but in natural language "divided into five sections" implies consecutive groups. The example also shows contiguous grouping. So it's safe.
Thus final answer.
We'll produce a solution in Python 3.
I will include a main guard.
Potential improvement: Use a list of targets = [5,7,5,7,7] and iterate.
Now time to write final message.
But before final, let's double-check input reading: Using sys.stdin.read().split() will split by whitespace, so we get tokens: first token is n, then n words. Works.
Edge: n could be up to 40, but we must handle trailing newline.
Now produce final answer.
One more check: Could there be a case where the phrase uses words that are not necessarily starting at i but maybe skipping some words in the middle? No, the phrase must be a contiguous block according to problem (since it's a Short Phrase with possibly prefix and suffix). So it's a substring.
Thus solution stands.
I'll write the code accordingly.
Now for final output: Print each result line.
Thus final. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
it = iter(data)
out_lines = []
TARGETS = [5, 7, 5, 7, 7]
while True:
try:
n = int(next(it))
except StopIteration:
break
if n == 0:
break
lengths = []
for _ in range(n):
word = next(it)
lengths.append(len(word))
answer = None
for start in range(n):
pos = start
ok = True
for t in TARGETS:
if pos >= n: # not enough words left
ok = False
break
s = 0
while pos < n and s < t:
s += lengths[pos]
pos += 1
if s != t: # overshoot or not enough
ok = False
break
if ok:
answer = start + 1 # 1‑based index
break
out_lines.append(str(answer))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()