• P1019 单词接龙


    P1019 单词接龙

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    string s[25];
    int vis[25];
    char ch;
    int mc[25][25];//重叠的最小部分
    int an;//每次搜到的当前最长长串
    int ans = -1;
    int match(int x, int y) {
        int p = 1;
        int ky = 0;
        for (int k = s[x].size() - 1; k >= 0; k--){
            
            for (int kx = k; kx < s[x].size(); kx++) {
    
                if (s[x][kx] != s[y][ky++]) {
                    p = 0;
                    break;
                }
            }
            if (p)
                return s[x].size() - k;
            ky = 0;
            p = 1;//不行就继续
        }
        return 0;
    }
    void dfs(int p){
        int f = 0;
        for(int i = 1; i <= n; i++){
            if(vis[i] >= 2 || !mc[p][i])//使用了超过两次 没有重合
                continue;
            if(mc[p][i] == s[p].size() || mc[p][i] == s[i].size())//包含关系
                continue;
            an += s[i].size() - mc[p][i];
            vis[i]++;
            f = 1;
            dfs(i);
            an -= s[i].size() - mc[p][i];
            vis[i]--;
        }
        if(!f) ans = max(ans,an);
        return;
    }
    int main(){
        //freopen("in","r",stdin);
        ios::sync_with_stdio(0);
        cin >> n;
        for(int i = 1; i <= n; i++) cin >> s[i];
        cin >> ch;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++)
                mc[i][j] = match(i,j);
        }
        for(int i = 1; i <= n; i++){
            if(s[i][0] == ch){
                vis[i]++;
                an = s[i].size();
                dfs(i);
                vis[i] = 0;
            }
        }
        cout << ans;
        return 0;
    }
    
  • 相关阅读:
    服务器重启后oracle监听无法打开
    Resport 四则运算
    For循环
    do...while循环
    Day03_Class01
    自学JavaDay02_class02
    自学JavaDay02_class01
    自学JavaDay01
    基本的Dos命令
    MarkDown语法
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12301528.html
Copyright © 2020-2023  润新知