早早地水完了三道题,pt1000用的是dfs,开始做的时候误认为复杂度最多就O(2^25),结果被一组O(2*3^16)的数据接近1e8给cha了。继续努力。
pt250:求两个串的前缀组成的不同串数目。set搞定。
1 /* 2 *Author: Zhaofa Fang 3 *Created time: 2013-07-10-18.54 4 *Language: C++ 5 */ 6 #include <cstdio> 7 #include <cstdlib> 8 #include <sstream> 9 #include <iostream> 10 #include <cmath> 11 #include <cstring> 12 #include <algorithm> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 #include <queue> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 25 #define REP(i,n) for(int i=0;i<(n);i++) 26 #define REPD(i,n) for(int i=(n-1);i>=0;i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 #define eps 1e-8 35 36 class TopFox{ 37 public : 38 int possibleHandles(string f, string g){ 39 set<string>S; 40 int lenf = f.length(); 41 int leng = g.length(); 42 REP(i,lenf){ 43 REP(j,leng){ 44 string tmp = ""; 45 REP(k,i+1)tmp += f[k]; 46 REP(k,j+1)tmp += g[k]; 47 S.insert(tmp); 48 } 49 } 50 return S.size(); 51 } 52 };
pt500:给定一幅图的连接情况,要求相邻两个点的差不大于d,求点之间的最大差。
可以对每个点进行bfs,比较直观。也有一些人用的是floyd,道理一样的。
1 /* 2 *Author: Zhaofa Fang 3 *Created time: 2013-07-10-18.54 4 *Language: C++ 5 */ 6 #include <cstdio> 7 #include <cstdlib> 8 #include <sstream> 9 #include <iostream> 10 #include <cmath> 11 #include <cstring> 12 #include <algorithm> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 #include <queue> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 25 #define REP(i,n) for(int i=0;i<(n);i++) 26 #define REPD(i,n) for(int i=(n-1);i>=0;i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 #define eps 1e-8 35 36 class Egalitarianism{ 37 public : 38 bool vist[100]; 39 int mon[100]; 40 bool OK; 41 int bfs(int s,vector <string> isFriend, int d){ 42 int n = isFriend.size(); 43 queue<int>Q; 44 Q.push(s); 45 int ans = -1; 46 memset(vist,0,sizeof(vist)); 47 mon[s] = 0; 48 vist[s] = 1; 49 while(!Q.empty()){ 50 int now = Q.front(); 51 Q.pop(); 52 REP(i,n){ 53 if(isFriend[now][i] == 'Y' && !vist[i]){ 54 Q.push(i); 55 mon[i] = mon[now] + d; 56 vist[i] = 1; 57 ans = max(ans,mon[i]); 58 } 59 } 60 } 61 REP(i,n)if(!vist[i])return -1; 62 return ans; 63 } 64 int maxDifference(vector <string> isFriend, int d){ 65 int n = isFriend.size(); 66 int ans = -1; 67 REP(i,n)ans = max(bfs(i,isFriend,d),ans); 68 return ans; 69 } 70 };
pt1000:给定一组数kind[],从中取K个,取得的数为found[],能有多少种可能。其中一组sample如下:
|
正解dp。很简单的转移-__-。
1 /* 2 *Author: Zhaofa Fang 3 *Created time: 2013-07-10-18.54 4 *Language: C++ 5 */ 6 #include <cstdio> 7 #include <cstdlib> 8 #include <sstream> 9 #include <iostream> 10 #include <cmath> 11 #include <cstring> 12 #include <algorithm> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 #include <queue> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 25 #define REP(i,n) for(int i=0;i<(n);i++) 26 #define REPD(i,n) for(int i=(n-1);i>=0;i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 #define eps 1e-8 35 36 ll C[55][55]; 37 38 void pre(){ 39 C[1][0] = C[1][1] = 1; 40 FOR(i,2,50){ 41 C[i][0] = 1; 42 FOR(j,1,i)C[i][j] = C[i-1][j-1] + C[i-1][j]; 43 } 44 } 45 class Excavations2{ 46 public : 47 int ff[55]; 48 ll dp[55][55]; 49 ll count(vector <int> kind, vector <int> found, int K){ 50 pre(); 51 int len = kind.size(); 52 memset(ff,0,sizeof(ff)); 53 REP(i,len)ff[kind[i]]++; 54 memset(dp,0,sizeof(dp)); 55 int n = found.size(); 56 FOR(i,1,ff[found[0]]){ 57 dp[1][i] = C[ff[found[0]]][i]; 58 } 59 FOR(i,2,n){ 60 FOR(j,1,K){ 61 FOR(k,1,ff[found[i-1]]) 62 dp[i][j] += dp[i-1][j-k]*C[ff[found[i-1]]][k]; 63 } 64 } 65 return dp[n][K]; 66 } 67 }; 68 69 70 //int main(){ 71 // //freopen("in","r",stdin); 72 // //freopen("out","w",stdout); 73 // 74 // return 0; 75 //}