hdu 5578 Friendship of Frog
题意:给定一行字符串(都是小写字母),每一个字符都代表一只青蛙以及其国籍,若字符串中出现两个字符相同,则这两个字符所代表的青蛙来自同一国度,可称之为好朋友。
现在需要找到距离最近的好朋友并输出他们的距离。
思路:建立一个map映射表,映射关系为:字符<->字符所在字符串的位置,从字符串头部到尾部依次进行扫描,每扫到一个字符,可以找一找map表中有没有出现该字符,若没出现过则在
表中建立这个字符的关系,若map表中有该字符的记录,那么说明当前字符串代表的青蛙找到了一个朋友,可以计算他与朋友的距离,并更新最短距离。之后map表也要更新这个字符的信息,
即把字符所在位置改为当前位置以方便后续的操作。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
string s;
map<char, int>m;//coutry<->position
int main() {
int T;
scanf("%d",&T);
int k = ;
while (T--) { k++;
cin >> s;
int dis = INT_MAX;
m.clear();//每次用完需要清空map表
for (int i = ; i < s.size(); i++) {
map<char,int>::iterator it = m.find(s[i]);
if (it != m.end()) {//集合中找到了这只青蛙的同伴
int distance = i - it->second;
if (distance < dis)dis = distance;
m.erase(s[i]);//更新青蛙位置
m.insert(make_pair(s[i], i));
}
else {
m.insert(make_pair(s[i],i));//新元素,记录位置
}
}
if (dis >= s.size())
printf("Case #%d: -1\n", k);
else
printf("Case #%d: %d\n",k,dis); }
return ;
}
hdu 5578 Friendship of Frog的更多相关文章
- HDU 5578 Friendship of Frog 水题
Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...
- hdu 5578 Friendship of Frog(multiset的应用)
Problem Description N frogs . Two frogs are friends if they come from the same country. The closest ...
- hdu-5578 Friendship of Frog(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5578 Friendship of Frog Time Limit: 2000/1000 MS (Jav ...
- Friendship of Frog(水题)
Friendship of Frog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 【HDU 5578】Friendship of Frog
题 题意 求相同字母最近距离 分析 用数组保存各个字母最后出现的位置,维护最小距离. 代码 #include <cstdio> int c[30],n,p,a,minl; char ch; ...
- 2015ACM/ICPC亚洲区上海站
5573 Binary Tree(构造) 题意:给你一个二叉树,根节点为1,子节点为父节点的2倍和2倍+1,从根节点开始依次向下走k层,问如何走使得将路径上的数进行加减最终结果得到n. 联想到二进制. ...
- hdu 4004 The Frog's Games
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 The annual Games in frogs' kingdom started again ...
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
随机推荐
- 个人对spring的IOC+DI的封装
暂时支持8种基本数据类型,String类型,引用类型,List的注入. 核心代码 package day01; import java.lang.reflect.Field;import java.l ...
- ssh整合思想 Spring分模块开发 crud参数传递 解决HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or(增加事务)
在Spring核心配置文件中没有增加事务方法,导致以上问题 Action类UserAction package com.swift.action; import com.opensymphony.xw ...
- c++ 调用php
int _System(const char * cmd, std::string& strRet) { FILE * fp; char * p = NULL; ; if ((fp = _po ...
- iOS应用架构谈-part1概述
当我们讨论客户端应用架构的时候,我们在讨论什么? 其实市面上大部分应用不外乎就是颠过来倒过去地做以下这些事情: --------------- --------------- ------------ ...
- 【交互 细节题 思维题】cf1064E. Dwarves, Hats and Extrasensory Abilities
第一次做交互真有趣……:挺好的细节思维题 This is an interactive problem. In good old times dwarves tried to develop extr ...
- c# 输出不同时间的格式
C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6- ...
- laravel中使用PHPQuery实现网页采集
由于没有PHPQuery的composer包安装所以需要我们手动在我们的laravel项目中安装加载PHPQuery,这里需要设置laravel的autoload->class map. 1.首 ...
- python3.6:DLL load failed:找不到指定的模块(from PyQt5 import QtCore)
本人小白搭建pyqt环境时遇到问题 运行代码 from PyQt5 import QtCore' 发现错误 ImportError: DLL load failed: 找不到指定的模块 这个问题折磨了 ...
- Python9-网络编程4-day33
解决黏包问题: 在传输大量数据之前,先告诉接收端要发送数据大小 如果想更漂亮的解决问题,可以通过struct模块来定制协议为什么会出现黏包现象: 首先只有在tcp协议中才会出现黏包现象 是因为tcp协 ...
- leetcode-21-knapsack
322. Coin Change Write a function to compute the fewest number of coins that you need to make up tha ...