解题报告 百进制数
題目 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/archive/2011/10/15/2213688.html
總結
- 上一篇: 大数据思维与数据驱动
- 下一篇: IPhone开发 用子类搞定不同的设备(