1528. Ïîäñ÷åò îáùèõ ïîäïîñëåäîâàòåëüíîñòåé

 

Ïîäïîñëåäîâàòåëüíîñòü îáðàçóåòñÿ èç ñòðîêè óäàëåíèåì íóëÿ èëè íåñêîëüêèõ ñèìâîëîâ èç íåå. Ïî çàäàííûì òðåì ñòðîêàì Âàì ñëåäóåò ïîäñ÷èòàòü êîëè÷åñòâî èõ ðàçíûõ íåïóñòûõ îáùèõ ïîäïîñëåäîâàòåëüíîñòåé.

 ïðèìåðå 1 îáùèìè 6 ïîäïîñëåäîâàòåëüíîñòÿìè áóäóò: "c", "a", "l", "al", "ca" è "cl".

 

Âõîä. Êàæäûé òåñò ñîñòîèò èç òðåõ ñëîâ, êîòîðûå íàõîäÿòñÿ â òðåõ ðàçíûõ ñòðîêàõ. Äëèíà êàæäîãî ñëîâà íå áîëåå 50. Êàæäîå ñëîâî ñîñòîèò òîëüêî èç ëàòèíñêèõ áóêâ íèæíåãî ðåãèñòðà ('a' - 'z').

 

Âûõîä. Äëÿ êàæäîãî òåñòà âûâåñòè â îòäåëüíîé ñòðîêå êîëè÷åñòâî ðàçíûõ íåïóñòûõ îáùèõ ïîäïîñëåäîâàòåëüíîñòåé.

 

Ïðèìåð âõîäà

Ïðèìåð âûõîäà

call

accelerate

candle

no

correct

answer

6

0

 

 

 

ÐÅØÅÍÈÅ

äèíàìè÷åñêîå ïðîãðàììèðîâàíèå

 

Àíàëèç àëãîðèòìà

 ÿ÷åéêå f[i][j][k] áóäåì õðàíèòü êîëè÷åñòâî îáùèõ ïîäïîñëåäîâàòåëüíîñòåé ñòðîê a1a2ai, b1b2bj è c1c2ck., ãäå ai = bj = ck. Åñëè äëÿ òðîéêè (i, j, k) ïîñëåäíåå ðàâåíñòâî íå âûïîëíÿåòñÿ, òî ïîëàãàåì f[i][j][k] = 0. Åñëè i = 0, òî ñ÷èòàåì, ÷òî ñòðîêà a ïóñòàÿ. Àíàëîãè÷íî ïðè j = 0 (k = 0) ñ÷èòàåì, ÷òî ñòðîêà b (c) ïóñòàÿ. Ïîëîæèì f[0][0][0] = 1, ñ÷èòàÿ, ÷òî îáùåé ïîäïîñëåäîâàòåëüíîñòüþ òðåõ ïóñòûõ ñòðîê ÿâëÿåòñÿ ïóñòàÿ ñòðîêà.

Èñêîìîå êîëè÷åñòâî îáùèõ ïîäïîñëåäîâàòåëüíîñòåé òðåõ ñòðîê a, b è c ðàâíî

 èëè  – 1

ãäå l, m, n – äëèíû ñîîòâåòñòâåííî ñòðîê a, b è c.

Âû÷èñëåíèå çíà÷åíèé f[i][j][k] ñîâåðøàåì ñëåäóþùèì îáðàçîì. Ïåðåáèðàåì âñå âîçìîæíûå çíà÷åíèÿ òðîåê (i, j, k) è äëÿ êàæäîãî çíà÷åíèÿ f[i][j][k], áîëüøåãî íóëÿ (ai = bj = ck) ïåðåáèðàåì âñå áóêâû ëàòèíñêîãî àëôàâèòà è äëÿ êàæäîé áóêâû èùåì åå ïîÿâëåíèå âî âñåõ ñòðîêàõ â ïîçèöèÿõ, áîëüøèõ ñîîòâåòñòâåííî i, j è k. Ïóñòü òàêèìè ïîçèöèÿìè áóäóò x, y è z. Äîáàâëÿåì ê f[x+1][y+1][z+1] çíà÷åíèå f[i][j][k] (èíäåêñû ñäâèíóòû íà 1, òàê êàê íóìåðàöèÿ ìàññèâîâ â Ñè íà÷èíàåòñÿ ñ íóëÿ, à èíäåêñó 0 â ìàññèâå f ñîîòâåòñòâóåò ïóñòàÿ ñòðîêà).

 

