网站毕设代做,电商运营培训哪个机构好,网站平台建设是什么,邢台企业网站制作公司题目 4. 百进制数 #xff08;hex.pas/c/cpp#xff09; 【问题描述】 科学进步飞快#xff0c;日新月异#xff0c;人们早已经不再习惯十进制那种单调的表示数字的方式。最近#xff0c;Y同学投入百进制数的研究中。两个百进制数可以相邻当且仅当前一个百进制数的最后一位…题目 4. 百进制数 hex.pas/c/cpp 【问题描述】 科学进步飞快日新月异人们早已经不再习惯十进制那种单调的表示数字的方式。最近Y同学投入百进制数的研究中。两个百进制数可以相邻当且仅当前一个百进制数的最后一位和后一个百进制数的第一位相同这一位数字称之为一个交点每一位数字最多能以起点和终点的角色属于交点一次例如1234—3434—3412是非法序列因为34以起点和终点的角色充当交点各两次。任意一个百进制数或多个可以相邻的百进制数可以形成一个合法序列。一个完美序列满足序列中所有的百进制数长度之和是所有合法序列中最大的。给出n个百进制数我们希望将其排列才能组成最长的百进制数完美序列。 【输入格式】 第一行一个数n表示百进制数的个数 第二行到第n-1行每行一个长度为L的百进制数。 【输出格式】 输出完美序列的长度。 【输入样例】 5 1234 347891 1291 9988 9156 【输出样例】 14 【数据范围】 20%的数据1n10, 1L10; 80%的数据1n50, 1L100; 100%的数据1n100,1L100; 算法 可以搜索深搜或使用图论。 搜索的时候只需要注意搜的数字串不要重复节点不要重复然后百进制数一定是两位算一位的不用枚举是一位算一位的情况。 或使用图论以开头或结尾的两位作为节点数字串作为边连边然后求最长路。 注意事项 注意百进制数两位算一位的。 代码 Dfs WYH program hex; type integerlongint; var clist:array[0..100005] of record f,t,l:integer; end; p:array[0..105] of integer; vis,viss:array[0..105] of boolean; node:array[0..105] of record st,en,len:integer; end; n,count,ans:integer; procedure init; var i,j,l,temp:integer; s:string; begin count:0; ans:0; fillchar(clist,sizeof(clist),0); fillchar(p,sizeof(p),0); fillchar(vis,sizeof(vis),0); fillchar(node,sizeof(node),0); readln(n); for i:1 to n do begin readln(s); l:length(s); node[i].len:l; if l2 then begin val(s,temp); node[i].st:temp; node[i].en:temp; end else begin val(copy(s,l-1,2),temp); node[i].en:temp; if l mod 20 then begin val(copy(s,1,2),temp); node[i].st:temp; end else begin val(copy(s,1,1),temp); node[i].st:temp; end; end; end; end; procedure add(ff,tt:integer); begin inc(count); clist[count].f:ff; clist[count].t:tt; clist[count].l:p[ff]; p[ff]:count; end; procedure makepicture; var i,j:integer; begin for i:1 to n do for j:1 to n do if ij then if node[i].ennode[j].st then add(i,j); end; procedure dfs(x,long:integer); var i,j,cc,tt:integer; begin if longans then ans:long; cc:p[x]; while cc0 do begin tt:clist[cc].t; if (not vis[tt]) and (not viss[node[tt].st]) then begin vis[tt]:true; viss[node[tt].st]:true; dfs(tt,longnode[tt].len); vis[tt]:false; viss[node[tt].st]:false; end; cc:clist[cc].l; end; vis[x]:false; end; procedure main; var i,j:integer; begin for i:1 to n do begin fillchar(vis,sizeof(vis),0); fillchar(viss,sizeof(viss),0); vis[i]:true; dfs(i,node[i].len); end; writeln(ans); end; begin assign(input,hex.in);reset(input); assign(output,hex.out);rewrite(output); init; makepicture; main; close(input); close(output); End. 图论 LYF var d1,d2:array[0..100] of longint; a:array[0..100,0..100] of longint; s:string; ans,l,r,len,n,i,j,k:longint; st:string; begin assign(input,hex.in); reset(input); assign(output,hex.out); rewrite(output); readln(n); for i:1 to n do begin readln(s); len:length(s); st:copy(s,len-1,2); val(st,k); if odd(len) then s:0s; st:copy(s,1,2); val(st,j); if jk then begin if lend1[j] then begin d2[j]:d1[j]; d1[j]:len; end else if lend2[j] then begin d2[j]:len; end; continue; end; if lena[j,k] then a[j,k]:len; end; for i:0 to 99 do if d2[i]0 then if ansd1[i]d2[i] then ans:d1[i]d2[i]; for k:0 to 99 do for i:0 to 99 do if (ik)and(a[i,k]0) then for j:0 to 99 do if (ij)and(jk)and(a[k,j]0)and(a[j,k]0) then if a[i,j]a[i,k]a[k,j] then a[i,j]:a[i,k]a[k,j]; l:maxlongint; for i:1 to 99 do for j:1 to 99 do if a[i,j]ans then begin ans:a[i,j]; l:i; r:j; end; if (lmaxlongint)and(lr) then writeln(d1[l]ansd1[r]) else writeln(ans); close(input); close(output); end.转载于:https://www.cnblogs.com/SueMiller/archive/2011/10/15/2213688.html