[NOIP] word Solitaire

[NOIP] word Solitaire

topic

Title Description

Solitaire Solitaire is a word we used to play with the idiom of similar games, and now we know a set of words, and given a letter that begins, requiring the longest "Dragon" (each word beginning with this letter are up to appear in the "dragon" twice), when connected to two words, which makes part of the overlapping portions together, and beast Astonish e.g., if connected to the train becomes beastonish, two additional portions adjacent relationship can not exist comprising , and for example, can not be connected between at atide.

Input Format

The first act input a single integer n (0n≤20) represents the number of words, the following n lines each have a word, the last act input of a single character, represents the letter "Dragon" at the beginning. You can assume that this begins with the letter "dragon" must exist.

Output Format

Just output begins with this letter the longest "Dragon" in length

Sample input and output

Input # 1 replication
. 5
AT
Touch
Cheat
the Choose
TACT
A
output copy # 1
23

Description / Tips

(Even as the "dragon" is atoucheatactactouchoose)

NOIp2000 improve the Group III title

analysis

This problem solution saw a lot of big brother, I feel better understand this, but super super detailed. (Directly on the Gangster solution .........)

Here Insert Picture Description
Here Insert Picture Description

Code

Gangster code. .

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define maxn 100
using namespace std;
int n;
int ans = 0;
string word[maxn];//字符串数组,用来存储单词
string beginn;//用来存储开头字符
int used[maxn];//这个就是用来记录dfs时候每一个单词被使用了几次的数组

bool check(string s,string m,int k){//重点一,check函数判断接口可行性,k代表接口长度,以下同
    int lens = s.length();
    for (int i=0;i<k;i++){
        if(s[lens-k+i]!=m[i])//我讲过了
            return false;
    }
    return true;
}

void add(string &s,string m,int k){//拼接操作,因为要把m接到s上,所以对于s串不可以传参,因为我们要试图改变这个串
    int lenm = m.length();
    for (int i=k;i<lenm;i++)
        s+=m[i];//C++字符串类型特性操作,支持+=符号直接拼接
}

void dfs(string now){//这只是一个看似平淡无奇的dfs
    int x = now.length();
    ans = max(ans,x);//每次拼接之后更新一下答案
    for (int i=1;i<=n;i++){
        if (used[i]>=2)//如果有一个单词用完了,那这个单词就不能选了
            continue;
        int maxk = word[i].length();
        for (int j=1;j<=maxk;j++){//枚举接口长度
            if (check(now,word[i],j)){
                string temp = now;//重点二,使用字符串副本进行拼接
                add(temp,word[i],j);
                if (temp==now)//拼完之后如果发现长度没增加,也就是和原串一样,那这次拼接没有意义,剪掉
                    continue;
                used[i]++;
                dfs(temp);
                used[i]--;//这只是一个看似平淡无奇的回溯
            }
        }
    }

}

int main(){
    cin >> n;
    for (int i=1;i<=n;i++)
        cin >> word[i];
    cin >> beginn;
    dfs(beginn);

    cout << ans << endl;
    return 0;

}
Published 75 original articles · won praise 1 · views 3649

Guess you like

Origin blog.csdn.net/A793488316/article/details/104623787