zoukankan      html  css  js  c++  java
  • D Mike and strings

    题目链接:https://vjudge.net/contest/162220#problem/D

    题解:

    1.构造两个函数(1-> 判断是否已经达到目的 2->计算达到目标字符串所需要多少步)//详见代码

    2.主函数利用双重for循环来找到最少所需要的步数

    AC code:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;

    char str[110][110];
    int n;

    bool is_ok(char sta[] , char test[],int len)
    {
    for(int i=0;i<len;i++)
    {
    if(sta[i]!=test[i])
    {
    return false;
    }
    }
    return true;
    }

    int step(int sta, int tes)
    {
    int cnt;
    int len;
    cnt=0;
    len=strlen(str[sta]);
    char test[2*len+1];
    for(int i=0;i<len;i++)
    {
    test[i]=str[tes][i];
    }
    for(int i=0;i<len;i++)
    {
    test[len+i]=str[tes][i];
    }
    test[2*len]='\0';
    while(cnt<len)
    {
    if(is_ok(str[sta],test+cnt,len))
    {
    break;
    }
    else
    {
    cnt++;
    }
    }
    if(cnt>=len) return -1;
    else return cnt;
    }

    int main()
    {
    int ans=99999999;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
    scanf("%s",str[i]);
    }
    for(int i=0;i<n;i++)
    {
    int cnt=0;
    for(int j=0;j<n;j++)
    {
    if(i!=j)
    {
    int temp=step(i,j);
    if(temp==-1)
    {
    printf("-1\n");
    return 0;
    }
    else
    {
    cnt+=temp;
    }
    }
    }
    if(ans>cnt)
    {
    ans=cnt;
    }
    }
    printf("%d\n",ans);
    return 0;
    }

  • 相关阅读:
    IOS6.0 应用内直接下载程序 不需跳转AppStore
    维护Product
    CRM Service summary 1
    "No update of sales order X from purchase order (error V1 154)."
    Organizational Management
    懂PMP的好处
    Buying Center
    更新
    Business Partner定义,以及与Account的区分
    Fact Sheet
  • 原文地址:https://www.cnblogs.com/DemonZiv/p/6818865.html
Copyright © 2011-2022 走看看