קוד:
//--------------------------------Include Section------------------------------
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cstring>
//--------------------------------Using Section--------------------------------
using std::cin;
using std::cout;
using std::endl;
using std::setw;
//--------------------------------Const Section--------------------------------
const int MAX_STRS = 10,
MAX_STR_LEN = 8,
MAX_TEXT_LEN = 100 ;
//-------------------------------
void read_in_2_dict(char dict[][MAX_STR_LEN]);
void read_2_text(char text[]);
int find_longst_sub_str(char dict[][MAX_STR_LEN], char text[MAX_TEXT_LEN],
int& sub_str_leng);
int find_word_leng(char text[MAX_TEXT_LEN], int txt_i);
bool check_if_in_dict(char dict[][MAX_STR_LEN], char text[MAX_TEXT_LEN],
int txt_i);
bool check_curr_word(char dict[MAX_STR_LEN], char text[MAX_TEXT_LEN],
int txt_i);
void convert_text_2_upper(char text[MAX_TEXT_LEN]);
//--------------------------------Main Section---------------------------------
int main()
{
char dict[MAX_STRS][MAX_STR_LEN],
text[MAX_TEXT_LEN];
int longst_sub_str_strt,
sub_str_leng;
read_in_2_dict(dict);
cin.get();
read_2_text(text);
cout << find_longst_sub_str(dict, text, sub_str_leng) << " "
<< sub_str_leng << endl;
return (EXIT_SUCCESS);
}
//----------------
int find_longst_sub_str(char dict[][MAX_STR_LEN], char text[MAX_TEXT_LEN],
int& sub_str_leng)
{
int word_leng, curr_sub_str_strt;
for(int txt_i = 0 ; txt_i < MAX_TEXT_LEN && text[txt_i] != '\0'
; txt_i += word_leng)
{
if(isalpha(text[txt_i]))
{
word_leng = find_word_leng(text, txt_i);
if(check_if_in_dict(dict, text, txt_i))
;
}
}
}
//------------------------------------
bool check_if_in_dict(char dict[][MAX_STR_LEN], char text[MAX_TEXT_LEN], int txt_i)
{
for(int dict_r_i = 0 ; dict_r_i < MAX_STRS ; dict_r_i++)
{
if(!(check_curr_word(dict[dict_r_i], text, txt_i)))
continue;
else
return true;
}
return false;
}
//---------------------------------
bool check_curr_word(char dict[MAX_STR_LEN], char text[MAX_TEXT_LEN], int txt_i)
{
for(int txt_cell_i = txt_i; (isalpha(text[txt_cell_i])) ; txt_cell_i++)
{
if(text[txt_cell_i]!=dict[txt_cell_i])
return false;
}
return true;
}
//------------------
int find_word_leng(char text[MAX_TEXT_LEN], int txt_i)
{
int txt_leng = txt_i;
while(txt_leng < MAX_TEXT_LEN && (isalpha(text[txt_leng])))
txt_leng++;
return (txt_leng);
}
//-------------------------------
void read_in_2_dict(char dict[][MAX_STR_LEN])
{
for(int str_i = 0 ; str_i < MAX_STRS ;str_i++)
{
cin>> setw(MAX_STR_LEN) >> dict[str_i];
for(int conv_i = 0 ; conv_i < MAX_STR_LEN && dict[str_i] != '\0'; conv_i++)
dict[str_i][conv_i] = toupper(dict[str_i][conv_i]);
}
}
//-----------------------
[LEFT]
void read_2_text(char text[MAX_TEXT_LEN])
{
cin.getline(text, MAX_TEXT_LEN);
convert_text_2_upper(text);
}
//--------------------------------------------------
void convert_text_2_upper(char text[MAX_TEXT_LEN])
{
for(int conv_i = 0 ; conv_i < MAX_TEXT_LEN && text[conv_i] != '\0'; conv_i++)
text[conv_i] = toupper(text[conv_i]);
}