高级算法课后题——能否成环

参考链接:
[算法]能否成环
能否成环
Description

Given an array of strings A[ ], determine if the strings can be chained together to form a circle. A string X can be chained together with another string Y if the last character of X is same as first character of Y. If every string of the array can be chained, it will form a circle. For example, for the array arr[] = {“for”, “geek”, “rig”, “kaf”} the answer will be Yes as the given strings can be chained as “for”, “rig”, “geek” and “kaf”.

Input

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.

The first line of each test case contains a positive integer N, denoting the size of the array.

The second line of each test case contains a N space seprated strings, denoting the elements of the array A[ ].

1 <= T

0 < N

0 < A[i]

Output

If chain can be formed, then print 1, else print 0.

用到DFS,另外注意一些特殊情况:

Input: arr[] = {"geek", "king"}
Output: Yes, the given strings can be chained.
Note that the last character of first string is same
as first character of second string and vice versa is
also true.

Input: arr[] = {"for", "geek", "rig", "kaf"}
Output: Yes, the given strings can be chained.
The strings can be chained as "for", "rig", "geek" 
and "kaf"

Input: arr[] = {"aab", "bac", "aaa", "cda"}
Output: Yes, the given strings can be chained.
The strings can be chained as "aaa", "aab", "bac" 
and "cda"

Input: arr[] = {"aaa", "bbb", "baa", "aab"};
Output: Yes, the given strings can be chained.
The strings can be chained as "aaa", "aab", "bbb" 
and "baa"

Input: arr[] = {"aaa"};
Output: Yes

Input: arr[] = {"aaa", "bbb"};
Output: No

Input  : arr[] = ["abc", "efg", "cde", "ghi", "ija"]
Output : Yes
These strings can be reordered as, “abc”, “cde”, “efg”,
“ghi”, “ija”

Input : arr[] = [“ijk”, “kji”, “abc”, “cba”]  
Output : No

完整代码:

```cpp
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>
#include<sstream>
using namespace std;

bool isAllVisit(vector<bool> isVisit) {
   //判断是否所有点都遍历过,如果是则结束
         int flag=0;
         for (int j = 0; j < isVisit.size(); j++)
         {
            if(isVisit[j]==false) {
               flag=1;
               break;
            }
         }
         if(flag==0) {
            return true;
         }
         return false;
}

bool DFS(vector<string> &vec,vector<bool> isVisit,char start,char end) {
   bool isEnded=false;
      while(!isEnded) {
         vector<int> possibleNext;
         for (int j = 0; j < vec.size(); j++)
         {
            if(vec[j][0]==end&&isVisit[j]==false) {
               possibleNext.push_back(j);
               // end=vec[j][vec[j].length()-1];
               // isVisit[j]=true;
            }
         }
         if(possibleNext.size()==0) {
            return false;
         }  else {
            for (int k = 0; k < possibleNext.size(); k++)
            {
               char nextEnd=vec[possibleNext[k]][vec[possibleNext[k]].size()-1];
               isVisit[possibleNext[k]]=true;
               if(nextEnd==start&&isAllVisit(isVisit)) {
                  return true;
               }
               
               if(DFS(vec,isVisit,start,nextEnd)) {
                  return true;
               }
            }
         }
         if(isAllVisit(isVisit)) {
            return false;
            } 
      }
}

int main()
{
    int T;
    cin>>T;
    cin.get();
    for (int i = 0; i < T; i++)
    {
       int n;
       string temp;
       vector<string> vec;
       cin>>n;
       for (int j = 0; j < n; j++)
       {
          cin>>temp;
          vec.push_back(temp);
       }
       if(n==1&&vec[0][0]==vec[0][vec[0].length()-1]) {
          cout<<"1"<<endl;
          continue;
       } 
       vector<bool> isVisit(n,false);
      char start,end;
      start=vec[0][0];
      end=vec[0][vec[0].length()-1];
      isVisit[0]=true;
      if(DFS(vec,isVisit,start,end)) {
         cout<<"1"<<endl;
      } else {
         cout<<"0"<<endl;
      }
    }
      // system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值