编程介的小学生 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
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?