首页 > 代码库 > SGU 403 404 405 406 407 408 409 410 411 413
SGU 403 404 405 406 407 408 409 410 411 413
SGU 403
#include<stdio.h> int x; int main(){ while(~scanf("%d",&x)) printf("%d\n", 2*x+1); return 0; }
SGU 404
#include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; int n, m; char s[105][105]; int main(){ int i ,j; while(~scanf("%d %d",&m,&n)) { m--; for(i = 0; i < n; i++)scanf("%s", s[i]); printf("%s\n", s[m%n]); } return 0; }
SGU 405
#include <cstdio> #include <algorithm> using namespace std; const int N = 105; int s[N]; int main() { int n, m, a, b, x, y; scanf("%d%d", &n, &m); for(int i = 0; i < m; i ++) { scanf("%d%d", &a, &b); for(int j = 0; j < n; j ++) { scanf("%d%d", &x ,&y); if(a == x) s[j]++; if(y == b) s[j]++; if(a-b == x-y) s[j] += 3; if((a-b)*(x-y) > 0) s[j] += 2; if(a == b && x == y) s[j] += 2; } } for(int i = 0; i < n; i ++) { printf("%d%c", s[i], i == n-1 ? '\n' : ' '); } return 0; }SGU 406
#include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; int n, m; int a[12][101]; vector<int>A[11], tmp, ans, no; int main(){ int i ,j, u, v; while(~scanf("%d %d",&n,&m)) { for(i = 1; i <= n; i++) { memset(a[i], 0, sizeof a[i]); A[i].clear(); scanf("%d",&j); while(j--) { scanf("%d",&u); A[i].push_back(u); a[i][u]++; } } while(m--) { scanf("%d", &u); tmp.clear(); ans.clear(); no.clear(); memset(a[0], 0, sizeof a[0]); while(u--) { scanf("%d",&v); if(v<0){no.push_back(-v); continue;} tmp.push_back(v); } for(i = 1; i <= n; i++) { bool ok = true; for(j = 0; ok && j < no.size(); j++) if(a[i][no[j]])ok = false; for(j = 0; ok && j < tmp.size(); j++) if(!a[i][tmp[j]])ok = false; if(ok)ans.push_back(i); } printf("%d\n", ans.size()); for(i = 0; i < ans.size(); i++) { u = ans[i]; printf("%d ", A[u].size()); for(j = 0; j < A[u].size(); j++) printf("%d%c", A[u][j], (j+1)==A[u].size()?'\n':' '); } } } return 0; }
SGU 407
Java大数
题解
SGU 408
显然是2个数字越接近越好,即平方数。。而且答案一定为整数
#include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #define N 10000 ll n; int main(){ while(cin>>n){ if(n<=1){ cout<<n; puts(".000"); continue;} ll ans = 5, x = 2, y = 2; bool yes = true; for(ll i = 3; i <= n; i++) { if(yes) x++; else y++; yes ^= 1; ans += x*y; } cout<<ans; puts(".000"); } return 0; }
SGU 409
#include <cstdio> #include <cstring> using namespace std; const int N = 25; char a[N][N]; char ans[N*N][N*N]; int main(){ <span style="white-space:pre"> </span>int n, k; scanf("%d%d", &n, &k); for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) { if(k > 0) { a[i][j] = '*'; k--; } else a[i][j] = '.'; } } for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) { for(int ii = 0; ii < n; ii ++) { for(int jj = 0; jj < n; jj ++) { ans[i*n+ii][j*n+jj] = a[(ii + j) % n][(jj + i) % n]; } } } } for(int i = 0; i < n*n; i ++) { for(int j = 0; j < n*n; j ++) { printf("%c", ans[i][j]); } puts(""); } return 0; }
SGU 410
百度有解。。
减到使得最大数是最小数的2倍就停止,然后翻倍再减
SGU 411
后缀数组
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX_N = 90007; #define F(x) ((x)/3+((x)%3==1?0:tb)) #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) int wa[MAX_N], wb[MAX_N], wv[MAX_N], ws[MAX_N]; int height[MAX_N], rank[MAX_N], sa[MAX_N]; struct Suffix { int c0(int *r, int a, int b) { return r[a] == r[b] && r[a+1] == r[b+1] && r[a+2] == r[b+2]; } int c12(int k, int *r, int a, int b) { if(k==2) return r[a] < r[b] || r[a] == r[b] && c12(1, r, a+1, b+1); else return r[a] < r[b] || r[a] == r[b] && wv[a+1] < wv[b+1]; } void sort(int *r,int *a,int *b,int n,int m) { int i; for(i = 0; i < n; i++) wv[i] = r[a[i]]; for(i = 0; i < m; i++) ws[i] = 0; for(i = 0; i < n; i++) ws[wv[i]]++; for(i = 1; i < m; i++) ws[i] += ws[i-1]; for(i = n-1; i >= 0; i--) b[--ws[wv[i]]] = a[i]; } void dc3(int *r, int *sa, int n, int m) { int i, j, *rn = r + n, *san = sa + n, ta = 0, tb = (n+1) / 3,tbc = 0, p; r[n] = r[n+1] = 0; for(i = 0; i < n; i++) if(i % 3 != 0) wa[tbc++] = i; sort(r + 2, wa, wb, tbc, m); sort(r + 1, wb, wa, tbc, m); sort(r, wa, wb, tbc, m); for(p = 1, rn[F(wb[0])] = 0, i = 1; i < tbc; i++) rn[F(wb[i])] = c0(r, wb[i-1], wb[i]) ? p - 1 : p++; if(p < tbc) dc3(rn, san, tbc, p); else for(i = 0; i < tbc; i++) san[rn[i]]=i; for(i = 0; i < tbc; i++) if(san[i] < tb) wb[ta++] = san[i]*3; if(n % 3 == 1) wb[ta++] = n-1; sort(r, wb, wa, ta, m); for(i = 0; i < tbc; i++) wv[wb[i] = G(san[i])] = i; for(i = 0, j = 0, p = 0; i < ta && j < tbc; p++) sa[p] = c12(wb[j]%3, r, wa[i], wb[j]) ? wa[i++] : wb[j++]; for(; i<ta; p++) sa[p] = wa[i++]; for(; j<tbc; p++) sa[p] = wb[j++]; } void calheight(int *r, int *sa, int n) { for (int i = 1; i <= n; ++i) rank[sa[i]] = i; for (int i = 0, k = 0; i < n; ++i) { if(k) --k; int j = sa[rank[i]-1]; while (i + k < n && j + k < n && r[i+k] == r[j+k]) ++k; height[rank[i]] = k; } } }; int maxLen, begin; char str[MAX_N], s1[MAX_N], s[MAX_N]; int d[MAX_N], p[MAX_N]; char m[MAX_N]; void Manacher(int st, int len) { int cnt = 0; m[cnt++] = '$', m[cnt++] = '#'; int len1 = min(st + len, (int)strlen(s1)); for (int i = st; i < len1; ++i) { m[cnt++] = s1[i]; m[cnt++] = '#'; } m[cnt] = '\0'; memset(p, 0, sizeof p); int mx = 0, id = 0; for (int i = 1; i < cnt; ++i) { if (mx > i) p[i] = min(p[2 * id - i], mx - i); else p[i] = 1; while (i - p[i] > 0 && i + p[i] < cnt && m[i - p[i]] == m[i + p[i]]) ++p[i]; if (i + p[i] > mx) { mx = i + p[i]; id = i; } } int d = 0; for (int i = 1; i < cnt; ++i) { if (p[i] - 1 > maxLen) { maxLen = p[i] - 1; begin = st - (p[i] - 1) / 2 + d; } if (m[i] != '#') ++d; } } int main() { scanf("%s", s1); int l1 = (int) strlen(s1); int cnt = 0; for (int i = 0; s1[i]; ++i) { str[cnt++] = s1[i]; } str[cnt++] = 'z' + 1; scanf("%s", s); int l2 = (int) strlen(s); for (int i = 0; s[i]; ++i) { str[cnt++] = s[i]; } str[cnt] = '\0'; int n = l1 + l2 + 1; for (int i = 0; i < n; ++i) { d[i] = str[i] - 'a' + 1; } d[n] = 0; Suffix ans; ans.dc3(d, sa, n + 1, 30); ans.calheight(d, sa, n); maxLen = 0, begin = 0; for (int i = 1; i <= n; ++i) { if (sa[i - 1] < l1 && sa[i] > l1) { Manacher(sa[i - 1], height[i]); } if (sa[i - 1] > l1 && sa[i] < l1) { Manacher(sa[i], height[i]); } } for (int i = 0; i < maxLen; ++i) putchar(s1[i + begin]); puts(""); return 0; }
SGU 413
枚举起点,然后任意2个点先成对,剩下的孤立点再投入相邻的集合里
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int MAX_N = 107; int n, m; bool vis[MAX_N]; vector<int> edge[MAX_N], cou[MAX_N]; int bel[MAX_N], sig; void addedge(int u, int v) { edge[u].push_back(v); edge[v].push_back(u); } void clear() { sig = 0; memset(vis, false, sizeof vis); memset(bel, 0, sizeof bel); for (int i = 0; i <= n; ++i) cou[i].clear(); } void dfs(int u) { vis[u] = true; cou[u].push_back(u); for (int i = 0; i < (int) edge[u].size(); ++i) { if (!vis[edge[u][i]]) { dfs(edge[u][i]); if (cou[edge[u][i]].size() == 1) { cou[u].push_back(edge[u][i]); } } } if (cou[u].size() > 1) { ++sig; for (int i = 0; i < (int)cou[u].size(); ++i) { bel[cou[u][i]] = sig; } } } bool check() { for (int i = 0; i < n; ++i) { if (bel[i] == 0) return false; } return true; } int main() { int T; scanf("%d", &T); while (T-- > 0) { scanf("%d%d", &n, &m); for (int i = 0; i < 103; ++i) edge[i].clear(); memset(bel, 0, sizeof bel); for (int i = 0; i < m; ++i) { int u, v; scanf("%d%d", &u, &v); --u, --v; addedge(u, v); } for (int i = 0; i < n; ++i) { clear(); dfs(i); if (check()) { for (int j = 0; j < n; ++j) { if (j == 0) printf("%d", bel[j]); else printf(" %d", bel[j]); } puts(""); break; } } } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。