zoukankan      html  css  js  c++  java
  • Friendship of Frog(水题)

    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
    ",++kase,ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Python学习系列之类与对象(二十三)
    面向过程和面向对象的异同点
    js 数值精确运算使用math.js
    js实现复制 、剪切功能-clipboard.min.js 示例
    css div嵌套层中button的margin-top不起作用解决方法
    IPhone中H5页面用on绑定click无效的解决方法
    This is a good start.
    element之input输入搜索联想框
    vue + element-ui 国际化实现
    async/await 处理多个网络请求同步问题
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5012878.html
Copyright © 2011-2022 走看看