2018年9月15日 星期六

itsa66 5Find the Sequence Pattern

1.找最長共同子字串,用長度小的找長度長的,長度2以上
2.最長共同子字串可以有很多個,要去掉重複的
3.輸出要按字母順序排列

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<string>
using namespace std;

int main()
{
 string s1, s2, lstr, sstr;
 string s[100];
 int a, b, x;
 while (getline(cin, s1))
 {
  bool ischeck = false;
  b = 0;
  x = 0;
  getline(cin, s2);
  lstr = s1;
  sstr = s2;
  if (s1.length() < s2.length())
  {
   lstr = s2;
   sstr = s1;
  }
  for (int j = sstr.length(); j > 1; j--)
  {
   a = 0;
   if (j != b && ischeck)
    break;
   for (int i = 0; i <= sstr.length() - j; i++)
   {
    if (string::npos != lstr.find(sstr.substr(i, j)))
    {
     for (int k = 0; k < x; k++)
      if (s[k].compare(sstr.substr(i, j)) == 0)
       a = 1;
     if (a == 0)
     {
      s[x++] = sstr.substr(i, j);
      b = j;
      ischeck = true;
     }
    }
   }
  }
  if (ischeck)
  {
   if (x > 1)
   {
    for (int i = 0; i < x - 1; i++)
    {
     for (int j = i + 1; j < x; j++)
     {
      if (s[i].compare(s[j]) > 0)
      {
       string tmp = s[i];
       s[i] = s[j];
       s[j] = tmp;
      }
     }
    }
   }
   for (int i = 0; i < x; i++)
    cout << s[i] << endl;
  }
  else
   cout << "No common sequence!" << endl;
 }
 return 0;
}

沒有留言:

張貼留言