开始做TopCoder,英语不好真是拙计啊,光是找到题库和知道怎么提交就用了很长时间。。然后随便找了道貌似简单的,读懂题意又用去很长时间,看来C++,python什么的都不重要,先学好英语才是关键啊。。
另外TopCoder的提交方式比较特别,跟一般OJ上的输入输出不太一样,还要用到class相关的东西,这又是一大坑,不过比英语坑好填一些,慢慢来吧。
先贴一题,虽然过了,还是没怎么搞懂这种submit方式。
还有TC每道题的分数是随着时间逐渐降低的,最低可降到30%,我这第一题虽然是水题,但研究返回string和使用class就花了好长时间,所以300point的题最后只拿98point。。丢人
300point : Problem Statement for BinaryCode
解
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class BinaryCode{
public:
vector<string> DeCode(string message){
vector<string> result;
result.push_back(decode('0',message));
result.push_back(decode('1',message));
return result;
}
private:
string decode(char first, string message){
string res;
int len = message.length();
if(len == 1){
if(message[0] == first)
res = message;
else
res = "NONE";
}
else{
for(int i = 0; i < len; i++){
if(i == 0){
res += first;
res += message[i]-res[i]+'0';
}
else if(i == len-1){
if(res[i]-'0'+res[i-1] != message[i])
res = "NONE";
}
else{
res += message[i]-res[i]+'0'-res[i-1]+'0';
}
if(res[i] != '0' && res[i] != '1'){
res = "NONE";
break;
}
}
}
return res;
}
};
550point:Problem Statement for Lottery
这道题主要是排列组合,难点在于对n个数可重复的m个数进行升序排列,公式是C(m,n+m-1)
代码还是比较简单的,不过时间又快用尽了,才拿到165point。。。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
struct Rule{
string name;
int choices, blanks;
char sorted, unique;
long long int odd;
bool operator < (const Rule &b) const{
if(odd != b.odd) return odd < b.odd;
return (name < b.name);
}
};
class Lottery{
public:
vector<string> sortByOdds(vector<string> rules){
vector<string> result;
Rule R[51]; int i = 0;
while(!rules.empty()){
string buf = rules.back();
rules.pop_back();
R[i++] = calOdd(buf);
}
sort(R,R+i);
for(int j = 0; j < i; j++)
result.push_back(R[j].name);
return result;
}
private:
long long int A(int n, int m){
long long int res = 1;
for(int i = 0; i < m; i++)
res *= n-i;
return res;
}
long long calC(int n, int m){
long long int C[110][9];
for(int i = 1;i <= n; i++){
for(int j = 0; j <= m; j++){
if(j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i-1][j-1]+C[i-1][j];
}
}
return C[n][m];
}
Rule process(string a){
Rule res;
int i = a.length()-1, buf = 0, mid = 1;
res.unique = a[i]; i--; i--;
res.sorted = a[i]; i--; i--;
while(a[i] != ' '){
buf += (a[i]-'0')*mid;
mid *= 10;
i--;
}
res.blanks = buf; buf = 0; i--; mid = 1;
while(a[i] != ' '){
buf += (a[i]-'0')*mid;
mid *= 10;
i--;
}
res.choices = buf; i--;
res.name = a.substr(0,i);
return res;
}
Rule calOdd(string a){
Rule buf = process(a);
int n = buf.choices, m = buf.blanks;
long long int res = 0;
if(buf.sorted == 'T' && buf.unique == 'T')
res = calC(n, m);
else if(buf.sorted == 'F' && buf.unique == 'T')
res = A(n, m);
else if(buf.sorted == 'F' && buf.unique == 'F')
res = pow(n, m);
else if(buf.sorted == 'T' && buf.unique == 'F')
res = calC(n+m-1,m);
buf.odd = res;
//printf("%d %d %c %c %lld",buf.choices,buf.blanks,buf.sorted,buf.unique,buf.odd);
cout << buf.name << endl;
return buf;
}
};