250pt:
题目:
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
分析:找出x之间差的最大值,y之间差的最大值即可代码:
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; class BoundingBox { public: int smallestArea(vector <int>, vector <int>); }; int BoundingBox::smallestArea(vector <int> X, vector <int> Y) { int xmax=-10000; int xmin = 10000; int ymax = -10000; int ymin = 10000; for(int i=0;i<X.size();i++) { xmax = max(X[i],xmax); xmin = min(X[i],xmin); } for(int i=0;i<Y.size();i++) { ymax = max(Y[i],ymax); ymin = min(Y[i],ymin); } return (xmax-xmin)*(ymax-ymin); } //Powered by [KawigiEdit] 2.0!
题目:
Problem Statement |
|||||||||||||
Marco recently learned about palindromic strings. A palindromic string is a string that reads the same forwards and backwards. For example, "radar" and "racecar" are palindromic strings. Now Marco is excited about palindromic strings. In particular, he likes strings that have a lot of palindromic substrings. For example, he really likes the string "aaa" because it has 6 palindromic substrings: "a" occurs three times, "aa" occurs twice, and "aaa" occurs once. Right now, Marco has a string S composed of lowercase letters. You are to reconstruct S from the given vector <string>s S1 and S2 as follows:
Return the number of palindromic substrings in S. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | S1 and S2 will each contain no more than 50 elements. | ||||||||||||
- | Each element of S1 and S2 will contain no more than 50 characters. | ||||||||||||
- | S will contain at least 1 character. | ||||||||||||
- | S will contain only lowercase letters ('a' - 'z'). | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
分析:如果暴力遍历的话,复杂度是N的三次,N=5000,明显复杂度太高,故用动态规划把复杂度降到N的平方代码:
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; class PalindromicSubstringsDiv2 { public: int count(vector <string>, vector <string>); }; int PalindromicSubstringsDiv2::count(vector <string> S1, vector <string> S2) { string s = ""; for(int i=0;i<S1.size();i++) s+=S1[i]; for(int i=0;i<S2.size();i++) s+=S2[i]; int n = s.length(); vector<vector<bool> > dp(n,vector<bool>(n,false)); for(int len = 1;len<=n;len++) { for(int i=0;i<=n-len;i++) { int j = i+len-1; if(i==j) dp[i][j]=true; else if(j==i+1) { if(s[i]==s[j]) dp[i][j]=true; } else if(dp[i+1][j-1]&&s[i]==s[j]) dp[i][j]=true; } } int ret = 0; for(int i=0;i<n;i++) for(int j=i;j<n;j++) if(dp[i][j]) ret++; return ret; } //Powered by [KawigiEdit] 2.0!
终于再次爬回div1了。。。再接再厉!