习题日常第十四练

1乌龟跑步(题目链接

这个题是一个记忆化搜索的题,只要找清楚每次是否变换以及方向即可。用一个四维数组来记录是否走过。具体代码如下。

#include<cmath>
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<cstring>
#include<math.h>
#include<stack>
#include<algorithm>
#include<queue>
#include<bitset>
#include<sstream>
#define ll long long int
const int mod=998244353;
using namespace std;
ll n,m,dp[105][55][210][2],ans=-1;
string s;
void dfs(ll step,ll se,ll w,ll p)
{
    if(se<0)
        return;
    if(step==s.length())
    {
        if(se%2==0)
        {
            ans=max(ans,abs(w));
        }
        return;
    }
     ll flat;
    if(p==1)
        flat=1;
    else
        flat=0;
    if(dp[step][se][w+100][flat])
        return ;
    dp[step][se][w+100][flat]=1;
    if(s[step]=='T')
    {
        dfs(step+1,se-1,w+p,p);
        dfs(step+1,se,w,-p);
    }
    else
    {
        dfs(step+1,se-1,w,-p);
        dfs(step+1,se,w+p,p);
    }

}
int main()
{
    ios::sync_with_stdio(false);
    ll i,j,l,p,k;
    cin>>s>>n;
    memset(dp,0,sizeof(dp));
    dfs(0,n,0,1);
    cout<<ans;
  return 0;
}

2虚虚实实(题目链接

这个题用到了上学期离散数学学的知识,也算是书本和实践的一个简单结合吧。用并查集判断图是否联通,再看节点度数。若只有俩个或零个奇数度节点,且图联通则符合条件。代码如下。

#include<cmath>
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<cstring>
#include<math.h>
#include<stack>
#include<algorithm>
#include<queue>
#include<bitset>
#include<sstream>
#define ll long long int
const int mod=998244353;
using namespace std;
ll n,m,a[1010],b[1010];
string s;
ll fin(ll x)
{
    if(a[x]==x)
        return x;
    else
        return a[x]=fin(a[x]);
}
void add(ll x,ll y)
{
    ll fx=fin(x),fy=fin(y);
    if(fx!=fy)
    {
        a[fx]=fy;
    }
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    ll i,j,l,p,k,t,n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(i=1;i<=n;i++)
            a[i]=i;
        memset(b,0,sizeof(b));
        for(i=0;i<m;i++)
        {
           cin>>p>>k;
           if(p!=k)
           {
           b[p]++;
           b[k]++;
           }
           add(p,k);
        }
        for(i=1;i<=n;i++)
           k=fin(i);
        k=0,p=1;
        a[0]=a[1];
        for(i=1;i<=n;i++)
        {
            if(b[i]%2==1)
                k++;
            if(a[i]!=a[i-1])
                p++;
        }
        if((k==2||k==0)&&p==1)
            cout<<"Zhen"<<endl;
        else
            cout<<"Xun"<<endl;
    }
  return 0;
}

3捡石头(题目链接

这个题是个比较水的博弈题,m>=n的时候第一个人直接取完。m<n的时候,就看谁能在自己最后第二次出手的时候将石子数目拿成m+1,但是最终我们发现只要n不为m+1的倍数,无论如何,第一个人都能将石子率先拿成m+1。
核心代码如下。

  if(m>=n)
        cout<<"first";
    else
    {
        if(n%(m+1)==0)
        {
            cout<<"second";
        }
        else
            cout<<"first";
    }

4方块I(题目链接

这个题要对异或非常的了解。首先,有以下三个等式。
0 ^1 ^3=2
0 ^2 ^3=1
1 ^2 ^3=0
我们可以用0,1,2代表三种颜色。将所有颜色代表数字异或起来再异或n-1个3,如果是3的话最后剩下2个,不是的话就是一个。需要特判一下颜色全部相同的情况。代码如下。

#include<cmath>
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<cstring>
#include<math.h>
#include<stack>
#include<algorithm>
#include<queue>
#include<bitset>
#include<sstream>
#define ll long long int
const int mod=998244353;
using namespace std;
ll n,m,a[1010],b[1010];
string s;
int main()
{
    ios::sync_with_stdio(false);
    ll i,j,l,p,k,t,n=0,m=0;
    while(cin>>s)
    {
        m=0;
        n=0;
    char c=s[0];
    for(i=0;i<s.length();i++)
    {
        t=s[i]-'a';
        m^=t;
        if(s[i]==s[0])
            n++;
    }
    for(i=0;i<s.length()-1;i++)
        m^=3;
    if(n==s.length())
        cout<<s.length()<<endl;
    else
    {
        if(m==3)
            cout<<2<<endl;
        else
            cout<<1<<endl;
    }
    }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值