P1278 Word Games-DFS

Source: Internet
Author: User

P1278 Word Game Portal Sol:

Enumerate each word in the dictionary, and then run DFS. Adding a memory will not be a T.

AC Code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 16 + 4,M = 100 + 10;int n;char S[N][M];int len[N];int f[N][1<<N];int dfs(int x,int ud){    if(f[x][ud]) return f[x][ud];    for(int i=0;i<n;i++){        if((ud>>i)&1) continue;        if(S[x][len[x]]!=S[i][1]) continue;        f[x][ud]=max(f[x][ud],dfs(i,ud|(1<<i))+len[i]);    }    return f[x][ud];}int main(){    scanf("%d",&n);    for(int i=0;i<n;i++){        scanf("%s",S[i]+1);len[i]=strlen(S[i]+1);    }    int ans=-(1<<30);    for(int i=0;i<n;i++){        ans=max(ans,dfs(i,(1<<i))+len[i]);    }    printf("%d",ans);    return 0;}

P1278 Word Games-DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.