A problem in Google code jam & its DP solution
来源:互联网 发布:nba2k16球员动作数据 编辑:程序博客网 时间:2024/04/30 12:46
Problem
The urban legend goes that if you go to the Google homepage and search for "Google", the universe will implode. We have a secret to share... It is true! Please don't try it, or tell anyone. All right, maybe not. We are just kidding.
The same is not true for a universe far far away. In that universe, if you search on any search engine for that search engine's name, the universe does implode!
To combat this, people came up with an interesting solution. All queries are pooled together. They are passed to a central system that decides which query goes to which search engine. The central system sends a series of queries to one search engine, and can switch to another at any time. Queries must be processed in the order they're received. The central system must never send a query to a search engine whose name matches the query. In order to reduce costs, the number of switches should be minimized.
Your task is to tell us how many times the central system will have to switch between search engines, assuming that we program it optimally.
Input
The first line of the input file contains the number of cases, N. N test cases follow.
Each case starts with the number S -- the number of search engines. The next S lines each contain the name of a search engine. Each search engine name is no more than one hundred characters long and contains only uppercase letters, lowercase letters, spaces, and numbers. There will not be two search engines with the same name.
The following line contains a number Q -- the number of incoming queries. The next Q lines will each contain a query. Each query will be the name of a search engine in the case.
Output
For each input case, you should output:
Case #X: Y
where X is the number of the test case and Y is the number of search engine switches. Do not count the initial choice of a search engine as a switch.
Limits
0 < N ≤ 20
Small dataset
2 ≤ S ≤ 10
0 ≤ Q ≤ 100
Large dataset
2 ≤ S ≤ 100
0 ≤ Q ≤ 1000
Sample
Input
2
5
Yeehaw
NSM
Dont Ask
B9
Googol
10
Yeehaw
Yeehaw
Googol
B9
Googol
NSM
B9
NSM
Dont Ask
Googol
5
Yeehaw
NSM
Dont Ask
B9
Googol
7
Googol
Dont Ask
NSM
NSM
Yeehaw
Yeehaw
Googol
Output:
Case #1: 1
Case #2: 0
In the first case, one possible solution is to start by using Dont Ask, and switch to NSM after query number 8.
For the second case, you can use B9, and not need to make any switches.
Answer: (author: CliveStudio):
DP Algorithm:
Def. Cost[k][i] as the min switches
when k queries come and current search engine is i
0<=i<=S
0<=k<=Q
1. Optimal: if i!=id[k]: Cost[k][i]=min{..., Cost[k-1][i-2]+1, Cost[k-1][i-1]+1, Cost[k-1][i], Cost[k-1][i+1]+1, Cost[k-1][i+2]+1,...}
else: Cost[k][i]=min{..., Cost[k-1][i-2]+1, Cost[k-1][i-1]+1, Cost[k-1][i+1]+1, Cost[k-1][i+2]+1,...}
2. Init: Cost[0][i]=0
- A problem in Google code jam & its DP solution
- Google code jam: Problem A. Store Credit
- Google code jam: Problem A. Alien Language
- 【Google Code Jam】Problem A. Store Credit
- Google code jam: Problem A. Minimum Scalar Product
- Google Code Jam 2014 Problem A. Magic Trick
- Google Code Jam 2014 预赛 Problem A. Magic Trick
- Google Code Jam 2014资格赛【Problem A. Magic Trick】
- Google Code Jam 2014 Qualification Round:Problem A. Magic Trick
- Google Code Jam 2015 Qualification Round: Problem A. Standing Ovation
- google code jam Round 1A 2015 Problem C. Logging
- Google Code Jam Round 1A 2015 Problem B. Haircut
- Google Code Jam 2015 Round 1A: Problem B. Haircut
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
- [Google Code Jam 2017] Round 2 problem A
- Google code jam Round1B Problem B
- Google code jam: Problem B. Reverse Words
- Google Code Jam-Problem B. Reverse Words
- 在C++中实现C#的delegate机制
- 深圳 经济环境
- uip协议栈学习(二)
- 让C++也支持RMI
- 关于数据库备份的问题
- A problem in Google code jam & its DP solution
- 使windows mobile CAB 安装文件安装后不丢失的方法
- sc命令
- JS 完成颜色渐变~~
- OTL调用存储过程和函数
- 免费3个月的VPS送独立IP (经测试可以终身免费)
- Excel 2007中的个人宏工作簿(Personal.xlsb)
- Oracle游标使用方法及语法大全
- 【转】MFC中使菜单变灰