SRM 144 1AB2AB 2013.12.1

SRM 144 1AB2AB 2013.12.1

SRM 144 DIV 1

 

300 Problem Statement

    

Let's say you have a binary string such asthe following:

011100011

One way to encrypt this string is to add toeach digit the sum of its adjacent digits. For example, the above string wouldbecome:

123210122

In particular, if P is the original string,and Q is the encrypted string, then Q[i] = P[i-1] + P[i] + P[i+1] for all digitpositions i. Characters off the left and right edges of the string are treatedas zeroes.

An encrypted string given to you in thisformat can be decoded as follows (using 123210122 as an example):

Assume P[0] = 0.

Because Q[0] = P[0] + P[1] = 0 + P[1] = 1,we know that P[1] = 1.

Because Q[1] = P[0] + P[1] + P[2] = 0 + 1 +P[2] = 2, we know that P[2] = 1.

Because Q[2] = P[1] + P[2] + P[3] = 1 + 1 +P[3] = 3, we know that P[3] = 1.

Repeating these steps gives us P[4] = 0,P[5] = 0, P[6] = 0, P[7] = 1, and P[8] = 1.

We check our work by noting that Q[8] =P[7] + P[8] = 1 + 1 = 2. Since this equation works out, we are finished, and wehave recovered one possible original string.

Now we repeat the process, assuming theopposite about P[0]:

Assume P[0] = 1.

Because Q[0] = P[0] + P[1] = 1 + P[1] = 1,we know that P[1] = 0.

Because Q[1] = P[0] + P[1] + P[2] = 1 + 0 +P[2] = 2, we know that P[2] = 1.

Now note that Q[2] = P[1] + P[2] + P[3] = 0+ 1 + P[3] = 3, which leads us to the conclusion that P[3] = 2. However, thisviolates the fact that each character in the original string must be '0' or'1'. Therefore, there exists no such original string P where the first digit is'1'.

Note that this algorithm produces at mosttwo decodings for any given encrypted string. There can never be more than onepossible way to decode a string once the first binary digit is set.

Given a string message, containing theencrypted string, return a vector <string> with exactly two elements. Thefirst element should contain the decrypted string assuming the first characteris '0'; the second element should assume the first character is '1'. If one ofthe tests fails, return the string "NONE" in its place. For the aboveexample, you should return {"011100011", "NONE"}.

Definition

    

Class:

BinaryCode

Method:

decode

Parameters:

string

Returns:

vector <string>

Method signature:

vector <string> decode(stringmessage)

(be sure your method is public)

    

 

Constraints

-

message will contain between 1 and 50characters, inclusive.

-

Each character in message will be either'0', '1', '2', or '3'.

Examples

0)

 

    

"123210122"

Returns: { "011100011",  "NONE" }

The example from above.

1)

 

    

"11"

Returns: { "01",  "10" }

We know that one of the digits must be '1',and the other must be '0'. We return both cases.

2)

 

    

"22111"

Returns: { "NONE",  "11001" }

Since the first digit of the encryptedstring is '2', the first two digits of the original string must be '1'. Ourtest fails when we try to assume that P[0] = 0.

3)

 

    

"123210120"

Returns: { "NONE",  "NONE" }

This is the same as the first example, butthe rightmost digit has been changed to something inconsistent with the rest ofthe original string. No solutions are possible.

4)

 

    

"3"

Returns: { "NONE",  "NONE" }

 

5)

 

    

"12221112222221112221111111112221111"

Returns:

{ "01101001101101001101001001001101001",

 "10110010110110010110010010010110010" }

 

 

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

 

Code 95.00 points

#include <string>

#include <vector>

#include <iostream>

 

using namespace std;

 

class BinaryCode

{

public:

         vector<string>decode(string& q)

         {

                   vector<string>p;

                   p.push_back(decode(q,'0'));

                   p.push_back(decode(q,'1'));

 

                   returnp;

         }// finally, I wrote my first C++ class! Cheers!

private:

         stringdecode(string q,char p0)

         {

                   unsignedint n=q.length();

                   stringp(n,'0');

 

                   p[0]=p0;

                   p[1]=q[0]-p[0]+'0';

 

                   for(unsigned int i=2;i<n;++i)

                            p[i]=q[i-1]-p[i-1]-p[i-2]+2*'0';

 

 

                   if(p.find_first_not_of("01",0)!=string::npos)

                            return"NONE";// Check whether the p string onlyhave digit ‘0’ and ‘1’

 

                   if(q[n-1]!=(p[n-1]+p[n-2]-'0')) return "NONE";

 

                   returnp;

         }

};

 

550

Problem Statement

    

