program main implicit none character*300::Stmp,S character*3,parameter::w='w' character*300::Sw,output integer::len,wlen,start,before,i,swlen,j read *,Stmp do i=1,LEN_TRIM(Stmp),3 if(.not.str_eq(Stmp(i:i+2),w(1:3))) exit end do S = Stmp(i:) len = LEN_TRIM(S) wlen = get_wlen(S) Sw = REPEAT(w,wlen) swlen = wlen*LEN_TRIM(w) start = 1 do i=2,len if(str_eq(S(i:i+swlen-1),Sw(1:swlen))) then do j=i-1,1,-1 if(str_eq(S(j:j+2),w)) then before = j+3 exit end if end do print '(a)',S(before:i-1) end if end do contains function str_eq(S1,S2) result(equal) character(*)::S1,S2 integer::len,i logical::equal equal=.true. len = LEN_TRIM(S2) do i=1,len if(S1(i:i).ne.S2(i:i)) then equal = .false. return end if end do end function str_eq function get_wlen(S) result(wlen) character(*)::S character*3,parameter::w='w' integer::i,j,len,wmax=0,wlen,wprev len = LEN_TRIM(S) wlen = 0 wprev = 0 do j=1,len-2 if(S(j:j+2).eq.w(1:3)) then if((wprev+3.ge.j.or.wprev.eq.0)) then wprev = j wlen = wlen + 1 wmax = MAX(wmax, wlen) else wprev = j wlen = 1 wmax = MAX(wmax, wlen) end if end if end do wlen = wmax end function get_wlen end program main