Binary Strings Gym - 101845B

https://cn.vjudge.net/problem/Gym-101845B 

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <stack>
//#include <tr1/unordered_map>
//#include <unordered_map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
//#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define prl(i) printf("%I64d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define inf 1e18
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

template<typename T>inline void read(T &x)
{
    x=0;
    static int p;p=1;
    static char c;c=getchar();
    while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
    while(isdigit(c)) {x=(x<<1)+(x<<3)+(c-48);c=getchar();}
    x*=p;
}

//-----------------------------------------------
const int maxn=5e5+9;
const int mod=1e9+7;

void ch(char &c,char &cc)
{
    if(c=='1') c='0';
    else c='1';

    if(cc=='1') cc='0';
    else cc='1';
}

int main()
{
    FAST_IO;

    //freopen("input.txt","r",stdin);

    string s,t,tmp;
    cin>>s>>t;
    int len=s.length();

    int minn=INF;

    for(int i=0;i<len;i++)
    {
        //fl();
        tmp="";
        int st=i;
        for(int j=0;j<len;j++)
        {
            tmp+=s[st++];
            st%=len;
        }
        string tmp2=tmp;
        //cout<<tmp<<endl;

        int ans=0;
        for(int j=0;j<len-1;j++)
        {
            if(t[j]!=tmp[j])
            {
                ans++;
                ch(tmp[j],tmp[j+1]);
            }
        }
        //cout<<tmp<<endl;;
        if(t[len-1]!=tmp[len-1])
        {
            ans=INF;
        }
        minn=min(minn,ans);

        tmp=tmp2;
        ans=0;
        ch(tmp[0],tmp[len-1]);
        ans++;
        for(int j=0;j<len-1;j++)
        {
            if(t[j]!=tmp[j])
            {
                ans++;
                ch(tmp[j],tmp[j+1]);
            }
        }
        if(t[len-1]!=tmp[len-1])
        {
            ans=INF;
        }
        minn=min(minn,ans);

    }
    if(minn==INF) minn=-1;
    cout<<minn<<endl;


    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用状态机编写的Binary-to-Gray代码(VHDL): ``` library ieee; use ieee.std_logic_1164.all; entity binary_to_gray is port ( binary_in : in std_logic_vector(3 downto 0); gray_out : out std_logic_vector(3 downto 0) ); end binary_to_gray; architecture Behavioral of binary_to_gray is type state_type is (s0, s1, s2, s3); signal current_state, next_state : state_type; begin process(current_state, binary_in) begin case current_state is when s0 => gray_out <= binary_in; next_state <= s1; when s1 => gray_out <= binary_in xor (binary_in(1) & binary_in(0) & binary_in(3)); next_state <= s2; when s2 => gray_out <= binary_in xor (binary_in(2) & binary_in(1) & binary_in(0)); next_state <= s3; when s3 => gray_out <= binary_in xor (binary_in(3) & binary_in(2) & binary_in(1)); next_state <= s0; end case; end process; process(current_state) begin case current_state is when s0 => current_state <= s1; when s1 => current_state <= s2; when s2 => current_state <= s3; when s3 => current_state <= s0; end case; end process; next_state <= s0 when current_state = s3 else next_state; process(current_state, next_state) begin if current_state /= next_state then current_state <= next_state; end if; end process; end Behavioral; ``` 这个代码使用了一个状态机来实现Binary-to-Gray的转换。在每个时钟周期中,状态机会根据当前状态和输入的Binary值计算出输出的Gray值,并将状态更新为下一个状态。最终的Gray值将在下一个时钟周期中输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值