1050 String Subtraction (20 分)哈希 map

本文详细解析了一种名为字符串减法的算法问题,该问题要求从一个字符串中移除另一个字符串的所有字符,展示了如何通过扫描两个字符串并使用哈希映射来高效地解决问题。文章提供了完整的代码实现,包括输入输出规范,以及一个示例输入和输出。
1050 String Subtraction (20 分)

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.
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<string.h>
#include<cstdio>
using namespace std;

map<char,int>mp;
char ch1[10005],ch2[10004];

int main()
{
    char ch;
    scanf("%c",&ch);
    int i,len1,len2;
    for(i=0; ch!='\n'; i++)
    {
        ch1[i]=ch;
        mp[ch]=1;
        scanf("%c",&ch);

    }
    len1=i;
    scanf("%c",&ch);
    for(i=0; ch!='\n'; i++)
    {
        ch2[i]=ch;
        mp[ch]=0;
        scanf("%c",&ch);
       // mp[ch]=1;

    }
    len2=i;
    for(int i=0; i<len1; i++)
    {
        if(mp[ch1[i]]==1)
            printf("%c",ch1[i]);
    }
    //cout<<len2<<len1;
    return 0;
}

 


转载于:https://www.cnblogs.com/zhanghaijie/p/10315963.html

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、付费专栏及课程。

余额充值