codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

653B.md (3451B)


      1 Source: https://codeforces.com/problemset/problem/653/B
      2 
      3 B. Bear and Compressing
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should also know that Limak is so young that he knows only first six letters of the English alphabet: 'a', 'b', 'c', 'd', 'e' and 'f'.
     22 
     23 You are given a set of *q* possible operations. Limak can perform them in any order, any operation may be applied any number of times. The *i*-th operation is described by a string *a**i* of length two and a string *b**i* of length one. No two of *q* possible operations have the same string *a**i*.
     24 
     25 When Limak has a string *s* he can perform the *i*-th operation on *s* if the first two letters of *s* match a two-letter string *a**i*. Performing the *i*-th operation removes first two letters of *s* and inserts there a string *b**i*. See the notes section for further clarification.
     26 
     27 You may note that performing an operation decreases the length of a string *s* exactly by 1. Also, for some sets of operations there may be a string that cannot be compressed any further, because the first two letters don't match any *a**i*.
     28 
     29 Limak wants to start with a string of length *n* and perform *n* - 1 operations to finally get a one-letter string "a". In how many ways can he choose the starting string to be able to get "a"? Remember that Limak can use only letters he knows.
     30 
     31 Input
     32 
     33 The first line contains two integers *n* and *q* (2 ≤ *n* ≤ 6, 1 ≤ *q* ≤ 36) — the length of the initial string and the number of available operations.
     34 
     35 The next *q* lines describe the possible operations. The *i*-th of them contains two strings *a**i* and *b**i* (|*a**i*| = 2, |*b**i*| = 1). It's guaranteed that *a**i* ≠ *a**j* for *i* ≠ *j* and that all *a**i* and *b**i* consist of only first six lowercase English letters.
     36 
     37 Output
     38 
     39 Print the number of strings of length *n* that Limak will be able to transform to string "a" by applying only operations given in the input.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 3 5  
     47 ab a  
     48 cc c  
     49 ca a  
     50 ee c  
     51 ff d
     52 ```
     53 
     54 Output
     55 
     56 ```
     57 4
     58 ```
     59 
     60 Input
     61 
     62 ```
     63 2 8  
     64 af e  
     65 dc d  
     66 cc f  
     67 bc b  
     68 da b  
     69 eb a  
     70 bb b  
     71 ff c
     72 ```
     73 
     74 Output
     75 
     76 ```
     77 1
     78 ```
     79 
     80 Input
     81 
     82 ```
     83 6 2  
     84 bb a  
     85 ba a
     86 ```
     87 
     88 Output
     89 
     90 ```
     91 0
     92 ```
     93 
     94 Note
     95 
     96 In the first sample, we count initial strings of length 3 from which Limak can get a required string "a". There are 4 such strings: "abb", "cab", "cca", "eea". The first one Limak can compress using operation 1 two times (changing "ab" to a single "a"). The first operation would change "abb" to "ab" and the second operation would change "ab" to "a".
     97 
     98 Other three strings may be compressed as follows:
     99 
    100 * "cab" ![](https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png) "ab" ![](https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png) "a"
    101 * "cca" ![](https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png) "ca" ![](https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png) "a"
    102 * "eea" ![](https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png) "ca" ![](https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png) "a"
    103 
    104 In the second sample, the only correct initial string is "eb" because it can be immediately compressed to "a".