(Query set + Euler's path) play on words

Source: Internet
Author: User
Tags define local

Question:

There are n words separated by lowercase letters. It is required to determine whether there are two adjacent words in a certain arrangement. The last letter of the first word is the same as the first letter of the last word.

Analysis:

When two letters of a word are considered as nodes, a word can be considered as a directed edge. The arrangement in the question is equivalent to the existence of Euler's path in the directed graph.

Before judging, you must first make sure that the graph is connected, and use the query set in the code.

Let's look back at the conditions with Euler's path. They are all even points or there are only two singularity. DEG is used to record the degree of each vertex. the outbound degree is 1, and the inbound degree is-1.

The condition in the program for determining the existence of Euler's path is: DEG is all 0 or there are two not 0, one of which is 1 and the other is-1.

Used records whether a letter has appeared.

 

 1 //#define LOCAL 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6  7 const int maxn = 1000 + 10; 8 char word[maxn]; 9 int pa[256], deg[256], cc, used[256];10 11 int find(int a)12 { return pa[a] == a ? a : pa[a] = find(pa[a]); }13 14 int main(void)15 {16     #ifdef LOCAL17         freopen("10129in.txt", "r", stdin);18     #endif19 20     int T, n;21     scanf("%d", &T);22     while(T--)23     {24         memset(used, 0, sizeof(used));25         memset(deg, 0, sizeof(deg));26         for(int i = ‘a‘; i <= ‘z‘; ++i)27             pa[i] = i;28         cc = 26;    //Á¬Í¨¿éµÄÊýÁ¿ 29 30         scanf("%d", &n);31         for(int i = 0; i < n; ++i)32         {33             scanf("%s", word);34             char c1 = word[0];35             char c2 = word[strlen(word) - 1];36             used[c1] = used[c2] = 1;37             deg[c1]++;    deg[c2]--;38             int p1 = find(c1);39             int p2 = find(c2);40             if(p1 != p2)41             {42                 cc--;43                 pa[p1] = p2;44             }45         }46 47         vector<int> d;48         for(int i = ‘a‘; i <= ‘z‘; ++i)49         {50             if(!used[i]) --cc;51             else if(deg[i])    d.push_back(i);52         }53         bool ok = false;54         if(cc == 1 && (d.empty() || (d.size() == 2 && (deg[d[0]] == 1 || deg[d[0]] == -1)))) ok = true;55         if(ok)    puts("Ordering is possible.");56         else puts("The door cannot be opened.");57     }58 59     return 0;60 }
Code Jun

 

(Query set + Euler's path) play on words

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.