Ïðèìåð 1. Ïóñòü a = b = c = “abc”. Òîãäà

f[0][0][0] = 1, îáùàÿ ïîäïîñëåäîâàòåëüíîñòü (“”, “”, “”).

f[1][1][1] = 1, îáùàÿ ïîäïîñëåäîâàòåëüíîñòü (“a”, “a”, “a”).

f[2][2][2] = 2, îáùèå ïîäïîñëåäîâàòåëüíîñòè (“b”, “b”, “b”), (“ab”, “ab”, “ab”).

f[3][3][3] = 4, îáùèå ïîäïîñëåäîâàòåëüíîñòè (“c”, “c”, “c”), (“bc”, “bc”, “bc”),

                                                                             (“ac”, “ac”, “ac”), (“abc”, “abc”, “abc”).

Îñòàëüíûå çíà÷åíèÿ f[i][j][k] ðàâíû íóëþ.

Êîëè÷åñòâî îáùèõ ïîäïîñëåäîâàòåëüíîñòåé ðàâíî 1 + 2 + 4 = 7.

 

Ïðèìåð 2. Ïóñòü a = “abcda”, b = “bakca”, c = “dlaca”.

 

 

Ïîëîæèì f[0][0][0] = 1. Ýòî çíà÷åíèå áóäåò äîáàâëåíî â:

f[1][2][3] ïî áóêâå 'a' (“a”, “a”, “a”)

f[3][4][4] ïî áóêâå 'c' (“c”, “c”, “c”)

 

Çíà÷åíèå f[1][2][3] = 1 áóäåò äîáàâëåíî â:

f[3][4][4] ïî áóêâå 'c' (“ac”, “ac”, “ac”), (“c”, “c”, “c”)

f[5][5][5] ïî áóêâå 'a' (“aa”, “aa”, “aa”)

 

Çíà÷åíèå f[3][4][4] = 2 áóäåò äîáàâëåíî â:

f[5][5][5] ïî áóêâå 'a' (“aca”, “aca”, “aca”), (“ca”, “ca”, “ca”), (“aa”, “aa”, “aa”)

 

Ïî îêîí÷àíèþ ðàáîòû àëãîðèòìà íåíóëåâûìè (êðîìå f[0][0][0]) áóäóò ñëåäóþùèå çíà÷åíèÿ ìàññèâà f:

f[1][2][3] = 1 (“a”, “a”, “a”),

f[3][4][4] = 2 (“ac”, “ac”, “ac”), (“c”, “c”, “c”),

f[5][5][5] = 3 (“aca”, “aca”, “aca”), (“ca”, “ca”, “ca”), (“aa”, “aa”, “aa”)

Êîëè÷åñòâî îáùèõ ïîäïîñëåäîâàòåëüíîñòåé ðàâíî 1 + 2 + 3 = 6. Âñå îíè ïåðå÷èñëåíû âûøå.

 

Ðåàëèçàöèÿ àëãîðèòìà

Îáúÿâèì òðåõìåðíûé ìàññèâ f è ñèìâîëüíûå ìàññèâû s1, s2, s3, â êîòîðûõ áóäåì õðàíèòü âõîäíûå ñòðîêè.

 

long long f[51][51][51], res;

char s1[100], s2[100], s3[100];

 

Ôóíêöèÿ countCommonSubsequences âû÷èñëÿåò êîëè÷åñòâî ðàçíûõ íåïóñòûõ îáùèõ ïîäïîñëåäîâàòåëüíîñòåé â ñòðîêàõ a, b, c.

 

long long countCommonSubsequences(string a, string b, string c)

{

  int i, j, k, x, y, z;

  f[0][0][0] = 1; res = 0;

  for(i = 0; i <= a.size(); i++)

    for(j = 0; j <= b.size(); j++)

      for(k = 0; k <= c.size(); k++)

        if (f[i][j][k] > 0)

        {

          for(char ch = 'a'; ch <= 'z';ch++)

          {

            x = a.find(ch,i); y = b.find(ch,j); z = c.find(ch,k);

            if (x != string::npos && y != string::npos &&

                z != string::npos)

              f[x+1][y+1][z+1] += f[i][j][k];

          }

          res += f[i][j][k];

        }

  return res - 1;

}

 

Îñíîâíàÿ ÷àñòü ïðîãðàììû.

 

while(gets(s1))

{

  gets(s2);gets(s3);

  memset(f,0,sizeof(f));

  long long res = countCommonSubsequences(s1,s2,s3);

  printf("%lld\n",res);

}