题目求助
查看原帖
题目求助
878110
xushikai楼主2023/9/27 22:37

TLE+RE 15分求助

#include<bits/stdc++.h>
#define _for(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define _all(i, a, b) for(int i = (a); i <= (int)(b); ++i)
#define elif(except) else if(except)
#define ifn(except) if(!(except))
#define lowbit(x) ((x) & (-(x)))
using namespace std;
typedef long long ll, LLONG;
const int N = 32;
int n, ans = 0;
vector<string> G[N];
map<string, bool> Vis;
void dfs(string& s, int cnt = 1) {
	ans = max(ans, (int)(s.size()));
	if(cnt == n) return ;
	_for(i, 1, s.size()) {
		int num = s.size() - i;
		for(auto& p : G[s[i] - 'a']) {
			if((int)(p.size()) <= num) continue;
			if(s.substr(i) == p.substr(0, num)) {
				Vis[p] = 1;
				string t = s + p.substr(num);
				dfs(t, cnt + 1);
				Vis[p] = 0;
			}
		}
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for(string s; n--; G[s[0] - 'a'].push_back(s)) cin >> s;
	char c; cin >> c;
	for(auto& s : G[c - 'a']) {
		Vis[s] = 1;
		dfs(s);
		Vis[s] = 0;
	}
	cout << ans;

	return 0;
}
2023/9/27 22:37
加载中...