编程介的小学生 2019-09-22 10:54 采纳率: 20.5%
浏览 125

Scrolling Sign 滚动的签名

Problem Description
Electric scrolling signs are often used for advertising. A given sign displays exactly k characters. When the sign is switched on, all of the character positions are initially empty (showing spaces). In each time interval, all of the characters on the sign are shifted to the left by one position, and a new character is added at the right-most position. The character that was in the left-most position moves off the sign.

For certain sequences of words, it is possible to reuse characters from one word to form a subsequent word. For example, on a sign with three character positions, the sign can display the message CAT ATE TED by scrolling in the five characters CATED.

The advertiser has a specific message to show using the sign. The faster the message is displayed, the more people will be able to see the whole message. Therefore, your job is to find a way to display all the words of the message by scrolling in the smallest number of letters. In between showing the words of the message, the sign may display other words that are not considered part of the message. However, the words of the message must be shown in the order in which they are given.

Input
The first line of test chunk contains a single integer n, the number of test cases in this chunk to follow. Each test case starts with a line containing a two integers, k, the number of character positions on the sign, and w, the number of words in the message. Each of the two integers is between 1 and 100, inclusive. The following w lines each contain a word of the message comprising exactly k uppercase letters.

Please process to the end of the data file.

Output
For each test case, output a line containing a single integer, the minimum number of letters that must be scrolled into the sign so that it displays all the words of the message.

Sample Input
2
3 2
CAT
TED
3 3
CAT
ATE
TEA
2
3 2
CAT
TED
3 3
CAT
ATE
TEA

Sample Output
5
5
5
5

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 delta降尺度方法,未来数据怎么降尺度
    • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
    • ¥15 再不同版本的系统上,TCP传输速度不一致
    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程