In most states, gamblers can choose from awide variety of different lottery games. The rules of a lottery are defined bytwo integers (choices and blanks) and two boolean variables (sorted and unique).choices represents the highest valid number that you may use on your lotteryticket. (All integers between 1 and choices, inclusive, are valid and canappear on your ticket.) blanks represents the number of spots on your ticketwhere numbers can be written.

The sorted and unique variables indicaterestrictions on the tickets you can create. If sorted is set to true, then thenumbers on your ticket must be written in non-descending order. If sorted isset to false, then the numbers may be written in any order. Likewise, if uniqueis set to true, then each number you write on your ticket must be distinct. Ifunique is set to false, then repeats are allowed.

Here are some example lottery tickets,where choices = 15 and blanks = 4:

{3, 7, 12, 14} -- this ticket isunconditionally valid.

{13, 4, 1, 9} -- because the numbers arenot in nondescending order, this ticket is valid only if sorted = false.

{8, 8, 8, 15} -- because there are repeatednumbers, this ticket is valid only if unique = false.

{11, 6, 2, 6} -- this ticket is valid onlyif sorted = false and unique = false.

Given a list of lotteries and theircorresponding rules, return a list of lottery names sorted by how easy they areto win. The probability that you will win a lottery is equal to (1 / (number ofvalid lottery tickets for that game)). The easiest lottery to win should appearat the front of the list. Ties should be broken alphabetically (see example 1).

Definition

    

Class:

Lottery

Method:

sortByOdds

Parameters:

vector <string>

Returns:

vector <string>

Method signature:

vector <string> sortByOdds(vector<string> rules)

(be sure your method is public)

    

 

Constraints

-

rules will contain between 0 and 50elements, inclusive.

-

Each element of rules will contain between11 and 50 characters, inclusive.

-

Each element of rules will be in the format"<NAME>:_<CHOICES>_<BLANKS>_<SORTED>_<UNIQUE>"(quotes for clarity). The underscore character represents exactly one space.The string will have no leading or trailing spaces.

-

<NAME> will contain between 1 and 40characters, inclusive, and will consist of only uppercase letters ('A'-'Z') andspaces (' '), with no leading or trailing spaces.

-

<CHOICES> will be an integer between10 and 100, inclusive, with no leading zeroes.

-

<BLANKS> will be an integer between 1and 8, inclusive, with no leading zeroes.

-

<SORTED> will be either 'T' (true) or'F' (false).

-

<UNIQUE> will be either 'T' (true) or'F' (false).

-

No two elements in rules will have the samename.

Examples

0)

 

    

{"PICK ANY TWO: 10 2 F F"

,"PICK TWO IN ORDER: 10 2 T F"

,"PICK TWO DIFFERENT: 10 2 F T"

,"PICK TWO LIMITED: 10 2 T T"}

Returns:

{ "PICK TWO LIMITED",

 "PICK TWO IN ORDER",

 "PICK TWO DIFFERENT",

 "PICK ANY TWO" }

The "PICK ANY TWO" game letseither blank be a number from 1 to 10. Therefore, there are 10 * 10 = 100possible tickets, and your odds of winning are 1/100.

The "PICK TWO IN ORDER" gamemeans that the first number cannot be greater than the second number. Thiseliminates 45 possible tickets, leaving us with 55 valid ones. The odds ofwinning are 1/55.

The "PICK TWO DIFFERENT" gameonly disallows tickets where the first and second numbers are the same. Thereare 10 such tickets, leaving the odds of winning at 1/90.

Finally, the "PICK TWO LIMITED"game disallows an additional 10 tickets from the 45 disallowed in "PICKTWO IN ORDER". The odds of winning this game are 1/45.

1)

 

    

{"INDIGO: 93 8 T F",

 "ORANGE: 29 8 F T",

 "VIOLET: 76 6 F F",

 "BLUE: 100 8 T T",

 "RED: 99 8 T T",

 "GREEN: 78 6 F T",

 "YELLOW: 75 6 F F"}

Returns: { "RED",  "ORANGE",  "YELLOW",  "GREEN",  "BLUE",  "INDIGO",  "VIOLET" }

Note that INDIGO and BLUE both have theexact same odds (1/186087894300). BLUE is listed first because it comes beforeINDIGO alphabetically.

2)

 

    

{}

Returns: { }

Empty case

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

 

看了别人的觉得可以借鉴的

(1)sort多关键字排序的两种写法

第一种在struct里面重载<号

第二种另开一个struct 重载operator()函数

(2)幂

(3)组合数

(4)简洁

Code 181.95

#include <string>

#include <iostream>

#include <vector>

#include <sstream>

#include <algorithm>

 

using namespace std;

 

class Lottery

{

         structnode

         {

                   stringname;

                   longlong possible;

         };

         structcmp

         {

                   booloperator()(const struct node& a,const struct node& b)

                   {

                            if(a.possible!=b.possible) return (a.possible<b.possible);

                            elsereturn (a.name<b.name);

                   }

         };

