Luo Gu P1019 [NOIP2000] word Solitaire (DFS)

Jump meaning of problem-solving

Analysis: It is a simple question of DFS, DFS idea is exhaustive, and that we put all possible Rendezvous exhaustive out, take the longest,

The difficulty is string matching, i.e. a string connected behind the other string code.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

char s [ 50 ] [ 50 ];
int n, ans, len = 1 ;
int manner [ 50 ];

inline void dfs(int k)
{
    ans=max(ans,len);
    for(int i=1;i<=n;i++)
    {
        if(vis[i]<2)
        {
            for(int j=0;j<strlen(s[k]);j++)
            {
                if(s[i][0]==s[k][j])
                {
                    int cnt1=0,cnt2=j;
                    while(s[i][cnt1]==s[k][cnt2]&&cnt2<strlen(s[k])) cnt1++,cnt2++;
                    if(cnt2==strlen(s[k]))
                    {
                        view [i] ++ ;
                        only + = strlen (s [i]) - CNT1;
                        dfs(i);
                        view [i] - ;
                        only - = strlen (s [i]) - CNT1;
                    }
                }
            }
        }
    }
}

int main ()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        cin>>s[i];
    cin>>s[0];
    dfs(0);
    printf("%d",ans);
    return 0;
}
P1019 code

 

Guess you like

Origin www.cnblogs.com/Hoyoak/p/11367399.html