hdu5578_Friendship of Frog_思维

来源:互联网 发布:知道mac地址能干什么 编辑:程序博客网 时间:2024/05/21 03:54

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


题目大意:让你找出两个相同字母间的最小距离。


解题思路:

对字母组进行标记,出现过的字母标记下,并记下位置,再次出现的时候计算一下与上一次距离,然后更新最小距离就行

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>using namespace std;const int inf=0x3f3f3f3f;int main(){    int t;    int a[30],d[30],new1[30];    char str[1005];    scanf("%d",&t);    int cas=0;    while(t--)    {        int mnn=inf;        memset(a,0,sizeof(a));        memset(d,0,sizeof(d));        memset(new1,0,sizeof(new1));        scanf("%s",str);        int len= strlen(str);        for(int i=0;i<len;i++)        {            int tmp=str[i]-'a';            if(!a[tmp])            {                a[tmp]=1;            }            else            {                d[tmp]=i-new1[tmp];                mnn=min(mnn,d[tmp]);            }            new1[tmp]=i;        }        printf("Case #%d: ",++cas);        if(mnn!=inf)        printf("%d\n",mnn);        else        printf("-1\n");    }    return 0;}


原创粉丝点击