给定只包含0、1、?(0或1)的串s1,当i >= 2, s[i][j] = abs(s[i - 1][j] - s[i - 1][j + 1]), 有多少个s1,使得s[n] = “1“

题目

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6 + 5, mod = 1e9 + 7;
int a[maxn];
string s;
int qpow(int a, int b){
   int res = 1;
    while(b){
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
     return res;
}
void solve(){
    int cnt0 = 0, cnt1 = 0;
    int n = s.size();
    bool bit = 0;
    for(int i = 0; s[i]; i++){
//         if(s[i] == '?') cnt++;
        if(((n - 1) & (i)) == i){
            if(s[i] == '?') cnt1++;
            else{
                bit ^= (s[i] - '0');
            }
        }
        else{
            if(s[i] == '?') cnt0++;
        }
    }
    if(bit == 0 && cnt1 == 0){
        cout << 0 << '\n';
        return;
    }
    int res = qpow(2, cnt0 + max(0LL, cnt1 - 1));
    cout << res << '\n';
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
//     cin >> T;
    while(cin >> s){
        solve();
    }
    return 0;
}

  • 18
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,根据您的要求,我不会透露您的任何要求。以下是设计的CSTRING类: ```c++ #include <iostream> #include <cstring> using namespace std; class CSTRING { private: char *p; public: CSTRING() { p = new char[1]; p[0] = '\0'; } CSTRING(const char *s) { p = new char[strlen(s) + 1]; strcpy(p, s); } CSTRING(const CSTRING &s) { p = new char[strlen(s.p) + 1]; strcpy(p, s.p); } ~CSTRING() { delete[] p; } CSTRING &operator=(const CSTRING &s) { if (this != &s) { delete[] p; p = new char[strlen(s.p) + 1]; strcpy(p, s.p); } return *this; } bool operator>(const CSTRING &s) const { return strcmp(p, s.p) > 0; } friend istream &operator>>(istream &is, CSTRING &s) { char buf[10000]; is >> buf; delete[] s.p; s.p = new char[strlen(buf) + 1]; strcpy(s.p, buf); return is; } friend ostream &operator<<(ostream &os, const CSTRING &s) { os << s.p; return os; } friend CSTRING operator+(const CSTRING &s1, const CSTRING &s2) { CSTRING s; delete[] s.p; s.p = new char[strlen(s1.p) + strlen(s2.p) + 1]; strcpy(s.p, s1.p); strcat(s.p, s2.p); return s; } void sort(int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (p[j] > p[j + 1]) { char temp = p[j]; p[j] = p[j + 1]; p[j + 1] = temp; } } } } void concatenate(int n) { char *temp = new char[n * strlen(p) + n + 1]; temp[0] = '\0'; for (int i = 0; i < n; i++) { strcat(temp, "--"); strcat(temp, p + i * strlen(p)); } delete[] p; p = temp; } }; ``` 其中,重载的 `operator>` 用于比较两个字符的大小,重载的 `operator+` 用于拼接两个字符。 然后,根据给定的 `main` 函数,需要在类中添加以下成员函数: ```c++ class CSTRING { private: // ... public: // ... void sort(int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (p[j] > p[j + 1]) { char temp = p[j]; p[j] = p[j + 1]; p[j + 1] = temp; } } } } void concatenate(int n) { char *temp = new char[n * strlen(p) + n + 1]; temp[0] = '\0'; for (int i = 0; i < n; i++) { strcat(temp, "--"); strcat(temp, p + i * strlen(p)); } delete[] p; p = temp; } }; ``` 其中,`sort` 函数用于对字符数组进行冒泡排序,`concatenate` 函数用于将排序后的字符数组拼接成一个字符,并在每个字符前后加上两个连字符。 最后,修改 `main` 函数中的代码如下: ```c++ int main() { int n, i, j; while (cin >> n) { CSTRING *c = new CSTRING[n + 2]; for (i = 0; i < n; i++) { cin >> c[i]; } for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (c[j] > c[j + 1]) { CSTRING temp = c[j]; c[j] = c[j + 1]; c[j + 1] = temp; } } } c[n + 1] = c[0]; c->concatenate(n); cout << *c << endl; delete[] c; } return 0; } ``` 在每次循环中,先读入字符数组,然后对其进行排序,将第一个字符复制到 `c[n+1]` 中,最后将排序后的字符数组拼接成一个字符输出,并释放内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__night_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值