         longlong Cnm(int n,int m)

         {

                   longlong ans=0;

                   longlong top=1,bottom=1;

                   if(n-m<m) m=n-m;

                   for(inti=1,j=n;i<=m;i++,j--)

                   {

                            top= top*j;

                            bottom=bottom*i;

                   }       

                   ans=top/ bottom;

                   returnans;

 

         }

long long doFF(int c,int b)

{

         longlong ans=1;

         for(inti=1;i<=b;i++)

         {

                   ans= ans*c;

         }

         returnans;

}

 

long long doTF(int c,int b)

{

   return Cnm(c+b-1,b);

疑问在这里~已在iask提问

}

long long doFT(int c,int b)

{

         longlong ans=1;

         for(inti=1,j=c;i<=b;i++,j--)

                   ans*=j;

         returnans;

}

long long doTT(int c,int b)

{

         returnCnm(c,b);

}

public: vector <string>sortByOdds(vector <string> rules)

                   {

                            vector<node>lists;

                            vector<string> ans;

                            lists.clear();

                            ans.clear();

                            for(vector<string>::iteratori=rules.begin();i!=rules.end();i++)

                            {

                                     intpos = (*i).find(':',0);

                                     nodetemp;

                                     strings1,s2,s3,s4;

                                     stringstreamss;

                                     temp.name= (*i).substr(0,pos);

                                     ss<<(*i).substr(pos+2,(*i).size()-1-pos);

                                     ss>>s1>>s2>>s3>>s4;              

                                     intchoices=atoi(s1.c_str()),blank=atoi(s2.c_str());

                                     if((s3=="F")&&(s4=="F"))temp.possible=doFF(choices,blank);

                                     else

                                               if((s3=="T")&&(s4=="F"))temp.possible=doTF(choices,blank);

                                               else

                                                        if((s3=="F")&&(s4=="T"))temp.possible=doFT(choices,blank);

                                                        else

                                                                 if((s3=="T")&&(s4=="T"))temp.possible=doTT(choices,blank);

                              lists.push_back(temp);

                             cout<<temp.name<<temp.possible<<endl;

                            }

                            sort(lists.begin(),lists.end(),cmp());

                            for(vector<node>::iteratork=lists.begin();k!=lists.end();k++)

                            {

                                     cout<<(*k).name<<(*k).possible<<endl;

                                     ans.push_back((*k).name);

 

                            }

                            returnans;

 

                   }

};

 

SRM 144 DIV 2

 

200 Problem Statement

    

Computers tend to store dates and times assingle numbers which represent the number of seconds or milliseconds since aparticular date. Your task in this problem is to write a method whatTime, whichtakes an int, seconds, representing the number of seconds since midnight onsome day, and returns a string formatted as"<H>:<M>:<S>". Here, <H> represents thenumber of complete hours since midnight, <M> represents the number ofcomplete minutes since the last complete hour ended, and <S> representsthe number of seconds since the last complete minute ended. Each of <H>,<M>, and <S> should be an integer, with no extra leading 0's. Thus,if seconds is 0, you should return "0:0:0", while if seconds is 3661,you should return "1:1:1".

Definition

    

Class:

Time

Method:

whatTime

Parameters:

int

Returns:

string

Method signature:

string whatTime(int seconds)

(be sure your method is public)

    

 

Constraints

-

seconds will be between 0 and 24*60*60 - 1= 86399, inclusive.

Examples

0)

 

    

0

Returns: "0:0:0"

 

1)

 

    

3661

Returns: "1:1:1"

 

2)

 

    

5436

Returns: "1:30:36"

 

3)

 

    

86399

Returns: "23:59:59"

 

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

Code

 

 

#include <iostream>

#include <string>

#include <sstream>

 

using namespace std;

 

class Time

{

public: string whatTime(int seconds)

                   {

                            inth,m,s;

                            stringstream res;

                            h= (int) seconds / 3600;

                            m= (int)((seconds-h*3600) / 60);

                            s= (int)(seconds-h*3600-m*60);

                            res<< h << ":" << m << ":" << s;

                            return res.str();

                   }

};

 

 

550

 

(1)编译的时候一直错误引用内存,后来发现,vector在使用的时候,不存在的元素,不能引用它的下标,比如,没有给第0个元素赋值,就不能引用p[0],但是push_back了以后,第0个元素就存在了,就可以引用p[0]了

(2)看别人的代码很短,因为如果没结果就是NONE,所以可以把结果初始化成NONE

(3)其实就用题目给的函数就行了,不用像我一样开那么多函数

Problem Statement

    

Let's say you have a binary string such asthe following:

011100011

One way to encrypt this string is to add toeach digit the sum of its adjacent digits. For example, the above string wouldbecome:

123210122

