编程介的小学生 2019-11-20 17:56 采纳率: 20.5%
浏览 109

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 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 spring后端vue前端
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题