# Minimum number of was to write a string

Consider the following question

There are K magical pens (numbered 1 through K). You are given strings P1,P2,…,PK (each of which consists of characters from ‘a’, ‘b’, …, ‘t’) ; for each valid i, the i-th pen can only write letters from the string Pi

.

You want to write a word S of length N. All the characters of S are between ‘a’ and ‘t’ inclusive. This string must be written from left to right. To write it, you pick up some pen and start writing; after you’ve written some prefix of S, you can put down that pen, pick up another pen, continue writing S from the point where you put down the previous pen, later pick up another pen (any pen) and continue writing S with that pen, and so on until you write the whole string S You may pick up each pen any number of times, including zero. You have to find a way of writing the word S such that the number of times you change the pen (put down the pen you’re currently writing with and pick up another) is the smallest possible. If there are multiple solutions, you may find any one. It is guaranteed that it is possible to write S with the given pens.

I know that to represnt a pen as a number using bitset and solve it ,but how can i solve it faster.