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

参考链接:
[算法]能否成环
能否成环
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
算法描述: 利用快慢指针判断一个链表是否成环,如果快指针能够追上慢指针,则链表存在环。 算法实现: ``` #include<stdio.h> #include<stdlib.h> typedef struct Node{ int data; struct Node* next; }Node; // 创建链表,返回头结点 Node* createList(int* arr, int n){ Node* head = (Node*)malloc(sizeof(Node)); head->data = -1; head->next = NULL; Node* p = head; for(int i = 0; i < n; i++){ Node* node = (Node*)malloc(sizeof(Node)); node->data = arr[i]; node->next = NULL; p->next = node; p = p->next; } return head; } // 判断链表是否成环 int isCircle(Node* head){ Node* slow = head->next; Node* fast = head->next; while(fast != NULL && fast->next != NULL){ slow = slow->next; fast = fast->next->next; if(slow == fast){ return 1; } } return 0; } // 主函数 int main(){ int arr[] = {1,2,3,4,5}; int n = sizeof(arr)/sizeof(int); Node* head = createList(arr, n); //将链表的最后一个节点的指针指向第二个节点,形成环 Node* tail = head; while(tail && tail->next){ tail = tail->next; } tail->next = head->next->next; if(isCircle(head)){ printf("链表成环!\n"); }else{ printf("链表未成环!\n"); } return 0; } ``` 算法解释: 该算法利用了快慢指针的特性,在遍历链表的过程中,慢指针每次向后移动一步,快指针每次向后移动两步,如果链表成环,那么快指针最终将追上慢指针。如果链表不成环,那么快指针最终将指向NULL,整个遍历过程将结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值