6120 Problem D String Subtraction (20)

问题 D: String Subtraction (20)
时间限制: 1 Sec 内存限制: 32 MB
献花: 78 解决: 38
[献花][花圈][TK题库]
题目描述
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 <fstream>
#include <string>
using namespace std;

const int MaxN = 200;

int main()
{
#ifdef _DEBUG
    ifstream cin("data.txt");
#endif // _DEBUG

    string str, substr;
    while (getline(cin,str))
    {
        getline(cin, substr);
        bool flag[MaxN] = { false };
        for (int i = 0, size = substr.size(); i < size; ++i)
            flag[substr[i]] = true;
        for (int i = 0, size = str.size(); i < size; ++i)
            if (!flag[str[i]])
                printf("%c",str[i]);
        printf("\n");
    }

#ifdef _DEBUG
    cin.close();
    system("pause");
#endif // _DEBUG

    return 0;
}
/**************************************************************
    Problem: 6120
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:3 ms
    Memory:1704 kb
****************************************************************/
=== 小学算式OCR训练系统 === 字符集: 0123456789+-×÷=Xx 类别数: 18 加载数据集: math_problems 错误使用 tranorc11 (第 39 行) 数据集结构错误!\n正确结构示例:\nmath_problems/\n├── 2+3=5/\n│ ├── img1.png\n│ └── img2.png\n├── 10-4=6/\n└── ...\n每个子文件夹名称应是算式标签 这是现在的代码提示,下面我会给出原来的math_problems数据图片生成代码,请根据上面的要求,修改生成代码,从而提供符合要求的数据 % === 图像生成代码 === outputDir = 'math_problems'; if ~exist(outputDir, 'dir') mkdir(outputDir); end 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]; 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'); for imgIdx = 1:numImages filename = sprintf('%04d.png', imgIdx); dataTable.Filename(imgIdx) = string(filename); opType = randi(4); switch opType case 1 % 加法 a = randi([10, 90]); b = randi([1, 100-a]); result = a + b; eq = sprintf('%d + %d = ', a, b); operator = 'addition'; case 2 % 减法 a = randi([20, 100]); b = randi([1, a-1]); result = a - b; eq = sprintf('%d - %d = ', a, b); operator = 'subtraction'; case 3 % 乘法 a = randi([1, 10]); b = randi([1, 10]); result = a * b; eq = sprintf('%d × %d = ', a, b); operator = 'multiplication'; case 4 % 除法 divisor = randi([2, 10]); quotient = randi([1, 10]); 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 fullEq = sprintf('%s%d', eq, displayedResult); 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, ... 'FontWeight', 'normal', ... 'Color', textColor, ... 'HorizontalAlignment', 'center', ... 'VerticalAlignment', 'middle'); dataTable.Problem(imgIdx) = string(fullEq); dataTable.CorrectResult(imgIdx) = string(result); dataTable.Operator(imgIdx) = operator; dataTable.CorrectFlag(imgIdx) = isCorrect; % 按运算符分类保存图像 opDir = fullfile(outputDir, operator); if ~exist(opDir, 'dir') mkdir(opDir); end imwrite(getframe(fig).cdata, fullfile(opDir, filename)); end writetable(dataTable, fullfile(outputDir, 'math_problems.xlsx')); close(fig); fprintf('生成完成!\n');
06-19
%% === 修正版小学算式OCR数据集生成器 === function generateMathProblems() % 初始化设置 outputDir = 'math_problems'; if ~exist(outputDir, 'dir') mkdir(outputDir); end 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; maxAttempts = numImages * 3; % 最大尝试次数 existingLabels = containers.Map(); % 标签检测器 % === 关键修改:使用纯算式作为文件夹名称 === while imgIdx <= numImages && maxAttempts > 0 maxAttempts = maxAttempts - 1; % 生成两位数算式 opType = randi(4); switch opType case 1 % 加法 (10-99) a = randi([10, 99]); b = randi([10, 99]); result = a + b; eq = sprintf('%d+%d=', a, b); % 算式部分 operator = 'addition'; case 2 % 减法 (20-99) a = randi([20, 99]); b = randi([10, min(a-1, 99)]); % 确保结果为正 result = a - b; eq = sprintf('%d-%d=', a, b); operator = 'subtraction'; case 3 % 乘法 (2-20) a = randi([2, 20]); b = randi([2, 20]); result = a * b; eq = sprintf('%d×%d=', a, b); operator = 'multiplication'; case 4 % 除法 (整数结果) divisor = randi([2, 20]); quotient = randi([2, 20]); dividend = divisor * quotient; result = quotient; eq = sprintf('%d÷%d=', dividend, divisor); operator = 'division'; end % 生成合理错误答案 if rand() > 0.5 displayedResult = result; isCorrect = "1"; else % 在正确结果附近生成合理错误 errorRange = max(1, round(result*0.2)); % 误差范围 offset = randi([-errorRange, errorRange]); while offset == 0 offset = randi([-errorRange, errorRange]); end displayedResult = result + offset; isCorrect = "0"; end % === 关键修改1:使用纯算式作为文件夹名 === problemLabel = eq; % 仅算式部分 safeLabel = regexprep(problemLabel, '[<>:"/\\|?*]', ''); % 检查标签是否已存在 if isKey(existingLabels, safeLabel) continue; % 跳过重复标签 end existingLabels(safeLabel) = true; % === 关键修改2:图像显示完整算式+结果 === 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; % === 关键修改3:按算式标签创建文件夹 === labelDir = fullfile(outputDir, safeLabel); if ~exist(labelDir, 'dir') mkdir(labelDir); end % 保存图像 filename = sprintf('%04d.png', imgIdx); imwrite(imgData, fullfile(labelDir, filename)); % 更新数据表 dataTable.Filename(imgIdx) = string(filename); dataTable.Problem(imgIdx) = string(problemLabel); % 存储纯算式 dataTable.CorrectResult(imgIdx) = string(result); dataTable.Operator(imgIdx) = operator; dataTable.CorrectFlag(imgIdx) = isCorrect; imgIdx = imgIdx + 1; end % 检查完成度 if imgIdx <= numImages fprintf('生成不足: %d/%d, 正在补充...\n', imgIdx-1, numImages); dataTable = dataTable(1:imgIdx-1, :); % 调整表格大小 % 补充生成缺失的图像 for i = imgIdx:numImages % 生成唯一算式标签 while true a = randi([10,99]); b = randi([10,99]); op = {'+', '-', '×', '÷'}; opChar = op{randi(4)}; problemLabel = sprintf('%d%s%d=', a, opChar, b); safeLabel = regexprep(problemLabel, '[<>:"/\\|?*]', ''); if ~isKey(existingLabels, safeLabel) existingLabels(safeLabel) = true; break; end end % 生成随机结果 displayedResult = randi([10,200]); fullEq = sprintf('%s%d', problemLabel, displayedResult); % 生成图像 set(fig, 'Position', [100, 100, 600, 300]); % 固定尺寸 cla(ax); text(ax, 0.5, 0.5, fullEq, ... 'FontSize', fontSize, ... 'HorizontalAlignment', 'center', ... 'VerticalAlignment', 'middle'); imgData = getframe(fig).cdata; % 创建文件夹并保存 labelDir = fullfile(outputDir, safeLabel); if ~exist(labelDir, 'dir') mkdir(labelDir); end filename = sprintf('%04d.png', i); imwrite(imgData, fullfile(labelDir, filename)); % 更新表格 newRow = {filename, problemLabel, '0', 'addition', '0'}; % 占位数据 dataTable = [dataTable; newRow]; end end % 保存元数据 writetable(dataTable, fullfile(outputDir, 'math_problems_metadata.csv')); % 关闭图像窗口 close(fig); % 验证生成结果 folders = dir(outputDir); folderCount = sum([folders.isdir]) - 2; % 排除.和.. fprintf('最终生成文件夹数量: %d\n', folderCount); if folderCount < numImages fprintf('警告: 有重复标签未被检测到\n'); end fprintf('数据集生成完成! 元数据保存为 math_problems_metadata.csv\n'); end 这是原来的数据集图片生成代码,请根据刚才的数据集要求进行修改
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值