编程介的小学生 2017-04-20 06:07 采纳率: 20.5%
浏览 731
已采纳

Bookcase

Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C.P), Patchouli Knowledge decides to rearrange the books on her bookcase.

TH_Patchouli.jpg
Patchouli Knowledge is a powerful magician who lives in Scarlet Devil Mansion. In Scarlet Devil Mansion there's a large library under the ground, and Patchouli Knowledge lives in it. Of course, it's dark and uncomfortable, however it's a good quiet place to do some research or study on new powerful magics.

In the library there's a large bookcase. For convenience, we assume that it has N levels of shelves. On each shelves there are M books. Of course, each book has a title, such as How to survive in Master Spark or Become a baka in nine minutes. Patchouli Knowledge reads a large number of books every day, so the bookcase is messy.

She decides to rearrange her books in ASCII order. For example, the book Two Dimension Mania should be put before the book Utawarerumono, and Zombies should be put before galgame. The books may have the same name, and the spaces are also considered when comparing titles in ASCII order.

Each time, she will choose one book from one shelf, and insert it in any place of the same shelf. However, because of asthma, it will cost her one unit of energy on each moves. Patchouli Knowledge is unable to do a lot of works. So she wants to know the minimal energy cost she need to rearrange all the books.

Input

There are multiple test cases. For each test case:

The first line has two numbers N (1 <= N <= 50) as the level of the shelves and M (1 <= M <= 50) as the number of the books on each shelf.
Then followed by N blocks, each block describes a shelf that contains M lines. Each line of a block is a string (no more than 50 characters) indicates a book's title on the shelf. We assume all titles only have A to Z, a to z and spaces.

Output

For each test case, output the answer as the description required.

Sample Input

2 2
Two Dimension Mania
Utawarerumono
Utawarerumono
Two Dimension Mania
Sample Output

1

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-05-05 03:35
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