不知为什么,判断全部包含反而A不了,不判断反而A了,╮(╯▽╰)╭
代码
#include <cstdio> #include <iostream> #define max(x, y) ((x) > (y) ? (x) : (y)) using namespace std; int n, ans; int b[21]; string s[21], st; inline bool check(int i, string dra) { if(!dra.length()) return 1; if(s[i].find(dra) == 0) return 0; if(dra.find(s[i], dra.length() - s[i].length()) == dra.length() - s[i].length()) return 0; return 1; } inline int connect(int p, string dra) { int i, j; for(i = 0; i < s[p].length(); i++) { j = i; while(s[p][j] == dra[dra.length() - 1 - (i - j)]) j--; if(j == -1) return i + 1; } } inline void dfs(string dra) { ans = max(ans, dra.length()); int i, sub; for(i = 1; i <= n; i++) if(b[i] < 2 && /*check(i, dra) &&*/ (sub = connect(i, dra))) { b[i]++; dfs(dra + s[i].substr(sub)); b[i]--; } } int main() { int i; scanf("%d", &n); for(i = 1; i <= n; i++) cin >> s[i]; cin >> st; for(i = 1; i <= n; i++) if(s[i][0] == st[0]) { b[i]++; dfs(s[i]); b[i]--; } printf("%d ", ans); return 0; }