解题报告 百进制数

题目 4. 百进制数 (hex.pas/c/cpp) 【问题描述】 科学进步飞快,日新月异,人们早已经不再习惯十进制那种单调的表示数字的方式。最近,Y同学投入百进制数的研究中。两个百进制数可以相邻当且仅当前一个百进制数的最后一位和后一个百进制数的第一位相同,这一位数字称之为一个交点,每一位数字最多能以起点和终点的角色属于交点一次(例如1234—3434—3412,是非法序列,因为34以起点和终点的角色充当交点各两次)。任意一个百进制数或多个可以相邻的百进制数可以形成一个合法序列。一个完美序列满足序列中所有的百进制数长度之和是所有合法序列中最大的。给出n个百进制数,我们希望将其排列才能组成最长的百进制数完美序列。 【输入格式】 第一行一个数n表示百进制数的个数; 第二行到第n-1行每行一个长度为L的百进制数。 【输出格式】 输出完美序列的长度。 【输入样例】 5 1234 347891 1291 9988 9156 【输出样例】 14 【数据范围】 20%的数据:1<=n<=10, 1<=L<=10; 80%的数据:1<=n<=50, 1<=L<=100; 100%的数据:1<=n<=100,1<=L<=100; 算法 可以搜索,深搜,或使用图论。 搜索的时候,只需要注意搜的数字串不要重复,节点不要重复,然后,百进制数一定是两位算一位的,不用枚举是一位算一位的情况。 或使用图论,以开头或结尾的两位作为节点,数字串作为边连边,然后求最长路。 注意事项 注意百进制数两位算一位的。 代码 Dfs (WYH) program hex; type integer=longint; 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 l<=2 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 2=0 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 i<>j then if node[i].en=node[j].st then add(i,j); end; procedure dfs(x,long:integer); var i,j,cc,tt:integer; begin if long>ans then ans:=long; cc:=p[x]; while cc<>0 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,long+node[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:='0'+s; st:=copy(s,1,2); val(st,j); if j=k then begin if len>d1[j] then begin d2[j]:=d1[j]; d1[j]:=len; end else if len>d2[j] then begin d2[j]:=len; end; continue; end; if len>a[j,k] then a[j,k]:=len; end; for i:=0 to 99 do if d2[i]<>0 then if ans<d1[i]+d2[i] then ans:=d1[i]+d2[i]; for k:=0 to 99 do for i:=0 to 99 do if (i<>k)and(a[i,k]>0) then for j:=0 to 99 do if (i<>j)and(j<>k)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 (l<>maxlongint)and(l<>r) then writeln(d1[l]+ans+d1[r]) else writeln(ans); close(input); close(output); end.

原文地址:https://www.cnblogs.com/SueMiller/p/2213688.html