Educational Codeforces Round 25 D Suitable Replacement 贪心
来源:互联网 发布:seo by yoast 编辑:程序博客网 时间:2024/06/02 04:29
CF传送门
题意:
1. 有s和t两个字符串,长度限制1e6
2. s中可能带有"?"字符,这个字符可以变换为任意字符
3. s串的任意两个相邻字符可以交换,并且这个交换次数没有限制(重点)
4. 要求在s串中尽量匹配出最多的t串,输出任意一种符合要求的s串
题解:
1. 纯属字眼题啊,看上面那个重点,两句话加起来等价于可以对s串进行随意重组的意思
2. 然后再通过变换"?"来使得s串尽可能地去匹配t串(贪心嘛,尽可能多)
3. 特殊情况是当s串中没有"?"时直接打印原s串即可,由于这个情况和我的带"?"操作并没有多大冲突和时间上的耗费,我就省略了这个判断
4. 正文从这里开始:
5. 首先是搜s串(代码中的s1串)中的"?"数量,以及对非普通字母字符进行数量统计存放在数组a中
6. 然后是循环遍历t串(代码中的s2串),如果s1串有对应字符就其数量减1,否则用"?"数量代替其减1,并且把当前字符压入栈
7. 遍历s串,把其中的"?"用栈中的字符一一对应替换(任意替换,反正s串可以随意重组)
以下是我的AC代码:
#include <iostream>#include <string>#include <stack>using namespace std;int a[30];stack<char> dit;int main(){ string s1,s2; cin >> s1 >> s2; int sum=0; for(unsigned int i=0;i<s1.size();i++){ if(s1[i]=='?') sum++; else a[int(s1[i]-'a')]++; } while(sum>0) for(unsigned int i=0;i<s2.size() && sum>0;i++){ if(a[int(s2[i]-'a')]) a[int(s2[i]-'a')]--; else {sum--;dit.push(s2[i]);} } for(unsigned int i=0;i<s1.size();i++) if(s1[i]=='?'){ s1[i]=dit.top(); dit.pop(); } cout << s1 << endl; return 0;}
阅读全文
0 0
- Educational Codeforces Round 25 D Suitable Replacement 贪心
- Educational Codeforces Round 25 D. Suitable Replacement
- Educational Codeforces Round 25 D. Suitable Replacement
- Educational Codeforces Round 25 D. Suitable Replacement【二分】
- Codeforces 825D Suitable Replacement【贪心】水题
- 【贪心】codeforces 825D Suitable Replacement
- codeforces 825D Suitable Replacement(字母代替?贪心)
- Codeforces 825 D Suitable Replacement
- Educational Codeforces Round 5 D Longest k-Good Segment 贪心
- 【Educational Codeforces Round 33】 D. Credit Card (贪心)
- Educational Codeforces Round 21-D
- Educational Codeforces Round 5 D
- Educational Codeforces Round 26 D
- Educational Codeforces Round 25
- Educational Codeforces Round 25
- Educational Codeforces Round 25
- Educational Codeforces Round 25
- Educational Codeforces Round 25
- ACM 资料
- java笔记之整数类型
- 高通音乐播放的音量调试和FM 音量调试
- 深度学习笔记01-概览
- AK-47 制造商 Kalashnikov 已成功研发 AI 武器 以 AK-47 闻名世界的俄罗斯军火商 Kalashnikov 近日宣布,其已成功研发全自动武器模块,能够利用人工智能技术识别目
- Educational Codeforces Round 25 D Suitable Replacement 贪心
- 快速排序
- java基础之运算符
- Myeclipse 的hadoop环境搭建
- POJ 3295 Tautology
- leetcode6
- java读取resource/通过文件名获取文件类型
- C++ map遍历
- Ubuntu中MySQL安装配置