编程介的小学生 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 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题