Friendship of Frog(暴力+剪枝)

Friendship of Frog

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


 

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 2ndfrog, the N−1th 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.

⋅ 1≤T≤50.

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

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

⋅ 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
 

Source
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ull unsigned long long
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//fastPow
int fastPow(int x,int n){
    if(n==1) return x;
    int tmp=fastPow(x,n/2); //fenzi
    if(n%2==1) return tmp*tmp*x;
    else return tmp*tmp;
}
ll gcd(ll m, ll n){return n == 0 ? m : gcd(n, m%n);}
ll lcm(ll m, ll n){return m*n / gcd(m, n);}
ll pows(ll base, ll power,ll mod){ll result=1;while(power>0){if(power&1){result=result*base%mod;}power>>=1;base=(base*base)%mod;}return result;}
 
ll poww(ll base, ll power){ll result=1;while(power>0){if(power&1){result=result*base;}power>>=1;base=(base*base);}return result;}


//dijsktra
//start
//const int MAX= 10020;
//struct node {
//    int x,to,next,w;
//    bool operator <(const node &a) const{
//        return this->w>a.w;
//    }
//}G[MAX<<1];
//int cnt;
//int dis[MAX];
//int vis[MAX];
//int head[MAX];
//
//
//int m,n;
//int b[10020];
//
//
//void add(int u,int v,int w){
//    G[++cnt].to=v;
//    G[cnt].w=w;
//    G[cnt].next=head[u];
//    head[u]=cnt;
//}
//int  dijkstra(int s){
//    mms(dis,INF);
//    dis[s]=0;
//    priority_queue<node> p;
//    node t;
//    t.x=s;
//    t.w=0;
//    p.push(t);
//    while (!p.empty()) {
//        node u=p.top();
//        p.pop();
//        int v=u.x;
//        if(dis[v]!=u.w) continue;
//        for(int i=head[v];i;i=G[i].next){
//            int to=G[i].to;
//            if(dis[to]>dis[v]+G[i].w){
//                dis[to]=dis[v]+G[i].w;
//                t.x=to;
//                t.w=dis[to];
//                p.push(t);
//            }
//
//        }
//    }
//    int m1=-1;
//        int m2=-1;
//        dis[s]=-3;
//        for(int i=1;i<=n;i++){
//            if(dis[i]==INF) continue;
//            if(dis[i]>m1){
//                m2=m1;
//                m1=dis[i];
//            }
//            else if(dis[i]>m2){
//                m2=dis[i];
//            }
//        }
//        if(m1!=-1&&m2!=-1) return m1+m2;
//        return -1;
//
//}
//
//
//int main(){
//    int t;
//    cin>>t;
//    while (t--) {
//        cin>>n>>m;
//        int u,v,w;
//
//        for(int i=1;i<=m;i++){
//            cin>>u>>v>>w;
//            add(u,v,w);
//            add(v,u,w);
//        }
//        int ans=0;
//        int cnt=0;
//        for(int i=1;i<=n;i++){
//            ans=max(ans,dijkstra(i));
//        }
//        cout<<ans<<endl;
//    }
//}
//end


//e_cheak
//int vis[1000010];
//int prime[100010];
//int  e_cheak(int n){   //k为2-n素数的个数
//    for(int i=0;i<=n;i++){
//        vis[i]=0;
//    }
//    for(int i=2;i*i<=n;i++){
//        if(!vis[i]){
//            for(int j=i*i;j<=n;j+=i){
//                vis[j]=1;
//            }
//        }
//    }
//    int k=0;
//    for(int i=2;i<=n;i++){
//        if(!vis[i])
//            prime[k++]=i; //统计素数
//    }
//    return k;
//}
//
进制转化 x转化为y进制
//string work(int x,int y){
//    string str="";
//    while(x){
//        if(x%y>=10) str+=x%y+'A'-10;
//        else str+=x%y+'0';
//        x/=y;
//    }
//    reverse(str.begin(),str.end());
//    return str;
//}
//
//
//const int MAX=1000005;
//char str[MAX];
//char pattern[MAX];
//int cnt;
//int Next[MAX];
//void  getnext(string p,int plen){
//    Next[0]=0;
//    Next[1]=0;
//    for(int i=1;i<plen;i++){
//        int j=Next[i];
//        while (j&&p[i]!=p[j]) {
//            j=Next[j];
//        }
//        Next[i+1]=(p[i]==p[j])?j+1:0;
//    }
//}
//int kmp(string s,string p){
//    int last=-1;
//    int slen=s.length();
//    int plen=p.length();
//    getnext(p,plen);
//    int j=0;
//    for(int i=0;i<slen;i++){
//        while (j&&s[i]!=p[j]) {
//            j=Next[j];
//        }
//        if(s[i]==p[j]) j++;
//        if(j==plen){
//            //start
//            return 1;
//            //end
//        }
//    }
//    return 0;
//}
//int main(){
//    int n;
//    cin>>n;
//    string t;
//    string s="";
//    for(int i=2;i<=16;i++){
//        s="";
//        s+=work(n,i);
//        if(kmp(s,t))
//        {
//            cout<<"yes";
//            return 0;
//        }
//    }
//    cout<<"no"<<endl;
//    return 0;
//}


