250pt
给定手机0-9按键对应的英文字母(1个对多个),0固定对应空格。然后在给定一些单词。以及一个要处理的串,叫你按照那个串模拟输出结果
思路:
大模拟,写的有点乱
1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "T9.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <algorithm> 13 #include <vector> 14 #include <string> 15 #include <iostream> 16 #include <sstream> 17 #include <map> 18 #include <set> 19 #include <queue> 20 #include <stack> 21 #include <fstream> 22 #include <numeric> 23 #include <iomanip> 24 #include <bitset> 25 #include <list> 26 #include <stdexcept> 27 #include <functional> 28 #include <utility> 29 #include <ctime> 30 using namespace std; 31 32 #define PB push_back 33 #define MP make_pair 34 35 #define REP(i,n) for(i=0;i<(n);++i) 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i) 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i) 38 39 typedef vector<int> VI; 40 typedef vector<string> VS; 41 typedef vector<double> VD; 42 typedef long long LL; 43 typedef pair<int,int> PII; 44 struct oo{ 45 string d, s; 46 bool operator <(const oo &b)const{ 47 if (s < b.s || (s == b.s && d < b.d)) return true; 48 return false; 49 } 50 }; 51 52 class T9 53 { 54 public: 55 char d[260]; 56 oo S[120]; 57 int n, m, k; 58 string find(string a, int k){ 59 int cnt = 0; 60 for (int i = 0; i < m; ++i){ 61 if (S[i].s == a) ++cnt; 62 if (cnt == k) return S[i].d; 63 } 64 return ""; 65 } 66 string message(vector <string> part, vector <string> dict, vector <string> keystr) 67 { 68 69 n = part.size(), m = dict.size(), k = keystr.size(); 70 for (int i = 0; i < n; ++i) 71 for (int j = 0; j < part[i].size(); ++j) 72 d[(int)part[i][j]] = i + 49; 73 for (int i = 0; i < m; ++i){ 74 S[i].d = dict[i]; 75 S[i].s.clear(); 76 for (int j = 0; j < dict[i].size(); ++j) 77 S[i].s.push_back(d[dict[i][j]]); 78 } 79 sort(S, S + m); 80 string ans = ""; 81 int cnt = 0; 82 string tmp = ""; 83 string ks = accumulate(keystr.begin(), keystr.end(), string("")); 84 int k = ks.size(); 85 for (int i = 0; i < k; ++i){ 86 if (ks[i] == ‘*‘){ cnt += 5; continue; } 87 if (ks[i] == ‘#‘){ cnt += 1; continue; } 88 if ((ks[i] < ‘1‘ || ks[i] > ‘9‘) && (int)tmp.size() > 0){ 89 ans += find(tmp, cnt + 1); 90 cnt = 0; 91 tmp.clear(); 92 } 93 if (ks[i] == ‘0‘) ans += ‘ ‘; 94 if (ks[i] > ‘0‘ && ks[i] <= ‘9‘) tmp += ks[i]; 95 } 96 if ((int)tmp.size() > 0) ans += find(tmp, cnt + 1); 97 return ans; 98 } 99 100 };
500pt
给定n(n <= 40w)个城市,然后在给定2个数组(需要按照给定的算出来),一个为不坐飞机情况下i->i+1所用的时间,一个为作为飞机所用的时间
求在不超过坐k次飞机的情况下,最少时间(一次可以连续做多个城市,但必须连续,比如i->i+1->i+2..)
思路:
动态规划
dp[i][j][k]表示当前到i城市,做了j次飞机,k(0或1)表示最后一次做没做飞机所用的最少时间
方程应该很好推。但要用滚动数组
1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "RoadOrFlightHard.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <algorithm> 13 #include <vector> 14 #include <string> 15 #include <iostream> 16 #include <sstream> 17 #include <map> 18 #include <set> 19 #include <queue> 20 #include <stack> 21 #include <fstream> 22 #include <numeric> 23 #include <iomanip> 24 #include <bitset> 25 #include <list> 26 #include <stdexcept> 27 #include <functional> 28 #include <utility> 29 #include <ctime> 30 using namespace std; 31 32 #define PB push_back 33 #define MP make_pair 34 35 #define REP(i,n) for(i=0;i<(n);++i) 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i) 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i) 38 #define Inf (1LL << 50) 39 typedef vector<int> VI; 40 typedef vector<string> VS; 41 typedef vector<double> VD; 42 typedef long long LL; 43 typedef pair<int,int> PII; 44 long long rT[420000], fT[420000]; 45 long long dp[2][45][2]; 46 47 class RoadOrFlightHard 48 { 49 public: 50 long long minTime(int N, int roadFirst, int roadProd, int roadAdd, int roadMod, int flightFirst, int flightProd, int flightAdd, int flightMod, int K) 51 { 52 rT[0] = roadFirst % roadMod; 53 fT[0] = flightFirst % flightMod; 54 // cout << rT[0] << endl; 55 for (int i = 1; i < N; ++i){ 56 rT[i] = (rT[i-1]*roadProd + roadAdd) % roadMod; 57 fT[i] = (fT[i-1]*flightProd + flightAdd) % flightMod; 58 } 59 for (int i = 0; i <= K; ++i) 60 dp[0][i][0] = dp[0][i][1] = Inf; 61 dp[0][0][0] = 0; 62 for (int i = 0; i < N; ++i){ 63 int cur = i & 1; 64 for (int j = 0; j <= K; ++j) 65 dp[cur^1][j][0] = dp[cur^1][j][1] = Inf; 66 for (int j = 0; j <= K; ++j){ 67 if (dp[cur][j][0] < Inf){ 68 if(j < K) dp[cur^1][j+1][1] = min(dp[cur^1][j+1][1], dp[cur][j][0] + fT[i]); 69 dp[cur^1][j][0] = min(dp[cur^1][j][0], dp[cur][j][0] + rT[i]); 70 } 71 if (dp[cur][j][1] < Inf){ 72 if(j < K) dp[cur^1][j+1][1] = min(dp[cur^1][j+1][1], dp[cur][j][1] + fT[i]); 73 dp[cur^1][j][1] = min(dp[cur^1][j][1], dp[cur][j][1] + fT[i]); 74 dp[cur^1][j][0] = min(dp[cur^1][j][0], dp[cur][j][1] + rT[i]); 75 } 76 } 77 } 78 long long ans = Inf; 79 for (int i = 0; i <= K; ++i) 80 for (int j = 0; j <= 1; ++j) 81 ans = min(ans, dp[N & 1][i][j]); 82 return ans; 83 } 84 85 };
原文:http://www.cnblogs.com/yzcstc/p/3602551.html