TopCoder SRM 281
来源:互联网 发布:珠海精实测控 知乎 编辑:程序博客网 时间:2024/04/29 18:16
SRM 281 - Problem Set & Analysis by lovro can be see here.
The following is my solution, all programs are pass system test.
The Problems
IntegerGenerator
This following is my algorithm :
-> decide if string current is "valid input", and put it into a vector<int> cur
-> sort allowed
-> for i = cur.end() to cur.begin() i--
find element E in allowed which is the smallest number larger than cur[i] and let cur[i] = E
let elements after pos i in cur = allow[0] (smaillest number in allowed)
-> if the cur is the largest number in allowed which has length cur.lenght() then
let return number ret.length = cur.length+1 and ret = smallest number with length cur.length+1
string IntegerGenerator::nextInteger(vector <int> allowed, string current)
{
vector<int> allow = allowed;
vector<int> cur;
int i,j,k;
int num;
if(current[0]=='0') return "INVALID INPUT";
for(i=0;i<current.size();i++){
num = current[i] - '0';
if(std::find(allow.begin(),allow.end(),num)!=allow.end()){
cur.push_back(num);
}
else{
return "INVALID INPUT";
}
}
std::sort(allow.begin(),allow.end());
bool find = false;
for(i=cur.size()-1;i>=0;i--){
find = false;
for(j=0;j<allow.size();j++){
if(allow[j]>cur[i]){
find = true;
cur[i] = allow[j];
for(k=i+1;k<cur.size();k++) cur[k] = allow[0];
break;
}
}
if(find) break;
}
char *result = new char[cur.size()+4];
memset(result,0,cur.size()+4);
string ret;
if(find){
for(i=0;i<cur.size();i++){
result[i] = cur[i]+'0';
}
ret = result;
return ret;
}
else{
if(allow[0]!=0){
for(i=0;i<cur.size()+1;i++){
result[i] = allow[0]+'0';
}
ret = result;
return ret;
}else{
result[0] = allow[1]+'0';
for(i=1;i<cur.size()+1;i++){
result[i] = '0';
}
ret = result;
return ret;
}
}
}
BinarySearchable
If a number E in vector<int> sequence is binarysearchable, it must satisfy two rules:
-> all numbers in the left of E must < E
-> all numbers in the right of E must > E
int BinarySearchable::howMany(vector <int> sequence)
{
int i,j;
int k = 0;
bool left =true;
bool right = true;
for(i=0;i<sequence.size();i++){
left = true;
for(j=0;j<i;j++){
if(sequence[j]>sequence[i]){
left = false;
break;
}
}
right = true;
for(j=i+1;j<sequence.size();j++){
if(sequence[i]>sequence[j]){
right = false;
break;
}
}
if(left&&right) k++;
}
return k;
}
- TopCoder SRM 281
- 第一次参加Topcoder SRM
- topcoder SRM 503报告
- topcoder SRM 506
- topcoder SRM 513 DIV2
- Topcoder SRM 495 - 513
- TopCoder SRM 543 DIV2
- topcoder-srm-233-div2
- Topcoder SRM 566 countJourneys
- TopCoder SRM 473 题解
- Topcoder SRM 573 WolfPackDivTwo
- Topcoder SRM Div2 Level2
- topcoder-srm-594
- Topcoder SRM 596
- TopCoder SRM 607 题解
- TopCoder SRM 144 div2
- TOPCODER SRM 612 DIV2
- TOPCODER SRM 613 DIV2
- Java validation with dynamic proxies(IBM)
- 测试
- eclipse on server.
- STL,Loki和Boost中Functor的比较(-)
- Java 线程综合述
- TopCoder SRM 281
- SQL Server 2000中处理树形结构的三个用户自定义函数
- 重装系统后修复受损的FlashGet(测试可用)
- Why don't we use a constructor instead of factory method?
- PO部分常用table 总结 PO01A(采购单)sql 语句
- 有点懒
- 文件(文件夹)删除拷贝操作类
- 我收藏的文章
- TopCoder SRM285 -- My Solution