Find the Nearest Repeated Entries in an Array People do not like reading text in which a word is used multiple times in a short paragraph. You are to write a program which helps identify such a problem. Write a program that takes as input an array and finds the distance between closest pairs of equal entries. For example if s = <"All, "work", "and", "no", "play", "makes", "for", "no", "work", "and", "no", "fun", "and", "no", "results">, then the second and third occurrences of "no" is the closest pair.

Respuesta :

Limosa

Answer:

The following program is written in C++ Programming Language.

#include <iostream>   //header file

#include<cstring>    //header file

using namespace std;   //using namespace

int main()    //main function

{

int num;

cout<<"Enter the no. of words: ";   //print message

cin>>num;    //get input from the user

char input[num][10];

int visited[num]={0};

for(int i=0;i<num;i++)

{

cin>>input[i];

}

for(int i=0;i<num;i++)

{

int distance=1000,left,right,prev,flag;

left=i;

prev=i;

if(visited[i]==0)

{

flag=0;

for(int j=i+1;j<num;j++)

{

if(strcmp(input[j],input[i])==0)

{

if(j-prev<distance)

{

left=prev;

right=j;

distance=j-prev;

}

prev=j;

visited[j]=1;

flag=1;

}

}

if(flag==1)

cout<<"The closest distance between the pair of "<<input[i]<<" is "<<distance<<" and pair is ("<<left<<" , "<<right<<")"<<endl;

}

}

return 0;

}

Explanation:

Here, we define two header file <iostream> and <cstring> and using namespace "std".

Then, we define the main() function.

Then, we declare an integer variable "num"  in which we get the input from the user.

Then, we initialized the two array variable first is character array "input" and integer array visited and after all, we follow the instructions.