求助,数据在本地AC提交WA
查看原帖
求助,数据在本地AC提交WA
459188
zrt090604楼主2024/11/22 09:24
#include<bits/stdc++.h>
using namespace std;
int n, a[21][21], vis[21], ans;
string s[21];
char ch;
int calc(int x, int y) {
	int len = min(s[x].size(), s[y].size())-1, mx = 0;
	for(int i = 1;i <= len;++i) {
		string s1 = s[x].substr(s[x].size()-i, i);
		string s2 = s[y].substr(0, i);
		if(s1 == s2) mx = i;
	}
	return mx;
}
void dfs(int id, int len) {
	for(int i = 1;i <= n;++i) {
		if(vis[i] && a[id][i]) {
			--vis[i];
			dfs(i, len+s[i].size()-a[id][i]);
			++vis[i];
		}
	}
	ans = max(ans, len);
	return;
}
int main () {
    scanf("%d", &n);
    for(int i = 1;i <= n;++i) cin >> s[i];
    for(int i = 1;i <= n;++i)
    	for(int j = 1;j <= n;++j) 
    		a[i][j] = calc(i, j);
	getchar();
	scanf("%c", &ch);
    for(int i = 1;i <= n;++i)
    	if(s[i][0] == ch) {
    		fill(vis+1, vis+n+1, 2), vis[i] = 1;
    		dfs(i, s[i].size());
		}
	printf("%d", ans);
    return 0;
}
2024/11/22 09:24
加载中...