Google 2014校招测试赛 Problem A

简介:

Problem A. Bad Horse

Problem

As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members. Being the Thoroughbred of Sin, Bad Horse isn't about to spend his valuable time figuring out how to split the League members by himself. That what he's got you -- his loyal henchman -- for.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a positive integer M on a line by itself -- the number of troublesome pairs of League members. The next M lines each contain a pair of names, separated by a single space.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "Yes" or "No", depending on whether the League members mentioned in the input can be split into two groups with neither of the groups containing a troublesome pair.

Limits

1 ≤ T ≤ 100.
Each member name will consist of only letters and the underscore character.
Names are case-sensitive.
No pair will appear more than once in the same test case.
Each pair will contain two distinct League members.

Small dataset

1 ≤ M ≤ 10.

Large dataset

1 ≤ M ≤ 100.

Sample


Input  
2
1
Dead_Bowie Fake_Thomas_Jefferson
3
Dead_Bowie Fake_Thomas_Jefferson
Fake_Thomas_Jefferson Fury_Leika
Fury_Leika Dead_Bowie

Output
Case #1: Yes
Case #2: No
AI 代码解读


我的解法是,将数据放入list中。

  1. 先取出一对记录,分别放入set1和set2。并且将两个记录分别放入stack1和stack2。

  2. 然后从stack1中逐个取出元素,对每个元素执行的操作是,从list中取出还有这个元素的一堆记录,然后将一对中的另一个放入set2和stack2。

  3. 然后对stack2执行类似2的操作,一对中另一个放入set1和stack1。

  4. 重复执行2和3直到stack1和stack2空。

  5. 重复执行1,2,3,4直到list空

  6. 检测set1和set2中是否有相同的元素。

关键代码如下:


boolean solve(List<String> list) {
    Set<String> set1 = new HashSet<String>();
    Set<String> set2 = new HashSet<String>();
    Stack<String> stack1 = new Stack<String>();
    Stack<String> stack2 = new Stack<String>();
    while (list.size() > 0) {
        String p1 = list.get(0);
        list.remove(0);
        String p2 = list.get(0);
        list.remove(0);
        set1.add(p1);
        set2.add(p2);
        stack1.push(p1);
        stack2.push(p2);
        while (!stack1.empty() || !stack2.empty()) {
            if (!stack1.empty()) {
                String s1 = stack1.pop();
                int index = list.indexOf(s1);
                while (index >= 0) {
                    String s2;
                    if (index % 2 == 1) {
                        s2 = list.get(index - 1);
                        list.remove(index - 1);
                        list.remove(index - 1);
                    } else {
                        s2 = list.get(index + 1);
                        list.remove(index);
                        list.remove(index);
                    }
                    stack2.push(s2);
                    set2.add(s2);
                    index = list.indexOf(s1);
                }
            }
            if (!stack2.empty()) {
                String s1 = stack2.pop();
                int index = list.indexOf(s1);
                while (index >= 0) {
                    String s2;
                    if (index % 2 == 1) {
                        s2 = list.get(index - 1);
                        list.remove(index);
                        list.remove(index - 1);
                    } else {
                        s2 = list.get(index + 1);
                        list.remove(index + 1);
                        list.remove(index);
                    }
                    stack1.push(s2);
                    set1.add(s2);
                    index = list.indexOf(s1);
                }
            }
        }
    }
    System.err.println("comapre set");
    // set1 与set2是否有相同元素
    for (String s : set1) {
        if (set2.contains(s))
            return false;
    }
    return true;
}

void run() {
    int COUNT = sc.nextInt();
    for (int c = 0; c < COUNT; c++) {
        int M = sc.nextInt();
        List<String> list = new ArrayList<String>();
        for (int m = 0; m < M; m++) {
            String p1 = sc.next();
            String p2 = sc.next();
            list.add(p1);
            list.add(p2);
        }
        boolean result = solve(list);
        System.out.println(String.format("Case #%d: %s", c + 1,
            result ? "Yes" : "No"));
    }
}
AI 代码解读


