Go: // Short Phrase(短句)
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1601&lang=jp
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		n := nextInt(sc)
		if n == 0 {
			break
		}

		var words []string
		for i := 0; i < n; i++ {
			words = append(words, nextString(sc))
		}

		res := 0
		for i := 0; i < n; i++ {
			tmpWords := words[i:]
			ok := checkRule(tmpWords, 0)
			if ok {
				res = i + 1
				break
			}
		}

		fmt.Println(res)
	}
}

var rule = [5]int{5, 7, 5, 7, 7}

func checkRule(words []string, depth int) bool {
	var shortPhrase = ""
	var i = depth

	if depth == 5 {
		return true
	}

	for len(words) > 0 {
		shortPhrase += words[0]
		words = words[1:]

		if rule[i] == len(shortPhrase) {
			return checkRule(words, i+1) // 短句のルールを満たしたので次の文節へ
		} else if rule[i] < len(shortPhrase) {
			return false // 文節の語数オーバー
		}
	}

	return false // 文節を満たす単語が足りない
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func nextString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: ans_list = []

def make_tanka(num, w_list, i):
    #print(num, w_list, i)
    cnt = 0
    while cnt < num:
        cnt += len(w_list[i])
        i += 1
    return [cnt, i]

while True:
    n = int(input())
    if n == 0:
        break
    w_list = []
    for i in range(n):
        w_list.append(input())

    f1 = 0
    s1 = 0
    f2 = 0
    s2 = 0
    s3 = 0
    i = 0
    ans = -1
    while True:
        #print(i)
        j = i
        f1, j = make_tanka(5, w_list, j)
        if f1 == 5:
            s1, j = make_tanka(7, w_list, j)
            if s1 == 7:
                f2, j = make_tanka(5, w_list, j)
                if f2 == 5:
                    s2, j = make_tanka(7, w_list, j)
                    if s2 == 7:
                        s3, j = make_tanka(7, w_list, j)
                        if s3 == 7:
                            ans = i
                            break
                    if ans != -1:
                        break
                if ans != -1:
                    break
            if ans != -1:
                break
        if ans != -1:
            break
        i += 1
    ans_list.append(ans + 1)

for i in ans_list:
    print(i)

Python: while True:
    n = int(input())
    if n == 0:
        break
    short = [5, 7, 5, 7, 7]
    phase = 0
    res = -1
    arr = [0 for i in range(n)]
    for i in range(n):
        arr[i] = len(list(input()))
    for i in range(n):
        tmpsum = 0
        phase = 0
        for j in range(i, n):
            tmpsum += arr[j]
            if tmpsum > short[phase]:
                break
            if tmpsum == short[phase]:
                if phase == 4:
                    res = i + 1
                    break
                tmpsum = 0
                phase += 1
        if res > 0:
            print(res)
            break

Python: p = [5, 7, 5, 7, 7]
while True:
    n = int(input())
    if n == 0:
        break
    word = [input() for _ in range(n)]
    flag = False
    for i in range(len(word)):
        ip = 0
        phrase = ["" for _ in range(len(p))]
        if flag:
            break
        for j in range(i, len(word)):
            if len(phrase[ip]+word[j]) == p[ip]:
                phrase[ip] += word[j]
                ip += 1
            else:
                phrase[ip] += word[j]
                if len(phrase[ip]) > p[ip]:
                    break
                if len(phrase[ip]) == p[ip]:
                    ip += 1
            if len(phrase[-1]) == 7:
                print(i+1)
                flag = True
                break

Python: while True:
    n = int(input())
    if n == 0:
        break
    
    a = []
    for i in range(n):
        p = list(input())
        a.append(len(p))
    
    def solve(a):
        N = len(a)
        for i in range(N):
            judge = 0
            rest = 5
            for j in range(i,N):
                if judge == 0:
                    rest -= a[j]
                    if rest == 0:
                        judge = 1
                        rest = 7
                    elif rest < 0:
                        break
                elif judge == 1:
                    rest -= a[j]
                    if rest == 0:
                        judge = 2
                        rest = 5
                    if rest < 0:
                        break
                elif judge == 2:
                    rest -= a[j]
                    if rest == 0:
                        judge = 3
                        rest = 7
                    elif rest < 0:
                        break
                elif judge == 3:
                    rest -= a[j]
                    if rest == 0:
                        judge = 4
                        rest = 7
                    elif rest < 0:
                        break
                elif judge == 4:
                    rest -= a[j]
                    if rest == 0:
                        judge = 5
                        break
                    elif rest < 0:
                        break
            if judge == 5:
                return i+1
    print(solve(a))

Python: def inp():
	global n
	n = int(input())
	return n
def pos(p, l):
	global w
	if p < 0:
		return -1
	while l > 0 and p < n:
		l -= w[p]
		p += 1
		if l == 0:
			return p
	return -1
while inp() > 0:
	w = []
	for i in range(n):
		w.append(len(input()))
	for i in range(n):
		if pos(pos(pos(pos(pos(i,5),7),5),7),7) >= 0:
			print(i+1)
			break

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define MOD(x) (x%(mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int haiku[45];
int num[6]={5,7,5,7,7,0};

void solve(int n){
  int s=0,e=0,cnt=0,sum=haiku[0];
  int dast=0;
  
  rep(i,n-4){
    dast=cnt=0;
    s=e=i;
    sum=haiku[i];
    
    while(s!=n&&e!=n){
      if(debug)cout<<"s: "<<s<<"  e:"<<e<<"  cheack( "<<cnt<<" ): "<<num[cnt]<<"  dast: "<<dast<<"  cnt: "<<cnt<<endl;
      if(cnt==5)break;
      if(sum==num[cnt]){
	s=e=e+1;sum=haiku[s];cnt++;
      }else if(sum<num[cnt]){
	e++;sum+=haiku[e];
      }else if(sum>num[cnt]){
	break;
      }
    }
    if(debug)cout<<endl;
    if(debug)cout<<"s: "<<s<<"  e:"<<e<<"  cheack( "<<cnt<<" ): "<<num[cnt]<<"  dast: "<<dast<<"  cnt: "<<cnt<<endl;
    if(cnt==5){
      cout<<i+1<<endl;
      break;
    } 
  }
}


int main(){
  int n;
  string s;
  while(cin>>n){
    if(n==0)break;
    rep(i,n){cin>>s;haiku[i]=s.size();}
    solve(n);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,l[45],L[45],a,d[5]={5,7,5,7,7};
string s;

int main(){
  while(1){
    cin>>n;
    if(!n)break;
    for(int i=1;i<=n;i++){
      cin>>s,l[i]=s.size();
      L[i]=L[i-1]+l[i];
    }
    a=0;
    for(int i=1;i<=n;i++){
      int x=0,s=i;
      for(int j=i;j<=n;j++){
	if(L[j]-L[s-1]==d[x])x++,s=j+1;
	if(!a&&x==5)a=i,cout<<a<<endl;
      }
    }
  }
  return 0;
}

C++: 9
do
the
best
and
enjoy
today
at
acm
icpc
14
oh
yes
by
far
it
is
wow
so
bad
to
me
you
know
hey
15
abcde
fghijkl
mnopq
rstuvwx
yzz
abcde
fghijkl
mnopq
rstuvwx
yz
abcde
fghijkl
mnopq
rstuvwx
yz
0