A 1084 Broken Keyboard
注意大小写即可。
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <string> 5 6 using namespace std; 7 8 int main() 9 { 10 string inStr, outStr; 11 cin >> inStr >> outStr; 12 int inIndex = 0, outIndex = 0; 13 bool charFlag[200]={false}; 14 while(inIndex < inStr.size()) 15 { 16 if(inStr[inIndex] == outStr[outIndex]) 17 { 18 inIndex++; 19 outIndex < outStr.size()-1 ? outIndex++ : outStr[outIndex] = ‘ ‘; 20 } 21 else 22 { 23 int tmpNum = (int)inStr[inIndex++]; 24 tmpNum = tmpNum >= ‘a‘ ? tmpNum-‘a‘+‘A‘ : tmpNum; 25 if(!charFlag[tmpNum]) 26 { 27 charFlag[tmpNum] = 1; 28 printf("%c", tmpNum); 29 } 30 } 31 } 32 return 0; 33 }
A 1085 Perfect Sequence
1.二分查找,不然会超时
2.注意数值的范围,有些地方需要使用long long
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 6 using namespace std; 7 typedef long long LL; 8 vector<int> inputNumVec; 9 int N, P; 10 int getMaxCnt(int lIndex) 11 { 12 int rIndex = inputNumVec.size()-1, midIndex; 13 LL tmpNum = (LL)inputNumVec[lIndex]*P; 14 while(lIndex <= rIndex) 15 { 16 midIndex = (lIndex + rIndex) >> 1; 17 if(inputNumVec[midIndex] > tmpNum) 18 rIndex = midIndex - 1; 19 else 20 lIndex = midIndex + 1; 21 } 22 return rIndex; 23 } 24 int main() 25 { 26 cin >> N >> P; 27 inputNumVec.resize(N); 28 for(int i = 0; i < N; ++ i) 29 cin >> inputNumVec[i]; 30 sort(inputNumVec.begin(), inputNumVec.end()); 31 int maxCnt = 0, tmpNum; 32 for(int i = 0; i <= inputNumVec.size()-maxCnt; ++ i) 33 { 34 tmpNum = getMaxCnt(i)-i+1; 35 if(tmpNum > maxCnt) 36 maxCnt = tmpNum; 37 } 38 cout << maxCnt; 39 return 0; 40 }
A 1086 Tree Traversals Again
自己想个办法建树之后进行后续遍历即可
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 #include <string> 6 #include <stack> 7 8 using namespace std; 9 typedef long long LL; 10 #define NoAddr -1 11 typedef struct NODE 12 { 13 int val, lChild, rChild; 14 NODE(int v):val(v),lChild(NoAddr),rChild(NoAddr){} 15 }node; 16 vector<node> nodeVec; 17 stack<int> valVec; 18 bool coutFlag = false; 19 void postOrder(int tmpRoot) 20 { 21 if(tmpRoot >= nodeVec.size()) 22 return; 23 if(nodeVec[tmpRoot].lChild > 0) 24 postOrder(nodeVec[tmpRoot].lChild); 25 if(nodeVec[tmpRoot].rChild > 0) 26 postOrder(nodeVec[tmpRoot].rChild); 27 coutFlag ? printf(" ") :coutFlag = true; 28 cout << nodeVec[tmpRoot].val; 29 } 30 int main() 31 { 32 int N, tmpNum, lastIndex = 0; 33 string tmpStr; 34 bool pushFlag = true; 35 cin >> N; 36 while(N > 0) 37 { 38 cin >> tmpStr; 39 if(tmpStr == "Push") 40 { 41 cin >> tmpNum; 42 valVec.push(nodeVec.size()); 43 nodeVec.push_back(NODE(tmpNum)); 44 pushFlag ? nodeVec[lastIndex].lChild = nodeVec.size()-1 : 45 nodeVec[lastIndex].rChild = nodeVec.size()-1 ; 46 lastIndex = nodeVec.size()-1; 47 pushFlag = true; 48 } 49 else 50 { 51 lastIndex = valVec.top(); 52 valVec.pop(); 53 pushFlag = false; 54 N--; 55 } 56 } 57 postOrder(0); 58 return 0; 59 }
A 1087 All Roads Lead to Rome
1.城市名string 和 序号index的转换
2.Dijkstra找最短距离
3.DFS根据题目中给出的优先级找到所需的条件
1 #include <cstdio> 2 #include <stdlib.h> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <string> 8 #include <stack> 9 #include <unordered_map> 10 using namespace std; 11 #define NoAddr -1 12 #define MAX_CITY 210 13 #define CLR(a,b) memset(a,b,sizeof(a)) 14 typedef long long LL; 15 const int INF = 0x7f7f7f7f; 16 int N, K; 17 string citySt; 18 int route[MAX_CITY][MAX_CITY]={0}; 19 vector<int> preVec[MAX_CITY]; 20 vector<int> tmpPath; 21 vector<int> outPath; 22 vector<int> disVec; 23 vector<int> cityHappyValVec; 24 int minCityCnt = MAX_CITY, maxHappy = -1, minPathCnt = 0, tmpMaxHappy = 0; 25 unordered_map<string, int> cityToIndexMap; 26 unordered_map<int, string> indexToCityMap; 27 void Dijkstra(int u) 28 { 29 vector<bool> visitFlagVec(N, false); 30 disVec.resize(N, INF); 31 disVec[u] = 0; 32 for(int i = 0; i < N; ++ i) 33 { 34 int minDis = INF, v = -1; 35 for(int j = 0; j < N; ++ j) 36 if(!visitFlagVec[j] && disVec[j] < minDis) 37 { 38 minDis = disVec[j]; 39 v = j; 40 } 41 if(v == -1) 42 return; 43 visitFlagVec[v] = true; 44 for(int j = 0; j < N; ++ j) 45 { 46 if(!visitFlagVec[j] && disVec[v] + route[v][j] < disVec[j]) 47 { 48 disVec[j] = disVec[v] + route[v][j]; 49 preVec[j].clear(); 50 preVec[j].push_back(v); 51 } 52 else if(!visitFlagVec[j] && disVec[v] + route[v][j] == disVec[j]) 53 { 54 preVec[j].push_back(v); 55 } 56 } 57 } 58 } 59 void dfs(int u) 60 { 61 if(u == 0) 62 { 63 minPathCnt ++; 64 tmpPath.push_back(0); 65 if(tmpMaxHappy > maxHappy) 66 { 67 maxHappy = tmpMaxHappy; 68 outPath = tmpPath; 69 minCityCnt = tmpPath.size(); 70 } 71 else if(tmpMaxHappy == maxHappy && tmpPath.size() < minCityCnt) 72 { 73 outPath = tmpPath; 74 } 75 tmpPath.pop_back(); 76 } 77 tmpPath.push_back(u); 78 tmpMaxHappy += cityHappyValVec[u]; 79 for(int i = 0; i < preVec[u].size(); ++ i) 80 dfs(preVec[u][i]); 81 tmpMaxHappy -= cityHappyValVec[u]; 82 tmpPath.pop_back(); 83 } 84 int main() 85 { 86 cin >> N >> K >> citySt; 87 cityHappyValVec.resize(N, 0); 88 string tmpCity, tmpSt, tmpEnd; 89 int tmpHappy, tmpDis, stIndex, endIndex, endCityIndex; 90 cityToIndexMap[citySt] = 0; 91 indexToCityMap[0] = citySt; 92 for(int i = 1; i < N; ++ i) 93 { 94 cin >> tmpCity >> tmpHappy; 95 cityHappyValVec[i] = tmpHappy; 96 cityToIndexMap[tmpCity] = i; 97 indexToCityMap[i] = tmpCity; 98 if(tmpCity == "ROM") 99 endCityIndex = i; 100 } 101 CLR(route,0x7f); 102 while(K--) 103 { 104 cin >> tmpSt >> tmpEnd >> tmpDis; 105 stIndex = cityToIndexMap[tmpSt]; 106 endIndex = cityToIndexMap[tmpEnd]; 107 route[stIndex][endIndex] = tmpDis; 108 route[endIndex][stIndex] = tmpDis; 109 } 110 Dijkstra(0); 111 dfs(endCityIndex); 112 cout << minPathCnt << " " << disVec[endCityIndex] << " " << maxHappy << " " << (int)maxHappy/(outPath.size()-1) << endl; 113 bool symbolFlag = false; 114 for(int i = outPath.size()-1; i >= 0; -- i) 115 { 116 symbolFlag ? printf("->") : symbolFlag = true; 117 cout << indexToCityMap[outPath[i]]; 118 } 119 return 0; 120 }
原文地址:https://www.cnblogs.com/codewars/p/11391781.html
时间: 2024-09-29 13:31:18