编程介的小学生 2017-09-16 18:08 采纳率: 20.5%
浏览 748
已采纳

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-09-30 20:14
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题