問題 B: Friendship of Mouse
時間限制: 1 Sec 內存限制: 64 MB提交: 24 解決: 15
[提交][狀態][討論版]
題目描述
The closest friends are a pair of friends with the minimum distance. Help us find that distance.
輸入
Every test case only contains a string with length N, and the ith character of the string indicates the city of ith mice.
⋅ 1≤T≤50.
⋅ for 80% data, 1≤N≤100.
⋅ for 100% data, 1≤N≤2000.
⋅ the string only contains lowercase letters.
輸出
樣例輸入
2
abcecba
abc
樣例輸出
Case #1: 2
Case #2: -1
提示
//
// main.cpp
// Friendship of Mouse
//
// Created by 張嘉韜 on 16/9/4.
// Copyright © 2016年 張嘉韜. All rights reserved.
//
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
const int inf=1<<30;
map <char,int> pos;
int main(int argc, const char * argv[]) {
//freopen("/Users/zhangjiatao/Documents/ACM/input.txt","r",stdin);
int T;
scanf("%d",&T);
for(int t=1;t<=T;t++)
{
pos.clear();
string a;
cin>>a;
int minimum=inf;
for(int i=0;i<a.size();i++)
{
char temp=a[i];
if(pos[temp]==0) pos[temp]=i+1;
else
{
minimum=min(minimum,i+1-pos[temp]);
pos[temp]=i+1;
}
}
if(minimum==inf) minimum=-1;
printf("Case #%d: %d\n",t,minimum);
}
return 0;
}