【luogu P1666 前缀单词】 题解

题目链接:https://www.luogu.org/problemnew/show/P1666

10.13考试题

当时没想出来,觉得是要用trie做,在trie上跑一个树形dp

结果是写了个子集枚举还炸了

后来这道题可以暴力+string类型解

还是string用的少啊

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long 
using namespace std;
const int maxn = 100;
string a[maxn];
ll n, ans, f[maxn][maxn], dp[maxn];
bool check(int i, int j)
{
	if(a[i].size() > a[j].size()) swap(i, j);
	if(a[j].find(a[i]) != 0) return 1;
	else return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i = 1; i <= n; i++) cin>>a[i];
	sort(a+1, a+1+n);
	for(int i = 1; i <= n; i++)
	{
		dp[i] = 1;
		for(int j = 1; j <= n; j++) f[i][j] = check(i, j);
	}
	for(int i = 1; i <= n; i++)
		for(int j = i; j <= n; j++)
			if(f[i][j]) dp[j] += dp[i];
			
	for(int i = 1; i <= n; i++)
	ans += dp[i];
	cout<<ans+1;
	return 0;
}
posted @ 2018-10-14 18:21  Misaka_Azusa  阅读(122)  评论(0编辑  收藏  举报
Live2D