Hướng dẫn giải của VM 13 Bài 21 - Vườn cây của ba
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int n, Q, id[256], flag[256], cnt[55][55]; char s[1000100], t[100]; int main() { for (char c = 'a'; c <= 'z'; c++) id[c] = c - 'a'; for (char c = 'A'; c <= 'Z'; c++) id[c] = c - 'A' + 26; scanf("%d%s", &n, s); for (int i = 1; i < n; i++) { int x = id[s[i - 1]], y = id[s[i]]; if (x > y) swap(x, y); cnt[x][y]++; } scanf("%d", &Q); for (int iq = 1; iq <= Q; iq++) { scanf("%s", t); int len = strlen(t); for (int i = 0; i < len; i++) flag[id[t[i]]] = iq; int ans = 0; for (int i = 0; i < 52; i++) for (int j = i + 1; j < 52; j++) if ((flag[i] == iq) ^ (flag[j] == iq)) ans += cnt[i][j]; if (flag[id[s[0]]] == iq) ans = ans / 2 + 1; else ans = (ans + 1) / 2; printf("%d\n", ans); } }
Code mẫu của ladpro98
program VMSUBSTR; uses math; const fi=''; var n,qq,t,i,res:longint; a,s:ansistring; p,q:char; chk:array['A'..'z'] of boolean; c:array['A'..'z','A'..'z'] of longint; inp:text; begin assign(inp,fi);reset(inp); readln(inp,n);readln(inp,a); for i:=2 to n do begin inc(c[a[i],a[i-1]]); inc(c[a[i-1],a[i]]); end; readln(inp,qq); for t:=1 to qq do begin readln(inp,s); res:=0; for i:=1 to length(s) do chk[s[i]]:=true; if chk[a[1]] then inc(res); if chk[a[length(a)]] then inc(res); for p:='A' to 'z' do for q:=chr(ord(p)+1) to 'z' do if chk[p]<>chk[q] then inc(res,c[p,q]); writeln(res shr 1); for i:=1 to length(s) do chk[s[i]]:=false; end; end.
Code mẫu của RR
// Author: RR. Expected point: 100 #include <sstream> #include <iomanip> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <deque> #include <complex> #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++) #define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--) #define SET(a,v) memset(a,v,sizeof(a)) #define sqr(x) ((x)*(x)) #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define DEBUG(x) cout << #x << " = "; cout << x << endl; #define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; #define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; using namespace std; //Buffer reading int INP,AM,REACHEOF; const int BUFSIZE = (1<<12) + 17; char BUF[BUFSIZE+1], *inp=BUF; #define GETCHAR(INP) { \ if(!*inp && !REACHEOF) { \ memset(BUF,0,sizeof BUF);\ int inpzzz = fread(BUF,1,BUFSIZE,stdin);\ if (inpzzz != BUFSIZE) REACHEOF = true;\ inp=BUF; \ } \ INP=*inp++; \ } #define DIG(a) (((a)>='0')&&((a)<='9')) #define GN(j) { \ AM=0;\ GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\ if (INP=='-') {AM=1;GETCHAR(INP);} \ j=INP-'0'; GETCHAR(INP); \ while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \ if (AM) j=-j;\ } //End of buffer reading const long double PI = acos((long double) -1.0); const int MN = 1000111; int l, q; char s[MN], a[55]; bool good[55]; int cnt[55][55]; int getVal(char c) { if (c >= 'a' && c <= 'z') return c - 'a'; else return c - 'A' + 26; } void init() { REP(i,l-1) { int u = getVal(s[i]), v = getVal(s[i+1]); cnt[u][v]++; } } int main() { scanf("%d\n", &l); gets(s); init(); scanf("%d\n", &q); while (q--) { gets(a); memset(good, false, sizeof good); REP(i,strlen(a)) good[getVal(a[i])] = true; int res = 0; REP(x,52) if (good[x]) REP(y,52) if (!good[y]) res += cnt[x][y]; if (good[getVal(s[l-1])]) ++res; printf("%d\n", res); } return 0; }
Bình luận