example code for find maximal two characater string in a long string

Coordinator
May 12, 2014 at 9:03 PM

include <string>

include <set>

include <iostream>

using namespace std;

int main(void)
{
string line;
cout<<"please input a string"<<endl;
set<char> char_set;
getline(cin, line);

int two_start,tow_end,len,one_start,two_end,i,max_len,max_start,max_end;

max_len=2;
max_start=1;
max_end=1;

char_set.insert(line[0]);

i=1;

while(i<line.size())
{
  two_start=one_start=i;
  char_set.insert(line[i]);

  while (char_set.size()<=2) 
  {
       two_end=i;
       if(line[i]!=line[i-1]) one_start=i;
       i++;
       char_set.insert(line[i]);
  }
 len=two_end-two_start+1;
 if(len>max_len)
 {
       max_len=len;
       max_start=two_start;
       max_end=two_end;
 }
 i=one_start;
 char_set.clear();
}

cout<<"Ths longest two character string is started at "<<max_start<<" end at:"<<max_end<<endl;

for (i=max_start;i<=max_end;i++)
{
 cout<<line[i];
}

}