[Codeup 6120] D: String Subtraction 字符串含空格+哈希

本文详细解析了一道名为StringSubtraction的算法题,探讨了如何快速高效地从一个字符串中移除另一个字符串的所有字符,提供了完整的C++代码实现,包括读取输入、处理字符串和输出结果的过程。

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

http://codeup.cn/problem.php?cid=100000582&pid=3

问题 D: String Subtraction (20)

时间限制: 1 Sec  内存限制: 32 MB
提交: 241  解决: 118
[提交][状态][讨论版][命题人:外部导入]

题目描述

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 - S2for any given strings. However, it might not be that simple to do it fast.

输入

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.

输出

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

样例输入

They are students.
aeiou

样例输出

Thy r stdnts.

 


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int nmax=1e4+10;
char s1[nmax];
int hashtable[nmax];


int main(int argc, char** argv) {
    while(cin.getline(s1,nmax)){
    	memset(hashtable,0,sizeof(hashtable));
    	int len1=strlen(s1);
    	while(true){
    		char c=getchar();
    		if(c=='\n') break;
    		hashtable[c]=1;
		}
		for(int i=0;i<len1;i++){
			if(hashtable[s1[i]]==0){
			   cout<<s1[i]; 
			} 
		}
        
	}
	return 0;
}

 

outputDir = 'math_problems'; if ~exist(outputDir, 'dir') mkdir(outputDir); end % === 新增:内容哈希记录器 === contentHashMap = containers.Map('KeyType', 'char', 'ValueType', 'logical'); labelHashMap = containers.Map('KeyType', 'char', 'ValueType', 'logical'); numImages = 100; dataTable = table('Size', [numImages, 5], ... 'VariableTypes', {'string', 'string', 'string', 'string', 'string'}, ... 'VariableNames', {'Filename', 'Problem', 'CorrectResult', 'Operator', 'CorrectFlag'}); fontSize = 80; horizontalPadding = 50; verticalPadding = 80; bgColor = [1, 1, 1]; textColor = [0, 0, 0]; % === 新增:引入字体变异 === availableFonts = {'Arial', 'Times New Roman', 'Courier New', 'Comic Sans MS'}; fig = figure('Visible', 'off', 'Color', bgColor, ... 'Position', [100, 100, 800, 600], ... 'InvertHardcopy', 'off'); ax = axes('Parent', fig, 'Position', [0, 0, 1, 1], ... 'XLim', [0, 1], 'YLim', [0, 1], 'Visible', 'off'); hold(ax, 'on'); imgIdx = 1; attemptCount = 0; maxAttempts = numImages * 5; % 防止无限循环 while imgIdx <= numImages && attemptCount < maxAttempts attemptCount = attemptCount + 1; % 生成唯一文件名 filename = sprintf('%04d.png', imgIdx); % === 生成随机算式 === opType = randi(4); switch opType case 1 % 加法 a = randi([10, 999]); b = randi([1, 999]); result = a + b; eq = sprintf('%d+%d=', a, b); operator = 'addition'; case 2 % 减法 a = randi([20, 999]); b = randi([1, min(a-1, 999)]); result = a - b; eq = sprintf('%d-%d=', a, b); operator = 'subtraction'; case 3 % 乘法 a = randi([1, 99]); b = randi([1, 99]); result = a * b; eq = sprintf('%d×%d=', a, b); operator = 'multiplication'; case 4 % 除法 divisor = randi([2, 99]); quotient = randi([1, 99]); dividend = divisor * quotient; result = quotient; eq = sprintf('%d÷%d=', dividend, divisor); operator = 'division'; end % === 随机生成正确/错误答案 === if rand() > 0.5 displayedResult = result; isCorrect = "1"; else minError = max(1, round(result * 0.8)); maxError = round(result * 1.2); possibleErrors = setdiff(minError:maxError, result); if isempty(possibleErrors) possibleErrors = [result-1, result+1]; end errorIdx = randi(length(possibleErrors)); displayedResult = possibleErrors(errorIdx); isCorrect = "0"; end % === 创建标签文件夹 === labelFolder = sprintf('%s%d', eq, displayedResult); % 移除非法字符 safeLabel = regexprep(labelFolder, '[<>:"/\\|?*]', ''); % === 新增:标签唯一性检查 === if isKey(labelHashMap, safeLabel) % fprintf('标签重复: %s\n', safeLabel); continue; end % === 生成图像 === fullEq = sprintf('%s%d', eq, displayedResult); % === 新增:字体随机选择 === selectedFont = availableFonts{randi(length(availableFonts))}; textWidth = length(fullEq) * fontSize * 0.6; imgWidth = textWidth + 2*horizontalPadding; imgHeight = verticalPadding + fontSize + verticalPadding/2; set(fig, 'Position', [100, 100, imgWidth, imgHeight]); cla(ax); text(ax, 0.5, 0.5, fullEq, ... 'FontSize', fontSize, ... 'FontName', selectedFont,... % 使用随机字体 'FontWeight', 'normal', ... 'Color', textColor, ... 'HorizontalAlignment', 'center', ... 'VerticalAlignment', 'middle'); % 获取图像数据 imgData = getframe(fig).cdata; % === 新增:内容哈希检测 === imgHash = getImageHash(imgData); if isKey(contentHashMap, imgHash) % fprintf('内容重复: %s (哈希: %s)\n', fullEq, imgHash); continue; end % === 创建标签目录 === labelDir = fullfile(outputDir, safeLabel); if ~exist(labelDir, 'dir') mkdir(labelDir); end % 保存图像 imwrite(imgData, fullfile(labelDir, filename)); % 更新哈希记录 contentHashMap(imgHash) = true; labelHashMap(safeLabel) = true; % 更新数据表 dataTable.Filename(imgIdx) = string(filename); dataTable.Problem(imgIdx) = string(fullEq); dataTable.CorrectResult(imgIdx) = string(result); dataTable.Operator(imgIdx) = operator; dataTable.CorrectFlag(imgIdx) = isCorrect; imgIdx = imgIdx + 1; end % === 新增:实际生成数量处理 === if imgIdx <= numImages dataTable = dataTable(1:imgIdx-1, :); fprintf('实际生成图像: %d/%d (跳过重复项)\n', imgIdx-1, numImages); end writetable(dataTable, fullfile(outputDir, 'math_problems.xlsx')); close(fig); fprintf('生成完成! 创建了%d个子文件夹\n', height(dataTable)); % === 新增:图像哈希函数 === function hash = getImageHash(imgData) % 使用简化哈希提高性能 grayImg = rgb2gray(imgData); smallImg = imresize(grayImg, [16, 16]); % 缩小尺寸 hashVec = smallImg(:) > 128; % 二值化 hash = char(bin2dec(reshape(num2str(hashVec'), 8, [])' + '0')'); end 修改刚才出错的代码
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值