zoukankan      html  css  js  c++  java
  • hdu-5578 Friendship of Frog(暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5578

    Friendship of Frog

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


    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 1 instead.
     
    Sample Input
    2 abcecba abc
     
    Sample Output
    Case #1: 2
    Case #2: -1
    AC代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    char str[1009];
    int main()
    {
        int t;
        scanf("%d",&t);
        int cnt=1;
        while(t--)
        {
            scanf("%s",str);
            int len=strlen(str);
            int ans=20000,flag=0;
            for(int i=0;i<len;i++)
            {
                for(int j=i+1;j<len;j++)
                {
                    if(str[j]==str[i])
                    {
                        ans=min(ans,j-i);
                        flag=1;
                        break;
                    }
                }
            }
            if(!flag)ans=-1;
            printf("Case #%d: %d
    ",cnt++,ans);
        }
        return 0;
    }
  • 相关阅读:
    java List转换为字符串并加入分隔符的一些方法总结
    jackson 实体转json 为NULL或者为空不参加序列化
    马云告别演讲
    Linux chmod命令
    Linux执行shell脚本的方法
    2019第36周日
    2019第36周六
    eclipse中的maven插件
    SpringBoot要点之使用Actuator监控
    eclipse隐藏的列编辑
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5260119.html
Copyright © 2011-2022 走看看