作业-5.19-401教室

· · 个人记录

第一题:D45420 智商测试

本题非常简单,是一道模拟枚举题,可以说没做出来可以回去学语法了。
思路:
暴力枚举转换每个地方的颜色,然后暴力判断2×2范围内的颜色是否相同。

#include<bits/stdc++.h> 
using namespace std;
//const int N=;
int n;
char a[15][15];
inline bool check(int x,int y)//inline可以不加,因为这一题并不需要卡常。
{
    x--,y--;
    if(x>n || y>n || x<1 || y<1)
    {
        return false;
    }
    if(a[x][y]==a[x][y+1] && a[x][y]==a[x+1][y] && a[x][y]==a[x+1][y+1])
    {
//      cout<<a[x][y]<<a[x][y+1]<<'\n'<<a[x+1][y]<<a[x+1][y+1]<<'\n'<<x<<' '<<y<<'\n';//测试代码,不用管。
        return true;
    }
    return false;
}
int main()//signed
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
    freopen("as01.in","r",stdin);
    freopen("as01.out","w",stdout);//考试时的文件定向读写
    n=4;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(a[i][j]=='#')
            {
                a[i][j]='.';
                if(check(i,j) || check(i+1,j) || check(i,j+1) || check(i+1,j+1) || check(i-1,j) || check(i,j-1) || check(i-1,j-1))
                {
                    cout<<"YES";
                    return 0;
                }
                a[i][j]='#';
            }
            else
            {
                a[i][j]='#';
                if(check(i,j) || check(i+1,j) || check(i,j+1) || check(i+1,j+1) || check(i-1,j) || check(i,j-1) || check(i-1,j-1))//暴力检查所有可能变化的区域。
                {
                    cout<<"YES";
                    return 0;
                }
                a[i][j]='.';
            }
        }
    }
    cout<<"NO";
    return 0;
}
/*



*/

这题标的普及-,我感觉应该是入门,洛谷的普及-就没有这么水。

第二题:D45423 简单的ABC题

本题仍然是简单的暴力,只需要枚举所有的点出发,dfs搜索出所有单词,最后取最小值。

#include<bits/stdc++.h>
using namespace std;
//const int N=;
int n=3;
char a[15][15];
string ans;
bool f[15][15];
int xl[8]={0,0,1,-1,1,-1,-1,1};
int yl[8]={1,-1,1,-1,-1,1,0,0};
void dfs(int x,int y,string s)
{
//  if(f[x][y])
//  {
//      return ;
//  }
    f[x][y]=true;//防止递归回初始点。
    if(s.size()==3)
    {
        if(ans>s || ans=="")//取min
        {
            ans=s;
        }
        return ;
    }
    for(int i=0;i<8;i++)//下一层递归
    {
        int nx=x+xl[i],ny=y+yl[i];
        if(nx<1 || nx>n || ny<1 || ny>n || f[nx][ny])//注意边界条件,我因为这个调代码调了19分钟。
        {
            continue;
        }
        f[nx][ny]=true;
        dfs(nx,ny,s+a[nx][ny]);
        f[nx][ny]=false;//回溯搜索大法
    }
}
int main()//signed
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
    freopen("as02.in","r",stdin);
    freopen("as02.out","w",stdout);//freopen
//  memset(a,0x3f,sizeof(a));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            memset(f,0,sizeof(f));
            dfs(i,j,"");//在a[i][j]开始dfs
        }
    }
    cout<<ans;
    return 0;
}
/*



*/

这题标的普及-,我感觉应该是入门,洛谷的普及-就没有这么水。

第三题:D45421 解压IPV6

这一题属于“普及+/提高”,我感觉没问题QWQ。
这一题并不是很难,思路很容易想,只是代码长,细节多。
也就是码农专属。

首先,我们先考虑测试点7~10:

这些测试点的特点是只需要补齐前导0。

我们先考虑将单个字符串转化为前导零补齐后的四位字符串。

我们设计一个函数ga(str)返回的是通过增加前导0,将str补齐前导零到四位得到的字符串。
那么,如果是本身就已经满足四位了,那么可以直接
return str
如果不满四位,那么用0补齐:

string ans="";
for(int i=1;i<=4-(int)s.size();i++)
{
    ans+="0";
}
ans=ans+s;
return ans;

然后,我们再考虑如何讲=将大块拆分成小块处理。

我们可以直接建立一个 string类型的变量str,用来存储变化前的字符串,然后用string类型变量ans存储答案。

得到了可以通过测试点7~10的代码:

