PAT 1050 String Subtraction

本文介绍了一种计算两个字符串差集的高效算法,并提供了完整的C++实现代码。输入为两个字符串S1和S2,输出为从S1中去除所有S2中存在的字符后的剩余字符串。文章通过示例详细解释了算法的运作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 - S2 for any given strings. However, it might not be that simple to do it fast.

Input Specification:

Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.

Output Specification:

For each test case, print S1 - S2 in one line.

Sample Input:
They are students.
aeiou
Sample Output:
thy r stdnts

This is easy.

#include<iostream>
using namespace std;

int main()
{
	char ch[10001];
	bool mark[128]={0};
	int len=0;
	while(ch[len++]=cin.get(),ch[len-1]!='\n');
	char chtmp;
	while(chtmp=cin.get(),chtmp!='\n')
	{
		mark[chtmp]=1;
	}
	for(int i=0;i<len;++i)
	{
		if(mark[ch[i]]) continue;
		else cout<<ch[i];
	}
}




### 频谱减法技术原理 频谱减法是一种用于去除加性噪声的有效方法,特别适用于语音增强领域。该算法基于假设:干净信号的短时傅里叶变换(STFT)系数与噪声的STFT系数相互独立[^1]。 对于给定的一帧带噪语音 \( X(k,n) \),其中 \( k \) 表示频率索引而 \( n \) 是时间帧编号,则可以表示为: \[ X(k, n) = S(k, n) + N(k, n) \] 这里 \( S(k, n) \) 和 \( N(k, n) \) 分别代表纯净语音和背景噪声对应的复数域表达形式。为了估计原始无干扰的声音部分,在理想情况下可以直接通过简单的相减操作来获得近似解: \[ Y(k, n) = |X(k, n)| - G|N(k, n)| \] 然而实际应用中由于存在不确定性以及可能存在的非平稳特性等因素影响,通常会引入增益因子 \( G \) 来调整补偿程度并防止过抑制现象的发生。 ```python import numpy as np def spectral_subtraction(noisy_signal, noise_frame, alpha=0.98): """ Perform basic spectral subtraction on noisy signal. Parameters: noisy_signal (array): Input array containing the noisy speech frames. noise_frame (array): Array representing an estimate of the noise spectrum. alpha (float): Smoothing factor for power spectra estimation. Returns: clean_estimated (array): Estimated cleaned-up audio data after applying SS technique. """ # Compute STFTs stft_noisy = np.fft.rfft(noisy_signal) stft_noise = np.fft.rfft(noise_frame) magnitude_spectra_ratio = abs(stft_noisy)**2 / (abs(stft_noise)**2 + 1e-10) smoothed_magnitude_spectrum = [] prev_mag_spec = None for mag_spec in magnitude_spectra_ratio.T: if prev_mag_spec is not None: current_smoothed = alpha * prev_mag_spec + (1-alpha)*mag_spec else: current_smoothed = mag_spec smoothed_magnitude_spectrum.append(current_smoothed) prev_mag_spec = current_smoothed.copy() enhanced_stft = stft_noisy * ((smoothed_magnitude_spectrum).T ** .5) clean_estimated = np.real(np.fft.irfft(enhanced_stft)) return clean_estimated ``` 此Python函数实现了基本版本的频谱减法过程,它接收含有噪音的话语片段及其对应静音期间获取到的一个典型噪音样本作为输入参数,并返回经过处理后的较为清晰的话音序列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值