In particular, if P is the original string,and Q is the encrypted string, then Q[i] = P[i-1] + P[i] + P[i+1] for all digitpositions i. Characters off the left and right edges of the string are treatedas zeroes.

An encrypted string given to you in thisformat can be decoded as follows (using 123210122 as an example):

Assume P[0] = 0.

Because Q[0] = P[0] + P[1] = 0 + P[1] = 1,we know that P[1] = 1.

Because Q[1] = P[0] + P[1] + P[2] = 0 + 1 +P[2] = 2, we know that P[2] = 1.

Because Q[2] = P[1] + P[2] + P[3] = 1 + 1 +P[3] = 3, we know that P[3] = 1.

Repeating these steps gives us P[4] = 0,P[5] = 0, P[6] = 0, P[7] = 1, and P[8] = 1.

We check our work by noting that Q[8] =P[7] + P[8] = 1 + 1 = 2. Since this equation works out, we are finished, and wehave recovered one possible original string.

Now we repeat the process, assuming theopposite about P[0]:

Assume P[0] = 1.

Because Q[0] = P[0] + P[1] = 1 + P[1] = 1,we know that P[1] = 0.

Because Q[1] = P[0] + P[1] + P[2] = 1 + 0 +P[2] = 2, we know that P[2] = 1.

Now note that Q[2] = P[1] + P[2] + P[3] = 0+ 1 + P[3] = 3, which leads us to the conclusion that P[3] = 2. However, thisviolates the fact that each character in the original string must be '0' or'1'. Therefore, there exists no such original string P where the first digit is'1'.

Note that this algorithm produces at mosttwo decodings for any given encrypted string. There can never be more than onepossible way to decode a string once the first binary digit is set.

Given a string message, containing theencrypted string, return a vector <string> with exactly two elements. Thefirst element should contain the decrypted string assuming the first characteris '0'; the second element should assume the first character is '1'. If one ofthe tests fails, return the string "NONE" in its place. For the aboveexample, you should return {"011100011", "NONE"}.

Definition

    

Class:

BinaryCode

Method:

decode

Parameters:

string

Returns:

vector <string>

Method signature:

vector <string> decode(string message)

(be sure your method is public)

    

 

Constraints

-

message will contain between 1 and 50characters, inclusive.

-

Each character in message will be either'0', '1', '2', or '3'.

Examples

0)

 

    

"123210122"

Returns: { "011100011",  "NONE" }

The example from above.

1)

 

    

"11"

Returns: { "01",  "10" }

We know that one of the digits must be '1',and the other must be '0'. We return both cases.

2)

 

    

"22111"

Returns: { "NONE",  "11001" }

Since the first digit of the encryptedstring is '2', the first two digits of the original string must be '1'. Ourtest fails when we try to assume that P[0] = 0.

3)

 

    

"123210120"

Returns: { "NONE",  "NONE" }

This is the same as the first example, butthe rightmost digit has been changed to something inconsistent with the rest ofthe original string. No solutions are possible.

4)

 

    

"3"

Returns: { "NONE",  "NONE" }

 

5)

 

    

"12221112222221112221111111112221111"

Returns:

{"01101001101101001101001001001101001",

 "10110010110110010110010010010110010" }

 

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

Code 203.38

#include<vector>

#include<string>

#include<iostream>

 

using namespace std;

 

class BinaryCode

{

 

private:vector<int> open(int key,vector<int> q)

                   {

                            vector<int>p;

                            p.clear();

                            stringans;

                            p.push_back(key);

                            if(q.size()<=1)

                            {

                                     p[0]=q[0];

                                     returnp;

                            }

                            p.push_back(q[0]-p[0]);

                            for(inti=2;i<q.size();i++)

                            {

                                     inttemp=q[i-1]-p[i-2]-p[i-1];

                                     p.push_back(temp);

                            }

                            returnp;

                   }

 

private: string valid(vector<int> p,vector<int> q)

                    {

                             string ans;

                             for(int i=0;i<p.size();i++)

                             {

                                      if ((p[i]!=0)&&(p[i]!=1))

                                                return "NONE";

                                      if (p[i]==0) ans.insert(i,"0");

                                      else ans.insert(i,"1");

                             }

                             int n=q.size()-1;

                             if (q[n]!=p[n-1]+p[n]) return"NONE";

                             return ans;

                    }

 

public:vector <string> decode(stringmessage)

            {

                      vector<string> ans;

                      ans.clear();

                      vector<int> msg;

                      msg.clear();

                      for(string::iteratori=message.begin();i!=message.end();i++)

                      {

                               msg.push_back((*i)-'0');

                      }

                     

                      vector<int> p;

                      for(int j=0;j<=1;j++)

                      {

                               p=open(j,msg);

                               ans.push_back(valid(p,msg));

                      }

                      return ans;

            }

};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值