2007

    技术2022-05-11  14

    250分题目 Problem Statement ???? All the alphabetical keys on your keyboard are broken. Given a string word, return the minimal number of keys you must repair to be able to type word. Definition ???? Class: BrokenKeyboardRepair Method: minimalNumberOfKeys Parameters: string Returns: int Method signature: int minimalNumberOfKeys(string word) (be sure your method is public) ???? Constraints - word will contain between 1 and 50 characters, inclusive. - word will contain only lowercase letters ('a'-'z'). Examples 0) ???? "coder" Returns: 5 You have to repair the following keys: 'c', 'd', 'e', 'o', 'r'. 1) ???? "hello" Returns: 4 You have to repair the following keys: 'e', 'h', 'l', 'o'. 2) ???? "abracadabra" Returns: 5 You have to repair the following keys: 'a', 'b', 'c', 'd', 'r'. //简单说就是要计算一个字符串中不同字符的个数 #include <iostream> #include <string> #include <stdio.h> #include <vector> #include <set> #include <map> #include <algorithm> #include <functional> #include <numeric> #include <cstdio> #include <cstdlib> #include <queue> using namespace std; class BrokenKeyboardRepair {    public:           vector <char> w_str;           int minimalNumberOfKeys(string word)           {            string p_word=""+word[0];            int returns_r=1;            for(int i =1 ;i<word.size();i++) if ( isInIt(p_word,word[i])==false )  returns_r++            return returns_r;           }           bool isInIt(string &s,char c)           {             for(int i =0 ;i<s.size();i++) if (s[i]==c) return true; else { s=s+c;return false;}           } }; 房间得分最高的一个选手 #include <iostream> #include <string> #include <stdio.h> #include <vector> #include <set> #include <map> #include <algorithm> #include <functional> #include <numeric> #include <cstdio> #include <cstdlib> #include <queue> #include <list> #include <deque> #include <stack> #include <bitset> #include <utility> #include <sstream> #include <cctype> #include <cmath> #include <ctime> using namespace std; #define foreach(it,c) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define print(A,N) copy(A,A+N,ostream_iterator<int>(cout,"")) class BrokenKeyboardRepair {    public:           int minimalNumberOfKeys(string); }; int BrokenKeyboardRepair::minimalNumberOfKeys(string word) {    set<char> res;    for(int i=0; i<word.size();i++)    res.insert(word[i]); return res.size(); }    

    最新回复(0)