目录
打赏
0
0
0
0
5
分享
相关文章
Google Gemini意外超越OpenAI,跃居第一,但基准测试结果并不能说明全部情况
Google Gemini意外超越OpenAI,跃居第一,但基准测试结果并不能说明全部情况
|
11月前
google测试框架
google测试框架
73 0
Google Earth Engine(GEE)——使用MODIS数据单点测试SG滤波和harmonics method 滤波的差异分析
Google Earth Engine(GEE)——使用MODIS数据单点测试SG滤波和harmonics method 滤波的差异分析
369 0
好饭不怕晚,Google基于人工智能AI大语言对话模型Bard测试和API调用(Python3.10)
谷歌(Google)作为开源过著名深度学习框架Tensorflow的超级大厂,是人工智能领域一股不可忽视的中坚力量,旗下新产品Bard已经公布测试了一段时间,毁誉参半,很多人把Google的Bard和OpenAI的ChatGPT进行对比,Google Bard在ChatGPT面前似乎有些技不如人。 事实上,Google Bard并非对标ChatGPT的产品,Bard是基于LaMDA模型对话而进行构建的,Bard旨在构建一个对话式的AI系统,使其能够更好地理解人类语言,并且具备进行多轮对话的能力。而GPT的目标是生成自然语言文本。
好饭不怕晚,Google基于人工智能AI大语言对话模型Bard测试和API调用(Python3.10)
流量如何才能变现?实际测试谷歌广告联盟(Google Adsense)的广告效果以及如何优化相关代码
2010年,谷歌正式退出中国市场,无数人扼腕叹息,如今十年过去了,谷歌还有两条重要的业务线并没有完全退出,一个是页面统计业务(Google Analytics),另外一个则是谷歌广告联盟(Google Adsense),说起广告联盟,玩儿过网站的朋友应该并不陌生,对于中小型站长、博主来说,要想通过网站的流量取得一些收入,除了和一些线下线上厂商谈包月广告位,更多的可能就是投放广告联盟广告了。但随着网络广告的不断发展,广告形式有了很大的变化,出现了CPC、CPS、CPA、CPV等众多广告类型。
流量如何才能变现?实际测试谷歌广告联盟(Google Adsense)的广告效果以及如何优化相关代码
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)
185 0
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(二)
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(一)
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(一)
358 0
【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 内部测试链接 | 安装 Google Play 中带 扩展文件 的 APK 安装包 | 验证下载的扩展文件 )(一)
除了postman还有什么接口测试工具
最好还是使用国内的接口测试软件,其实国内替换postman的软件有很多,这里我推荐使用yunedit-post这款接口测试工具来代替postman,因为它除了接口测试功能外,在动态参数的支持、后置处理执行sql语句等支持方面做得比较好。而且还有接口分享功能,可以生成接口文档给团队在线浏览。
33 2
Socket.IO介绍,以及怎么连接测试Socket.IO接口?
Socket.IO 是一个用于浏览器和服务器间实时双向通信的库,支持低延迟消息传递、跨平台运行及自动重连。文章介绍了其特点与调试需求,并详细说明如何使用 Apifox 工具创建、连接、发送/接收 Socket.IO 事件,以及团队协作和调试技巧。掌握这些技能可提升实时应用开发效率与质量。
Python测试淘宝店铺所有商品接口的详细指南
本文详细介绍如何使用Python测试淘宝店铺商品接口,涵盖环境搭建、API接入、签名生成、请求发送、数据解析与存储、异常处理等步骤。通过具体代码示例,帮助开发者轻松获取和分析淘宝店铺商品数据,适用于电商运营、市场分析等场景。遵守法规、注意调用频率限制及数据安全,确保应用的稳定性和合法性。

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等