pat 1050. String Subtraction (20)
2015-07-19 18:50
337 查看
1050. String Subtraction (20)
时间限制10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.
解:暴力卡时,后来听陈叔叔的建议,预处理了下s2,考虑这种情况,如s2=aaa,其实只是a而已,我们预处理为s2=a,可以在一定程度上降低时间复杂度。事实证明是可行的。
代码:
#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<set> using namespace std; int main() { char s1[10000+1]; char s2[10000+1]; string res; set<char> sss; gets(s1); gets(s2); int len1=strlen(s1); int len2=strlen(s2); for(int i=0;i<len2;i++) //预处理s2 sss.insert(s2[i]); set<char>::iterator p; for(int i=0;i<len1;i++) { int flag=1; for(p=sss.begin();p!=sss.end();p++) { if(s1[i]==*p) { flag=0; break; } } if(flag==1) res.push_back(s1[i]); } cout<<res<<endl; }
底下是改进的办法,用了set的内置函数find():
#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<set> using namespace std; int main() { char s1[10000+1]; char s2[10000+1]; string res; set<char> sss; gets(s1); gets(s2); int len1=strlen(s1); int len2=strlen(s2); for(int i=0;i<len2;i++) //预处理s2 sss.insert(s2[i]); for(int i=0;i<len1;i++) { int flag=1; if(sss.find(s1[i])!=sss.end()) { flag=0; } if(flag==1) res.push_back(s1[i]); } cout<<res<<endl; }
相关文章推荐
- HTTP返回状态码说明
- 如何用Python来进行查询和替换一个文本字符串
- css字体颜色动画
- Linux学习日志之Linux之命令基本格式及文件处理命令
- 自定义jquery ui日历
- secondarynamenode无法启动
- swift学习之常量和变量
- iOS开发中,关于什么时候使用点语法的解答
- BZOJ 1025 [SCOI2009]游戏
- CAS学习笔记(一)
- poj 1185 火炮 (减少国家DP)
- Intersection of Two Linked Lists
- word2010表格中的内容怎么设置行距
- ACM暑期计划——第四天
- 使用xml.etree.ElementTree遍历xml所有节点
- 《开源分享1》:前端开发必备《Html-CSS中文手册》
- java多线程之停止线程
- 查找---BF算法(1)
- POJ-1247
- mysql的日志管理