但是你会发现你侥幸的过了测试点6
#include<bits/stdc++.h>
using namespace std;
//const int N=;
int q;
void work();
int main()//signed
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
//  freopen("as03.in","r",stdin);
//  freopen("as03.out","w",stdout);
    cin>>q;
    while(q--)
    {
        work();
    }
    return 0;
}
string s; 
string ga(string s)//得到四位字符串
{
    if(s.size()==4)
    {
        return s;
    }
    else
    {
        string ans="";
        for(int i=1;i<=4-(int)s.size();i++)
        {
            ans+="0";
        }
        ans=ans+s;
        return ans;
    }
}
void work()
{
    cin>>s;
    string str="",ans="";
//  cout<<"step1 ";
    for(int i=0;i<(int)s.size();i++)
    {
        if(s[i]==':')
        {
            ans+=":"+ga(str);//拼接
            str="";
        }
        else
        {
            str+=s[i];//将当前字符加入待处理字符串序列。
        }
    }
//  cout<<"step2 ";
    if(str!="")
    {
        ans+=":"+ga(str);
    }
//  cout<<"step3 ";
//  cout<<"step4 "; 
    for(int i=1;i<(int)ans.size();i++)//跳过第一位的:
    {
        cout<<ans[i];
    }
    cout<<'\n';
}
/*



*/

然后考虑正解

我们发现,前面的功能实现以后,后面就很简单了。
我们可以把ans分成ansl和ansr,表示左边的和右边的。
以什么为分隔呢?当然是::啦。
我们只需要在最后加上中间的:0000块即可。

最终答案:

#include<bits/stdc++.h>
using namespace std;
//const int N=;
int q;
void work();
int main()//signed
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
//  freopen("as03.in","r",stdin);
//  freopen("as03.out","w",stdout);
    cin>>q;
    while(q--)
    {
        work();
    }
    return 0;
}
string s; 
string ga(string s)
{
    if(s.size()==4)
    {
        return s;
    }
    else
    {
        string ans="";
        for(int i=1;i<=4-(int)s.size();i++)
        {
            ans+="0";
        }
        ans=ans+s;
        return ans;
    }
}
void work()
{
    cin>>s;
    string str="",ans="",ansl="",ansr="";
    bool f=false;
//  cout<<"step1 ";
    for(int i=0;i<(int)s.size();i++)
    {
        if(s[i+1]==s[i] && s[i]==':')
        {
            f=true; 
            i++;
            ansl+=":"+ga(str);
            str="";
            continue;
         } 
        if(s[i]==':')
        {
            if(f)
            {
                ansr+=":"+ga(str);
                str="";
                continue;
            }
            ansl+=":"+ga(str);
            str="";
        }
        else
        {
            str+=s[i];
        }
    }
//  cout<<"step2 ";
    if(str!="")
    {
        if(f)
        {
            ansr+=":"+ga(str);
        }
        else
        {
            ansl+=":"+ga(str);
        }
    }
    ans=ansl;
//  cout<<"step3 "; 
    for(int i=1;i<=(40-(int)ansl.size()-(int)ansr.size())/5;i++)//补全零块
    {
        ans+=":0000";
    }
    ans+=ansr;
//  cout<<"step4 "; 
    for(int i=1;i<(int)ans.size();i++)
    {
        cout<<ans[i];
    }
    cout<<'\n';
}
/*



*/

第四题:D45419 动态拆点图

这一题标签也是普及+/提高,我认为应该算上位绿,但是想清楚了就简单很多。
本题其实就是离线化+加点Floyd。

那么先讲加点Floyd:

加点Floyd顾名思义,是解决加点问题的最短路的。
那么先讲原理: 如果一个点变化了,那么如果长度变化了,那么一定会经过那一个点,所以我们可以减少一层循环。

加点Floyd模板(k是加入的点):

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
            dp[j][i]=min(dp[j][i],dp[j][k]+dp[k][i]);
        }
    }

讲完这个你就一定会想到离线化,反着解。

那么轻而易举地就得到了答案:

#include<bits/stdc++.h>
using namespace std;
//const int N=;
int n,dist[505][505],q,qu[505],dp[505][505],ans[505];
bool f[505];
inline void add(int id)
{
    f[id]=1;
    return ;
}
inline void floyd(int qid,int k)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);//dp转移方程
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(f[i] && f[j])//需要加入的
            {
                ans[qid]+=dp[i][j];
            }
        }
    }
    return ;
}
int main()//signed
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
//  freopen("as04.in","r",stdin);
//  freopen("as04.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>dp[i][j];//输入邻接矩阵
        }
    }
    for(int i=1;i<=n;i++)
    {
        cin>>qu[i];
    }
    for(int i=n;i>=1;i--)
    {
        add(qu[i]);//标记表示需要统计求和
        floyd(i,qu[i]);//加点Floyd
    }
    for(int i=1;i<=n;i++)
    {
        cout<<ans[i]<<' ';
    }
    return 0;
}
/*



*/

最后,介绍一道加点Floyd的好题:
P1119 灾后重建这一题只需要一次性加多条边就可以,也算是加点Floyd的模板了。