找出不出头的单词的字符串
#include<cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, char** argv)
{
string goal = { "acenmorsuvwxz" };
string s;
string::size_type pos = 0, pos1 = 0, pos2 = 0, max_pos1 = 0, max_pos2 = 0, s_length = 0;
fstream infile("string.txt");
if (infile)
{
infile >> s;
cout << "原字符串为" << s << endl;
while ((pos1 = s.find_first_of(goal, pos1)) != string::npos)
{
for (pos1; pos1 != s.size() ; ++pos1)
{
if ((pos2 = s.find_first_not_of(goal, pos1)) != string::npos)
{
pos = pos2 - pos1;
if (s_length < pos)
{
s_length = pos;
max_pos1 = pos1;
max_pos2 = pos2;
}
}
else
{
pos = s.size() - pos1;
if (s_length < pos)
{
s_length = pos;
max_pos1 = pos1;
max_pos2 = s.size();
}
}
}
}
string output_s = s.substr(max_pos1, s_length);
cout << "最长的不出头的字符串为" << output_s << endl;
}
else
{
cerr << "open failed." << endl;
}
return 0;
}