HackerRank - string-reduction【反推】【规律】

本文针对HackerRank上的字符串缩减问题进行详细解析,该问题要求将由'a'、'b'、'c'组成的字符串通过特定规则转换,以达到最短长度。文章提出了解决方案,并通过分析字符数量的奇偶性来判断最终字符串的可能长度。

HackerRank - string-reduction【反推】

这里写图片描述

题意
给出一串 只有 字母 a, b, c 组成的字符串,然后没两个不同的字符碰到一起都可以变成另外一个字符,然后变到最后,求最短的字符串长度。

思路

①如果给出的字符串都是相同的字符,那么显然,答案就是这个字符串的长度

②如果不是 那么答案 可能是1 或者是 2

③我们可以反着推回去。就是假如 刚开始的状态的1
我们可以先假定 x = 字符a的数量 y = 字符b的数量 z = 字符c的数量

如果某个字符串 是由 1 推过去的
那么刚开始 x, y, z的数量就是 (1, 0, 0) || (0, 1, 0) || (0, 0, 1)
其奇偶状态就是 奇,偶,偶,
如果一个字符变成连个字符
其奇偶状态就变为 偶,奇,奇
如果继续变
其奇偶状态就变为 奇,偶,偶
所以 如果答案是1 那么 奇偶状态就是 两奇一偶 或者 一奇两偶
如果某个字符串是由 2 推过去的
刚开始的奇偶状态是 偶,偶,偶
如果减少一个字符,加另外两个字符
其奇偶状态就变成 奇,奇,奇

所以 我们可以得出,如果 一个字符串的奇偶状态是
全奇 或者 全偶
那么最后的答案 就是2
如果是其他情况
最后的答案 就是1

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;  
const double eps = 1e-6;
const int MAXN   = 0x3f3f3f3f;
const int MINN   = 0xc0c0c0c0;
const int maxn   = 1e5 + 5; 
const int MOD    = 1e9 + 7;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int n[3] = {0};
        int len = s.size();
        for (int i = 0; i < len; i++)
        {
            n[s[i] - 'a'] ++;
        }
        if (n[0] == len || n[1] == len || n[2] == len)
        {
            cout << len << endl;
        }
        else if((n[0] % 2 == 1 && n[1] % 2 == 1 && n[2] % 2 == 1) || (n[0] % 2 == 0 && n[1] % 2 == 0 && n[2] % 2 == 0))
            cout << 2 << endl;
        else 
            cout << 1 << endl;
    }
}
### WebRTC 音频处理中的噪声抑制(Noise Reduction)支持情况 WebRTC 的音频处理模块(`AudioProcessing`)提供了噪声抑制(NS)功能,支持多种采样率,包括 8000 Hz、16000 Hz、32000 Hz、44100 Hz 和 48000 Hz。如果输入音频的采样率不在这些范围内,噪声抑制模块将无法正常工作,从而导致“not supported”错误[^4]。 噪声抑制模块在 WebRTC 中默认是启用的,可以通过 `NoiseSuppression::Enable()` 方法进行控制。如果遇到不支持的情况,通常是因为音频格式(如采样率、声道数)与模块要求不匹配。例如,WebRTC 的 `AudioProcessing` 模块要求输入音频为 16-bit PCM 格式,并且声道数需为单声道或立体声[^2]。 ### 常见问题与解决方案 #### 1. 音频采样率不支持 WebRTC 的噪声抑制模块仅支持特定的采样率。如果输入音频的采样率不在支持范围内,需要进行重采样。例如,使用 `Resampler` 类进行重采样处理: ```cpp #include "common_audio/resampler/include/resampler.h" Resampler resampler; resampler.Init(sample_rate, target_sample_rate, num_channels); size_t out_length = resampler.Process(input, input_length, output, output_capacity); ``` #### 2. 音频格式不匹配 WebRTC 的 `AudioProcessing` 模块要求输入为 16-bit PCM 数据。如果输入音频为其他格式(如浮点型或 24-bit),需要进行格式转换。例如,使用 `S16ToFloat` 或 `FloatToS16` 进行转换: ```cpp void ConvertToFloat(const int16_t* input, float* output, size_t length) { for (size_t i = 0; i < length; ++i) { output[i] = static_cast<float>(input[i]) / 32768.0f; } } ``` #### 3. 多通道音频处理 WebRTC 的噪声抑制模块支持多通道音频,但需要在初始化时正确配置声道数。例如,使用 `StreamConfig` 设置双声道输入: ```cpp webrtc::StreamConfig config(sample_rate, 2, false); ``` 如果音频为多通道(如 5.1 声道),需要先进行下采样或混音处理,确保声道数与模块支持的格式一致[^2]。 ### 替代方案 如果 WebRTC 的噪声抑制模块无法满足需求,可以考虑以下替代方案: - **SpeexDSP**:提供噪声抑制、回声消除等功能,支持多种采样率和声道数。 - **RNNoise**:基于机器学习的噪声抑制库,适用于高质量语音处理。 - **SoX(Sound eXchange)**:命令行工具,支持多种音频处理操作,包括降噪、重采样等。 ### 示例代码:使用 WebRTC 进行噪声抑制处理 ```cpp #include "modules/audio_processing/include/audio_processing.h" #include <vector> #include <fstream> std::vector<int16_t> ReadPCMFile(const std::string& filename) { std::ifstream file(filename, std::ios::binary); file.seekg(0, std::ios::end); std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); std::vector<int16_t> samples(size / sizeof(int16_t)); file.read(reinterpret_cast<char*>(samples.data()), size); return samples; } void ApplyNoiseSuppression(int sample_rate, int num_channels, std::vector<int16_t>& samples) { rtc::scoped_refptr<webrtc::AudioProcessing> apm(webrtc::AudioProcessing::Create()); apm->noise_suppression()->Enable(true); apm->noise_suppression()->set_level(webrtc::NoiseSuppression::Level::kHigh); webrtc::StreamConfig config(sample_rate, num_channels, false); apm->Initialize(config, config); size_t frame_size = sample_rate / 100; for (size_t i = 0; i < samples.size(); i += frame_size) { size_t remaining = samples.size() - i; size_t process_size = std::min(frame_size, remaining); webrtc::AudioFrameView<int16_t> input_frame(&samples[i], process_size, num_channels); webrtc::AudioFrameView<int16_t> output_frame(&samples[i], process_size, num_channels); apm->ProcessStream(input_frame, config, config, output_frame); } } void WritePCMFile(const std::string& filename, const std::vector<int16_t>& samples) { std::ofstream file(filename, std::ios::binary); file.write(reinterpret_cast<const char*>(samples.data()), samples.size() * sizeof(int16_t)); } int main(int argc, char* argv[]) { if (argc != 4) { std::cerr << "Usage: " << argv[0] << " <input.pcm> <output.pcm> <sample_rate>" << std::endl; return -1; } std::string input_file = argv[1]; std::string output_file = argv[2]; int sample_rate = std::atoi(argv[3]); std::vector<int16_t> samples = ReadPCMFile(input_file); ApplyNoiseSuppression(sample_rate, 1, samples); WritePCMFile(output_file, samples); return 0; } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值