朴素dfs,TLE一个点

评测结果
编译成功

测试数据 #0: Accepted, time = 0 ms, mem = 796 KiB, score = 12
测试数据 #1: Accepted, time = 0 ms, mem = 808 KiB, score = 12
测试数据 #2: Accepted, time = 0 ms, mem = 804 KiB, score = 12
测试数据 #3: Accepted, time = 0 ms, mem = 808 KiB, score = 12
测试数据 #4: Accepted, time = 0 ms, mem = 808 KiB, score = 12
测试数据 #5: Accepted, time = 15 ms, mem = 808 KiB, score = 12
测试数据 #6: TimeLimitExceeded, time = 1015 ms, mem = 816 KiB, score = 0
测试数据 #7: Accepted, time = 0 ms, mem = 824 KiB, score = 12
TimeLimitExceeded, time = 1030 ms, mem = 824 KiB, score = 84
代码
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
map<string,map<string,bool> > edges;
map<string,map<string,int> > g;
map<string,int> vis;
string word[21];
int n,ans = 0,Max = 0;
inline void dfs(string now,string str,int sum) {
    for (int i = 1;i <= n;i++)
        if (vis[word[i]] && edges[now][word[i]]) {
            vis[word[i]]--;
            dfs(word[i],str+word[i],sum+g[now][word[i]]);
            vis[word[i]]++;
        }
    Max = max(Max,(int)str.length()-sum);
}
int main() {
    scanf("%d\n",&n);
    for (int i = 1;i <= n;i++) {
        getline(cin,word[i]);
        vis[word[i]] = 2;
    }
    for (int i = 1;i <= n;i++)
        for (int j = 1;j <= n;j++) {
            int tmp = min(word[i].length(),word[j].length())-1;
            bool flag = false;
            for (int k = 0;k < tmp;k++) {
                string cmp1,cmp2;
                for (int l = 0;l <= k;l++) cmp1 += word[j][l];
                for (unsigned l = word[i].length()-1-k;l <= word[i].length()-1;l++) cmp2 += word[i][l];
                if (cmp1 == cmp2) {
                    flag = true;
                    g[word[i]][word[j]] = k+1;
                    break;
                }
            }
            edges[word[i]][word[j]] = flag;
            if (!flag) g[word[i]][word[j]] = -1;
        }
    char ch = getchar();
    for (int i = 1;i <= n;i++)
        if (word[i][0] == ch) {
            vis[word[i]]--;
            dfs(word[i],word[i],0);
            ans = max(ans,Max);
            vis[word[i]]++;
        }
    printf("%d",ans);
    return 0;
}

0 条评论

目前还没有评论...

信息

ID
1311
难度
5
分类
搜索 点击显示
标签
递交数
3155
已通过
1011
通过率
32%
被复制
15
上传者