hhoj Friendship of Mouse 水题 TWT Tokyo Olympic 1COMBO-2

爱过

恨过

水过

哈哈哈哈哈1A好爽啊

问题 B: Friendship of Mouse

时间限制: 1 Sec   内存限制: 64 MB
提交: 2   解决: 2
[ 提交][ 状态][ 讨论版]

题目描述

Today in KBW, N mice from different cities are standing in a line. Each city is represented by a lowercase letter. The distance between adjacent mice (e.g. the 1 st and the 2 nd mouse, the N−1 th and the N th mouse, etc) are exactly 1. Two mice are friends if they come from the same city.

The closest friends are a pair of friends with the minimum distance. Help us find that distance.

输入

First line contains an integer T, which indicates the number of test cases.

Every test case only contains a string with length N, and the i th character of the string indicates the city of i th mice.

⋅ 1≤T≤50.

⋅ for 80% data, 1≤N≤100.

⋅ for 100% data, 1≤N≤1000.

⋅ the string only contains lowercase letters.

输出

For every test case, you should output "Case #x: y", where x indicates the case number and counts from 1 and y is the result. If there are no mice in same city, output −1 instead.

样例输入

2
abcecba
abc

样例输出

Case #1: 2
Case #2: -1


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <iostream>
using namespace std;
const int maxn = 1005;
int note[26],DISTANCE[26];
int main(){
    int t,i,j;
    scanf("%d",&t);
    int rnd=1;
    while(t--){
        string in;
        cin>>in;
        for(i=0;i<26;i++){
            note[i]=-1;
            DISTANCE[i]=9999;
        }
        for(i=0;i<in.length();i++){
            if(note[in[i]-'a']==-1){
                note[in[i]-'a']=i;
            }else{
                DISTANCE[in[i]-'a'] = min(DISTANCE[in[i]-'a'],i-note[in[i]-'a']);
                note[in[i]-'a']=i;
            }
        }
        int flag = 0;
        for(i=0;i<26;i++){
            if(DISTANCE[i]==9999){
                flag++;
            }
        }
        //cout<<"falg "<<flag<<endl;
        if(flag==26){
            printf("Case #%d: -1\n",rnd);
        }else{
            int minn=99999;
            for(i=0;i<26;i++){
                minn =min(DISTANCE[i],minn);
            }
            printf("Case #%d: %d\n",rnd,minn);
        }
        rnd++;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值