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 ...
随机推荐
- superset docker 部署
公众号原文有更多效果图哦 一.使用自己的数据库 1. 拉取项目 // 创建目录用于存放项目 mkdir -p /mnt/superset cd /mnt/superset git clone http ...
- Codeforces Round 513 (Div.1+Div.2)
比赛传送门 10月4号的比赛,因为各种原因(主要是懒),今天才写总结-- Div1+Div2,只做出两个题+迟到\(20min\),日常掉\(rating\)-- \(\rm{A.Phone\;Num ...
- linux 安装并且设置环境lua环境变量
在lua官网下载lua安装包并安装: http://www.lua.org/download.html 解压编译: wget http://www.lua.org/ftp/lua-5.3.2.tar. ...
- 控制nginx并发链接数量和客户端请求nginx的速率
一.控制nginx并发链接数 ngx_http_limit_conn_module这个模块用于限制每个定义的key值的链接数,特别是单IP的链接数. 不是所有的链接数都会被计数,一个符合计数要求的连接 ...
- PyCharm2019 激活方式
1.修改hosts激活:需要修改hosts,稳定无影响,持续更新,推荐~ 一.修改hosts激活 1.修改hosts文件 将0.0.0.0 account.jetbrains.com和0.0.0.0 ...
- shell中变量字符串的截取 与 带颜色字体、背景输出
字符串截取 假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个 /及其左边的字符串:dir ...
- perl-basic-分支&循环
if elsif shorter if: if+condition放在句子尾部. use strict; use warnings; my $word = "antidisestablish ...
- Java并发编程的艺术 记录(二)
volatile的应用 volatile的定义如下:Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致地更新,线程应该确保通过排他锁单独获得这个变量.Java语言提供了volatil ...
- LightOj:1422-Halloween Costumes
传送门:http://www.lightoj.com/volume_showproblem.php?problem=1422 Halloween Costumes problem descriptio ...
- poj--1064
题意:有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子最长能有多长?答案保留到小数点后2位. 思路:这些最大最小化问题大多数可以用二分查找的方法来解题 用 d 表 ...