Friendship of Frog(水题)

本文深入探讨了编程领域中常见的复杂问题解决策略,涵盖了从理论基础到实际应用的全过程。通过实例分析,详细阐述了如何运用数据结构、算法及软件工程原则来优化代码,提高程序性能。此外,还讨论了现代开发工具和框架在解决复杂问题中的作用,旨在为开发者提供实用的解决方案和最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Friendship of Frog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 135    Accepted Submission(s): 106

Problem Description
N frogs from different countries are standing in a line. Each country is represented by a lowercase letter. The distance between adjacent frogs (e.g. the 1st and the 2nd frog, the N1th and the Nth frog, etc) are exactly 1. Two frogs are friends if they come from the same country. The closest friends are a pair of friends with the minimum distance. Help us find that distance.
 

 

Input
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 ith character of the string indicates the country of ith frogs. 1T50. for 80% data, 1N100. for 100% data, 1N1000. the string only contains lowercase letters.
 

 

Output
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 frogs in same country, output 1instead.
 

 

Sample Input
2 abcecba abc
 

 

Sample Output
Case #1: 2 Case #2: -1

题解:水。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
#define T_T while(T--)
#define SI(x) scanf("%d",&x)
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1010;
char s[MAXN];
int a[30];
int main(){
	int T,len,kase=0;
	SI(T);
	T_T{
		scanf("%s",s);
		len=strlen(s);
		mem(a,0);
		int ans=INF;
		for(int i=0;i<len;i++){
			if(!a[s[i]-'a'])a[s[i]-'a']=i;
			else ans=min(ans,i-a[s[i]-'a']),a[s[i]-'a']=i;
		}
		if(ans==INF)ans=-1;
		printf("Case #%d: %d\n",++kase,ans);
	}
	return 0;
}

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值