program main implicit none character*10::S character::c integer::a(26),ca integer::num(10) integer::pat(1024),spat(1024) integer::len,i,j,k integer::N integer,allocatable::x(:),y(:) data a/26*0/,num/10*0/ read *,S len = LEN_TRIM(S) N = 0 ca = ICHAR('a') do i=0,25 c = CHAR(ca+i) if(SCAN(S,c).eq.0) cycle do j=1,len if(S(j:j).eq.c) num(j) = N end do N = N + 1 end do allocate(x(2**len),y(2**len)) do i=1,2**len k = 0 x(i) = 0 do j=0,len-1 if(btest(i,j).eqv..true.) then k = k + 1 x(i) = x(i) + num(k)*10**j else x(i) = x(i) + num(len-(j-k))*10**j end if end do end do y = qsort(x) j = 1 do i = 1,2**len-1 if(y(i).ne.y(i+1)) then j = j + 1 end if end do print '(i0)', j contains recursive function qsort(x) result(y) integer,intent(in) ::x(:) integer,allocatable::y(:) integer::pivot,total total = size(x) if (total <=1) then y = x else pivot = x(total/2) y = [qsort(pack(x, x .lt. pivot)), & pack(x, x .eq. pivot), & qsort(pack(x, x .gt. pivot))] endif end function qsort end program main