1 solutions
-
0
C++ :
#include <iostream> #include <string> #include <limits.h> #include <cstdio> using namespace std; string s[25]; char c; int vis[25]; int m = 0, n; void dfs(string a, int sum) { //cout<<sum<<endl; for (int i=0; i<n; i++) { if (vis[i]<2) { //cout<<a<<" "<<s[i]<<endl; int j, t=0, k, max=0; for (j=1; j<=s[i].size(); j++) { for (k=a.size()-j, t=0; k<a.size() && t<j; t++, k++) if (a[k]!=s[i][t]) break; if (k==a.size()) {max = j; break;} } if (max==0) continue; vis[i]++; dfs(s[i], sum + a.size()-max); vis[i]--; } } sum+=a.size(); if (sum>m) m = sum; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin>>n; for (int i=0; i<n; i++) cin>>s[i]; cin>>c; for (int i=0; i<n; i++) if (s[i][0]==c){ vis[i]++; dfs(s[i], 0); vis[i]--; } cout<<m<<endl; return 0; }Pascal :
var st:array[1..20]of ansistring; a:array[1..20,1..20]of longint; b:array[1..20]of longint; i,j,n,max:longint; ch:char;maxst:ansistring; function f1(st1,st2:ansistring):longint; var i1,max1:longint; begin max1:=0; if (pos(st1,st2)<>0)and(st1<>st2) then exit(0); if (pos(st2,st1)<>0)and(st1<>st2) then exit(0); for i1:=length(st1) downto 1 do if (pos(copy(st1,length(st1)+1-i1,i1),st2)=1) then max1:=i1; f1:=max1; end; procedure work(st0:ansistring;x:longint); var i2:longint; begin i2:=0; for i2:=1 to n do begin if (b[i2]<2)and(a[x,i2]<>0)then begin b[i2]:=b[i2]+1; work(st0+copy(st[i2],a[x,i2]+1,length(st[i2])-a[x,i2]),i2); b[i2]:=b[i2]-1; end; end; if max<length(st0) then begin max:=length(st0);maxst:=st0;end; end; begin readln(n); for i:=1 to n do readln(st[i]); readln(ch); fillchar(a,sizeof(a),0); for i:=1 to n do for j:=1 to n do a[i,j]:=f1(st[i],st[j]); for i:=1 to n do begin if st[i][1]=ch then begin b[i]:=b[i]+1;work(st[i],i);b[i]:=b[i]-1; end;end; writeln(max); end.
- 1
Information
- ID
- 16325
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By