//string s1,s2;
//int main(){
//    cin>>s1;
//    s2="cocacola";
//    int ans;
//    for(int i=0;i<s1.length();i++){
//        if(s1[i]!=s2[i])
//            ans++;
//    }
//    cout<<ceil(ans/2);
//}


string s;
int t;
int main(){
    cin>>t;
    int num=1;
    while (t--) {
        cin>>s;
        int ans=INF;
        for(int i=0;i<s.size();i++){
            for(int j=1;j<=26&&i-j>=0;j++){ //出现相同字符最多要26位
                if(s[i]==s[i-j]){
                    ans=min(ans,j);
                    break;//已经找到最近的 没必要继续进行了
                }
            }
        }
        if(ans==INF) cout<<"Case #"<<num++<<": "<<-1<<endl;
        else cout<<"Case #"<<num++<<": "<<ans<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
目录 ㈠ 点的基本运算 1. 平面上两点之间距离 1 2. 判断两点是否重合 1 3. 矢量叉乘 1 4. 矢量点乘 2 5. 判断点是否在线段上 2 6. 求一点饶某点旋转后的坐标 2 7. 求矢量夹角 2 ㈡ 线段及直线的基本运算 1. 点与线段的关系 3 2. 求点到线段所在直线垂线的垂足 4 3. 点到线段的最近点 4 4. 点到线段所在直线的距离 4 5. 点到折线集的最近距离 4 6. 判断圆是否在多边形内 5 7. 求矢量夹角余弦 5 8. 求线段之间的夹角 5 9. 判断线段是否相交 6 10.判断线段是否相交但不交在端点处 6 11.求线段所在直线的方程 6 12.求直线的斜率 7 13.求直线的倾斜角 7 14.求点关于某直线的对称点 7 15.判断两条直线是否相交及求直线交点 7 16.判断线段是否相交,如果相交返回交点 7 ㈢ 多边形常用算法模块 1. 判断多边形是否简单多边形 8 2. 检查多边形顶点的凸凹性 9 3. 判断多边形是否凸多边形 9 4. 求多边形面积 9 5. 判断多边形顶点的排列方向,方法一 10 6. 判断多边形顶点的排列方向,方法二 10 7. 射线法判断点是否在多边形内 10 8. 判断点是否在凸多边形内 11 9. 寻找点集的graham算法 12 10.寻找点集凸包的卷包裹法 13 11.判断线段是否在多边形内 14 12.求简单多边形的重心 15 13.求凸多边形的重心 17 14.求肯定在给定多边形内的一个点 17 15.求从多边形外一点出发到该多边形的切线 18 16.判断多边形的核是否存在 19 ㈣ 圆的基本运算 1 .点是否在圆内 20 2 .求不共线的三点所确定的圆 21 ㈤ 矩形的基本运算 1.已知矩形三点坐标,求第4点坐标 22 ㈥ 常用算法的描述 22 ㈦ 补充 1.两圆关系: 24 2.判断圆是否在矩形内: 24 3.点到平面的距离: 25 4.点是否在直线同侧: 25 5.镜面反射线: 25 6.矩形包含: 26 7.两圆交点: 27 8.两圆公共面积: 28 9. 圆和直线关系: 29 10. 内切圆: 30 11. 求切点: 31 12. 线段的左右旋: 31 13.公式: 32
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值