1 条题解
-
0
C++ :
#include <iostream> #include <string> #include <limits.h> #include <cstdio> using namespace std; string s[25]; char c; int n,x[25],m=0; void y(string a,int sum) { unsigned int i,j,k,t; for(i = 0;i<n; i++) { if(x[i]<2) { int max=0; for(j=1;j<=s[i].size();j++) { if(a.size()<s[i].size()) { for(k=0;k<a.size();k++) if(a[k]!=s[i][k])break; if(k ==a.size())break; } for(k=a.size()-j,t=0;t<j&&k<a.size();t++,k++) { if(a[k]!=s[i][t]) break; } if(k==a.size()) { max=j; break; } } if(max == 0) continue; x[i]++; y(s[i],sum+a.size()-max); x[i]--; } } sum+=a.size(); if(sum>m)m=sum; } int main() { int i; cin>>n; for(i=0;i<n;i++) cin>>s[i]; cin>>c; for(i=0;i<n;i++) { if(s[i][0]==c) { x[i]++; y(s[i],0); x[i]--; } } cout<<m<<endl; return 0 ; }
Pascal :
program shdj; var a:array[1..21] of integer; i,m,n,ans,k:longint; b:array[1..21] of string; s,dra:string; c:char; function check(s1,s2:string):boolean; var j:longint; begin check:=false; for j:=length(s2) downto 1 do if (pos(copy(s2,j,length(s2)-j+1),s1)=1) and ((pos(s1,s2)=0) or (s1=s2)) then begin check:=true; k:=length(s2)-j+1; exit; end; end; procedure work(best:longint); var i,j,m:longint; begin for i:=1 to n+1 do begin if i=n+1 then begin if best>ans then ans:=best;exit;end; if (a[i]<2) and check(b[i],dra) then begin m:=k;dra:=copy(dra,length(dra)-m+1,m)+b[i];a[i]:=a[i]+1; work(best+length(b[i])-m); delete(dra,length(dra)-(length(b[i])-m)+1,length(b[i])-m); //回溯 dec(a[i]); end; end; end; begin readln(n); for i:=1 to n do readln(b[i]); readln(c); for i:=1 to n do begin dra:=b[i]; if b[i,1]=c then begin fillchar(a,sizeof(a),0); inc(a[i]); work(length(b[i])); end; end; writeln(ans); end.
Java :
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String args[]) throws Exception { Scanner scanner = new Scanner(System.in); Integer num = Integer.parseInt(scanner.nextLine()); List<String> list = new ArrayList<String>(); int count = 0; while (scanner.hasNextLine()) { count++; String input = scanner.nextLine(); if (input == null || input.equals("") || input.length() == 0) { break; } list.add(input); list.add(input); if (count >= num) { break; } } String dragon = scanner.nextLine(); int maxlen = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).startsWith(dragon)) { List<String> subList = new ArrayList<String>(); subList.addAll(list.subList(0, i)); subList.addAll(list.subList(i + 1, list.size())); String ret = fun(list.get(i), list.get(i), subList); if (ret.length() > maxlen) { maxlen = ret.length(); } } } System.out.println(maxlen); } public static String fun(String dragon, String tail, List<String> wordList) { String maxDragon = dragon; for (int i = 0; i < wordList.size(); i++) { if (match(tail, wordList.get(i))) { String dragonThis = connect(dragon, wordList.get(i)); List<String> subWordList = new ArrayList<String>(); int findi = 0; for (int j = 0; j < wordList.size(); j++) { if (wordList.get(j).equals(wordList.get(i))) { findi = j; break; } } subWordList.addAll(wordList.subList(0, findi)); subWordList .addAll(wordList.subList(findi + 1, wordList.size())); dragonThis = fun(dragonThis, wordList.get(i), subWordList); if (dragonThis.length() > maxDragon.length()) { maxDragon = dragonThis; } } } return maxDragon; } public static boolean match(String start, String word) { boolean flag = false; for (int i = start.length() - 1; i >= 1; i--) { if (word.startsWith(start.substring(i, start.length()))) { flag = true; break; } } return flag; } public static String connect(String dragon, String tail) { for (int i = dragon.length() - 1; i >= 1; i--) { if (tail.startsWith(dragon.substring(i, dragon.length()))) { return dragon.substring(0, i) + tail; } } return "WTF!"; } }
- 1
信息
- ID
- 202
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者