如何优化本程序,使得本程序的运行时间控制在1000毫秒以内?
查看原帖
如何优化本程序,使得本程序的运行时间控制在1000毫秒以内?
1213524
C_plus_plus_12345楼主2024/11/29 20:18
#include <iostream>
#include <string>
#include <algorithm>
 
using namespace std;

#pragma G++ optimize(3)
 
// 翻转二进制字符串,直接修改输入字符串
void flip(string& s)
{
    reverse(s.begin(), s.end());
}
 
// 取反二进制字符串,直接修改输入字符串
void bitNot(string& s)
{
    for (char& c : s)
    {
        c = c == '0' ? '1' : '0';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr); // nullptr其实就是0的一种体现。
 
    int n, s;
    string t, y;
 
    // 输入二进制字符串的长度 n 和操作次数 s
    cin >> n >> s;
    cin.ignore(); // 忽略换行符
 
    // 输入二进制字符串 t
    cin >> t;
 
    // 输入操作字符串 y
    cin >> y;
 
    // 对字符串 t 进行操作
    for (char op : y)
    {
        if (op == '1')
        {
            flip(t);
        }
        else if (op == '2')
        {
            bitNot(t);
        }
    }
 
    // 输出结果
    cout << t << endl;
 
    return 0;
}
2024/11/29 20:18
加载中...