编程介的小学生 2019-09-16 21:58 采纳率: 20.5%
浏览 168

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条回答

    报告相同问题?

    悬赏问题

    • ¥30 matlab解优化问题代码
    • ¥15 写论文,需要数据支撑
    • ¥15 identifier of an instance of 类 was altered from xx to xx错误
    • ¥100 反编译微信小游戏求指导
    • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
    • ¥15 学不会递归,理解不了汉诺塔参数变化
    • ¥15 基于图神经网络的COVID-19药物筛选研究
    • ¥30 软件自定义无线电该怎样使用
    • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
    • ¥15 Jenkins+k8s部署slave节点offline