21423_FreeWilly

2022-5-16 18:19| 发布者: Hocassian| 查看: 25| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-4-89505250763299-Problem List-采集的数据-后羿采集器.html

Pro.ID

21423

Title

Free Willy

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21423

AC

3

Submit

31

Ratio

9.68%

时间&空间限制

  • Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    Willy is sitting behind bars in Alcatraz. Jan Erik Vold is guarding him, and gives him a challenge:

    "I managed to transform KULTURUKE into UKTURKULE by applying these permutations in succession:

    bcdefaghi cabfdeghi bcadefghi adcefgbhi cgabdefhi cdaefhgbi

    That's what gave me the start of my great poem: KULTURUKE ULTURKUKE TULKURUKE ULTKURUKE UKTURULKE TLUKURUKE UKTURKULE

    Now, I want you to do the transformation using the same set of available permutations. I permuted 6 times, but if you can manage to do it by permuting fewer times than I did, then I'll unlock the cage!"

    "That's easy, I only need 4 permutations!" says Willy, "You first apply bcadefghi to get ULKTURUKE. Then cdaefhgbi to get KTUURKULE. Then bcadefghi again to get TUKURKULE. And finally bcadefghi a third time to get UKTURKULE."

    "Oh, you're not a big, dumb fish after all" says Jan Erik and brings out the keys. Willy jumps into the ocean and lives happily ever after!

    输入

    The first line of the input gives the number of test cases T ≤ 30. The first line of each test case contains 1 ≤ N ≤ 26, 1 ≤ P ≤ 10, and 1 ≤ L ≤ 10. The second line contains two words with N characters each. Then follow P lines, each with an allowed permutation of the first N letters of the alphabet (in lowercase).

    输出

    Description

    Willy is sitting behind bars in Alcatraz. Jan Erik Vold is guarding him, and gives him a challenge:

    "I managed to transform KULTURUKE into UKTURKULE by applying these permutations in succession:

    bcdefaghi cabfdeghi bcadefghi adcefgbhi cgabdefhi cdaefhgbi

    That's what gave me the start of my great poem: KULTURUKE ULTURKUKE TULKURUKE ULTKURUKE UKTURULKE TLUKURUKE UKTURKULE

    Now, I want you to do the transformation using the same set of available permutations. I permuted 6 times, but if you can manage to do it by permuting fewer times than I did, then I'll unlock the cage!"

    "That's easy, I only need 4 permutations!" says Willy, "You first apply bcadefghi to get ULKTURUKE. Then cdaefhgbi to get KTUURKULE. Then bcadefghi again to get TUKURKULE. And finally bcadefghi a third time to get UKTURKULE."

    "Oh, you're not a big, dumb fish after all" says Jan Erik and brings out the keys. Willy jumps into the ocean and lives happily ever after!

    Input

    The first line of the input gives the number of test cases T ≤ 30. The first line of each test case contains 1 ≤ N ≤ 26, 1 ≤ P ≤ 10, and 1 ≤ L ≤ 10. The second line contains two words with N characters each. Then follow P lines, each with an allowed permutation of the first N letters of the alphabet (in lowercase).

    Output

    For each test case, output one line with the minimum number of times you need to apply one of the allowed permutations to the letters of the first word in order to arrive at the second word, or "whalemeat" if it's not possible to do it in at most L steps.

    Sample Input

    3
    9 6 5
    KULTURUKE UKTURKULE
    bcdefaghi
    cabfdeghi
    bcadefghi
    adcefgbhi
    cgabdefhi
    cdaefhgbi
    9 5 4
    kulturuke tlukuruke
    bcdefaghi
    cabfdeghi
    bcadefghi
    adcefgbhi
    cgabdefhi
    9 3 4
    WILLFREEY FREEWILLY
    bacdefghi
    abghefdic
    fecdbaigh

    Sample Output

    4
    whalemeat
    4

    Source

    样例输入

    3
    9 6 5
    KULTURUKE UKTURKULE
    bcdefaghi
    cabfdeghi
    bcadefghi
    adcefgbhi
    cgabdefhi
    cdaefhgbi
    9 5 4
    kulturuke tlukuruke
    bcdefaghi
    cabfdeghi
    bcadefghi
    adcefgbhi
    cgabdefhi
    9 3 4
    WILLFREEY FREEWILLY
    bacdefghi
    abghefdic
    fecdbaigh

    样例输出

    4
    whalemeat
    4

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部