acdream 1146 Moist

原创 2015年07月07日 13:42:04

Problem Description

Moist has a hobby -- collecting figure skating trading cards. His card collection has been growing, and it is now too large to keep in one disorganized pile. Moist needs to sort the cards in alphabetical order, so that he can find the cards that he wants on short notice whenever it is necessary.

The problem is -- Moist can't actually pick up the cards because they keep sliding out his hands, and the sweat causes permanent damage. Some of the cards are rather expensive, mind you. To facilitate the sorting, Moist has convinced Dr. Horrible to build him a sorting robot. However, in his rather horrible style, Dr. Horrible has decided to make the sorting robot charge Moist a fee of $1 whenever it has to move a trading card during the sorting process.

Moist has figured out that the robot's sorting mechanism is very primitive. It scans the deck of cards from top to bottom. Whenever it finds a card that is lexicographically smaller than the previous card, it moves that card to its correct place in the stack above. This operation costs $1, and the robot resumes scanning down towards the bottom of the deck, moving cards one by one until the entire deck is sorted in lexicographical order from top to bottom.

As wet luck would have it, Moist is almost broke, but keeping his trading cards in order is the only remaining joy in his miserable life. He needs to know how much it would cost him to use the robot to sort his deck of cards.

Input

The first line of the input gives the number of test cases, T(1 ≤ T ≤ 100).

T test cases follow.

Each one starts with a line containing a single integer, N(1 ≤ N ≤ 100).

The next N lines each contain the name of a figure skater, in order from the top of the deck to the bottom.

Each name will consist of only letters and the space character.
Each name will contain at most 100 characters.
No name with start or end with a space.
No name will appear more than once in the same test case.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of dollars it would cost Moist to use the robot to sort his deck of trading cards.

Sample Input

3
2
Oksana Baiul
Michelle Kwan
3
Elvis Stojko
Evgeni Plushenko
Kristi Yamaguchi
4
Evgeni Plushenko
Kristi Yamaguchi
Elvis Stojko
Aksana Baiul

Sample Output

Case #1: 1
Case #2: 0
Case #3: 2

类似插入排序,统计插入的次数。

/*
* this code is made by yuexunjiang
* Problem: 1146
* Verdict: Accepted
* Submission Date: 2015-07-07 13:37:24
* Time: 4MS
* Memory: 1692KB
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN = 100 + 5;
 
char ch[MAXN][MAXN];
vector<char*> v;
 
int main(){
#if 0
    freopen("A.in","rt",stdin);
#endif
    int t,n,num = 1;
    cin >>t;
    while(t--){
        cin >>n;
        getchar();
        v.clear();
        for(int i = 0;i < n;i++){
            fgets(ch[i],MAXN,stdin);
            v.push_back(ch[i]);
        }
        int cnt = 0;
        for(vector<char *>::iterator it=v.begin()+1; it!=v.end(); ++it){
            if(strcmp(*it, *(it-1)) < 0){
                v.erase(it);
                --it;
                cnt++;
            }
        }
        cout <<"Case #" <<num++ <<": "<<cnt <<endl;
    }
    return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Google Practice Round_C: Moist

Problem Moist has a hobby -- collecting figure skating trading cards. His card collection has been ...

Moist (cin,getline的区别)

ProblemMoist has a hobby – collecting figure skating trading cards. His card collection has been gro...

Google Code Jam Notes - Moist - Java

Problem: http://code.google.com/codejam/contest/2845486/dashboard#s=p0 Analysis: There is no n...

Moist - Practice Round China New Grad Test 2014 - 插入排序

Moist 题意:简单的字符串处理与插入排序。难度:1星。 从文件读取字符串数据之后,模拟插入排序过程,计算插入次数,再将计数输出到文件。 需要注意: 1)C++的文件操作; 2)从文件读取时,在读取...

junit测试中Hibernate报错SQL Error:1146,SQLState:42S02

此问题困扰笔者很久,希望能帮到一些初学者。 先不多说了,初学Hibernate框架写了一个Demo,结果一直运行不出来想要的结果。 先描述下环境:在eclipse中添加了Hiberna...
  • qzcsu
  • qzcsu
  • 2016-10-05 21:51
  • 604

nec 78f1146 单片机

  • 2009-10-25 17:29
  • 10.73MB
  • 下载

joj1146

1146: Word Reversal Result TIME Limit MEMORY Limit Run Times AC Times JUDGE 3...

HYSBZ 1146 网络管理Network Treap+线段树+树链

题意很简单:在一颗树上修改某个点的值,然后查询两点之间路径上的第k大的元素查阅了很多资料,因为不会Treap,链接如下: http://blog.csdn.net/ssccode/article/...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)