250pt:
水题,直接统计矩阵中的A-Z的字符个数然后与给定的数匹配即可。
500pt:
话说这次的500真的不难,可是还是由于自己的粗心导致被查。
题意:
给你两个数A,B问最少的步数从A得到B,怎么走这样规定:可以A/10的到一个数,或者翻转A得到一个数。问最少的不熟得到B,如果不能输出-1;
思路:
思路很明确,如果A中包含B这个字符串,那么肯定能够得到B,否则输出-1.最少步数就是A的长度减去B的长度, 然后加上翻转的次数即可,这里翻转是关键。刚开始我没有考虑到这种情况99999 9 这种直接4步即可, 后来想了想,我们如果在A的0位置就能发现B那么只需要除10即可,否则,就要考虑反着找到B的情况,如果找到后,我们只需要加上一次翻转即可,否则是正着能够够找到,要反正两个了。
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define ll long long #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("din.txt", "r", stdin) #define Write() freopen("dout.txt", "w", stdout); #define M 5007 #define N 1007 using namespace std; int n,m; class TheNumberGameDiv2 { public: string Rverse(string ai) { string tmp = ""; for (int i = 0; i < ai.size(); ++i) { tmp = ai[i] + tmp; } return tmp; } int minimumMoves(int A, int B) { string ai = ""; string bi = ""; while (A) { ai.push_back(((A%10) + '0')); // ai = ((A%10) + '0') + ai; A /= 10; } while (B) { bi.push_back(((B%10) + '0')); // bi = ((B%10) + '0') + bi; B /= 10; } ai = Rverse(ai); bi = Rverse(bi); cout<<ai<<endl<<bi<<endl; int ans = 0; int id = ai.find(bi); if (id == 0)//出现这样的情况 9999 9 { ans = ai.size() - bi.size(); } else { string tmp = Rverse(ai); cout<<"TMP = " <<tmp<<" " <<bi<<endl; int pos = tmp.find(bi); if (pos == -1 && id == -1) ans = -1; else { ans = ai.size() - bi.size() + 1; printf("%d %d\n",ans,pos); if (pos == -1) ans += 1;//只存在正着的情况 } } return ans; } }; // Powered by FileEdit // Powered by TZTester 1.01 [25-Feb-2003] // Powered by CodeProcessor // Powered by FileEdit // Powered by TZTester 1.01 [25-Feb-2003] // Powered by CodeProcessor //int main() //{ // TheNumberGameDiv2 a; // printf("%d\n",a.minimumMoves(25,5)); //} int main() { // string s1 = "15674674"; // string s2 = "67"; // printf("%d\n",s1.find(s2)); TheNumberGameDiv2 a; cout<<a.minimumMoves(9798147, 79817)<<endl; }
1000 pt:
题意:
给定N,按顺时针排列这些数 1 - N ,然后给出一个vector<> p里面的数是从1 - N中取出来的数,然后依次连接p[i] 与 p[i + 1]. 然后从不属于p的剩余还未连线的点中找出一个点与p[M - 1]连线(M表示P的长度),tail[0],依次再找满足上述条件不在p中也不再tail中的点与tail[0]连线,tail[1],tail[N - M - 1]最后于p[0]连线, 问一共存在多少种连线?(注意这里的新连接的线段,必须与在这条线出现之前出现的线段相交)。
思路:
其实这里看到13这么小的数据就应该想到DFS的。
比赛时时间不够没有想出来,还以为是排列组合什么的,赛后想了想,这里关键判断两个点是否可以连线。我们只要从p[M - 1]开始枚举满足上述条件的点,然后深搜,找到满足条件的连线计数即可。这里关键是如何判断两点可以连接。
假设判断x与y连线,我们只要判断这个圆,x->y上半部分的点与下半部分的点之间存在连线即可连接。
DFS+判断连线:
#line 5 "PolygonTraversal2.cpp" #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define inf 0x7f7f7f7f #define MOD 1073741824 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define PB push_back #define MP make_pair #define REP(i,n) for(i=0;i<(n);++i) #define FOR(i,l,h) for(i=(l);i<=(h);++i) #define FORD(i,h,l) for(i=(h);i>=(l);--i) typedef vector<int> VI; typedef vector<string> VS; typedef vector<double> VD; typedef long long LL; typedef pair<int,int> PII; #define M 20 class PolygonTraversal2 { public: int mp[M][M]; int a[M],b[M],la,lb,len; bool vt[M]; int ans,e; bool isok(int x,int y,int N) { int i,j; if (x < y) swap(x,y); la = lb = 0; int tx = x,ty = y; tx++; while (tx <= N) a[la++] = tx++; ty--; while (ty >= 1) a[la++] = ty--; tx = x; ty = y; for (i = y + 1; i < x; ++i) b[lb++] = i; for (i = 0; i < la; ++i) { for (j = 0; j < lb; ++j) { if (mp[a[i]][b[j]]) return true; } } return false; } void dfs(int u,int ct,int N) { int i; if (ct == N - len) { if (isok(u,e,N)) { ans++; } return ; } for (i = 1; i <= N; ++i) { if (!vt[i]) { if (isok(u,i,N)) { mp[u][i] = mp[i][u] = 1; vt[i] = true; dfs(i,ct + 1,N); mp[u][i] = mp[i][u] = 0; vt[i] = false; } } } } int count(int N, vector <int> points) { int i; len = points.size(); CL(mp,0); CL(vt,false); for (i = 0; i < len - 1; ++i) { mp[points[i]][points[i + 1]] = 1; mp[points[i + 1]][points[i]] = 1; vt[points[i]] = vt[points[i + 1]] = true; } int st = points[len - 1]; e = points[0]; ans = 0; dfs(st,0,N); return ans; } // BEGIN CUT HERE public: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 4)) test_case_5(); } private: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } void test_case_0() { int Arg0 = 5; int Arr1[] = {1, 3, 5}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1; verify_case(0, Arg2, count(Arg0, Arg1)); } void test_case_1() { int Arg0 = 6; int Arr1[] = {1, 4, 2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1; verify_case(1, Arg2, count(Arg0, Arg1)); } void test_case_2() { int Arg0 = 7; int Arr1[] = {2, 4, 7}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(2, Arg2, count(Arg0, Arg1)); } void test_case_3() { int Arg0 = 7; int Arr1[] = {1, 2, 3, 4, 6, 5}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(3, Arg2, count(Arg0, Arg1)); } void test_case_4() { int Arg0 = 11; int Arr1[] = {1, 5, 10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1412; verify_case(4, Arg2, count(Arg0, Arg1)); } void test_case_5() { int Arg0 = 6; int Arr1[] = {6, 3, 1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1; verify_case(5, Arg2, count(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE int main() { PolygonTraversal2 ___test; ___test.run_test(-1); return 0; } // END CUT HERE