zoukankan      html  css  js  c++  java
  • 2020 10 02 个人赛

    B题 Yet Another Crosses Problem:

    思路:统计每一行每一列的‘.’的个数,然后输出行和列’.’的个数加起来最小的的和,特判行列交叉点,如果是‘.’还要再减去1.

    思路是对的,问题出在对数据的处理上。开始是只记录行中’.’最小的个数和列中’.’最小的个数,忽视了下面这种情况:

    * * .

    . * .

    * . *

    . * .

    应该把每一行每一列的‘.’的数据都记录下来,之后再遍历进行比较,取最小值。

    后来一直超时,原因是把对数组的定义放在了循环里面。

    代码如下:

    #include<bits/stdc++.h>

    using namespace std;

    #define ll long long

    int hang[50000+8],lie[50000+8];

    string s[50000+8];

    int main()

    {

           int q,n,m;

           int i,j,k;

           scanf("%d",&q);

           while(q--)

           {

                  scanf("%d%d",&n,&m);

                  for(i=0;i<n;i++)

                  {

                         cin>>s[i];

                         int sum=0;

                         for(j=0;j<m;j++)

                         {

                                if(s[i][j]=='.')

                                {

                                       sum++;

                                }

                         }

                         hang[i]=sum;

                  }

                  for(j=0;j<m;j++)

                  {

                         int sum=0;

                         for(i=0;i<n;i++)

                         {

                                if(s[i][j]=='.')

                                {

                                       sum++;

                                }

                         }

                         lie[j]=sum;

                  }

                  int mmin=4e5;

                  int sum=0;

                  for(i=0;i<n;i++)

                  {

                         for(j=0;j<m;j++)

                         {

                                if(s[i][j]=='.')

                                {

                                       sum=-1;

                                }

                                else

                                {

                                       sum=0;

                                }

                               

                                mmin=min((hang[i]+lie[j]+sum),mmin);

                         }

                  }

                  printf("%d ",mmin);

           }

           return 0;

    }

    C题:

    思路:先判断s是否为t的子串(不一定连续),如果不是,直接输出“no”,如果是,从t中删除s,然后在p中逐个寻找s,如果能全找到,就输出“yes”,否则就输出“no”。这题的思路很简单,难的就是敲代码时需要注意的各种细节。

    代码如下:

    #include<bits/stdc++.h>

    using namespace std;

    #define ll long long

    int main()

    {

           string s,t,p;

           int n,i,j,k;

           int q;

           cin>>q;

           int lens,lent,lenp;

           while(q--)

           {

                  cin>>s>>t>>p;

                  int flag=0;

                  int pri=t.size();

                  lens=s.size();

                  lent=t.size();

                  lenp=p.size();

                  //cout<<"t为:"<<t<<endl;

                  j=0;

                  for(i=0;i<lens;i++)

                  {

                         for(;j<lent;j++)

                         {

                                if(s[i]==t[j])

                                {

                                       t.erase(j,1);

                                       lent--;

                                      

                                       break;

                                }

                         }

                  }

                  //cout<<"删除s后t为:"<<t<<endl;

                  //cout<<"s为:"<<s<<endl;

                  if(lent==pri-lens)

                  {

                         flag=1;

                  }

                  if(flag==0)

                  {

                         cout<<"no"<<endl;

                         continue;

                  }

                  else

                  {

                         int flag1=1;

                         for(i=0;i<lent;i++)

                         {

                                int flag2=0;

                                for(j=0;j<lenp;j++)

                                {

                                       if(t[i]==p[j])

                                       {

                                              flag2=1;

                                              p.erase(j,1);

                                              j--;

                                              break;

                                       }

                                }

                                if(flag2==0)

                                {

                                       flag1=0;

                                       break;

                                }

                         }

                         if(flag1==1)

                         {

                                cout<<"yes"<<endl;

                         }

                         else

                         {

                                cout<<"no"<<endl;

                         }

                  }

           }

           return 0;

    }

    E题 Buying a TV Set:

    贼简单的一道题,比赛的时候没看,补题的时候却一直超时,后来才知道C++给我们提供了一种更快的求最大公约数的函数__gcd(),比我自己写的快多了。。。。(好像是辗转相除法,我用的是相减法)

    代码如下:

    #include<bits/stdc++.h>

    using namespace std;

    #define ll long long

    ll gcd(ll a,ll b)

    {

           return a==b?a:a>b?gcd(a-b,b):gcd(a,b-a);

    }

    int main()

    {

           ll x,y,a,b,c,d;

           cin>>a>>b>>c>>d;

           ll up=0,down=0;

           ll tem=__gcd(c,d);

           x=c/tem;

           y=d/tem;

           up=a/x;

           down=b/y;

          

           cout<<min(up,down)<<endl;

           return 0;

    }

  • 相关阅读:
    nginx中root和alias的区别
    linux修改服务时间
    nginx.conf属性
    mybatis批量操作
    linux查看日志关键字搜索
    项目启动报错Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.ImageFormatException
    springboot打包忽略Test
    mybatis文档
    On Java 8
    zabbix如何修改web字体
  • 原文地址:https://www.cnblogs.com/chengxvzhishen/p/13795375.html
Copyright © 2011-2022 走看看