Constraints
Time Limit: 10 secs, Memory Limit: 32 MB
Description
You never had any friends, and don't really want any anyways, and so you have decided to collect email addresses from web pages for direct e-mail advertising.
The text delivered to a web browser is usually marked up HTML, which may contain email addresses of the form:
user@server
¨ Both user and server are of the form alpha.numeric.with.dots. By alpha.numeric.with.dots, we mean a sequence of one or more characters which are alphabetic (A-Z,a-z), numeric (0-9), hyphens (-), underbars (_) and/or periods (.), with the following restrictions on periods:
n The sequence neither starts nor ends with a period.
n No periods are adjacent.
¨ Email addresses are preceded by the beginning of the file, or some character other than a letter (A-Z,a-z), digit (0-9), hyphen (-), or underbar (_).
¨ Email addresses are succeeded by the end of the file, or some character other than a letter (A-Z,a-z), digit (0-9), hyphen (-), or underbar (_).
¨ If the scanned text contains a sequence of the form
first@second@third
Then the output should contain first@second and second@third as email addresses. In a longer run, each pair split by an @-sign should appear as an email address in the output.
The point of this problem is to extract and record the email addresses embedded in other text.Input
The input file will contain zero or more lines of ASCII text.
Output
Other than the standard leader and trailer, the output file has each email address found in the input file in the order it was found (duplicates not removed).
Sample Input
bob@banks.com wrote: What does <tt>x=7</tt> mean for this problem? For example, ..a@a@aa@aaa@aaa..a@a@aa@aaa@aaa..a@a..@a...a@..@.. this scrolling @-example from jim@jones.com
Sample Outp
bob@banks.com a@a a@aa aa@aaa aaa@aaa a@a a@aa aa@aaa aaa@aaa a@a jim@jones.com这道题的代码的关键是找到字符@,然后再观察@前后的字符串是否满足条件要求,如果满足,就输出,同时,因为题目的要求是输入无限,所以我采取的方法是用getline函数读入一整行字符串,逐行处理。
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
string s;
while(getline(cin,s))
{
int a[100],k=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='@')
{
a[k]=i;
k++;
}
}
int l;
for(int b=0;b<k;b++)
{
int count1=0,count2=0;
if(b==0)
l=0;
else
l=a[b-1];
for(int i=a[b]-1;i>=l;i--)
{
if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z')||(s[i]>='0'&&s[i]<='9')||s[i]=='-'||s[i]=='_')
{
count1++;
}
else if(s[i]=='.')
{
if(i==l-1)
break;
else if((s[i-1]>='A'&&s[i-1]<='Z')||(s[i-1]>='a'&&s[i-1]<='z')||(s[i-1]>='0'&&s[i-1]<='9')||s[i-1] =='-'||s[i-1]=='_')
count1++;
else
break;
}
else
break;
}
int la;
if(b==k-1)
la=s.size();
else
la=a[b+1];
for(int i=a[b]+1;i<la;i++)
{
if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z')||(s[i]>='0'&&s[i]<='9')||s[i]=='-'||s[i]=='_')
{
count2++;
}
else if(s[i]=='.')
{
if(i==la-1)
break;
else if((s[i+1]>='A'&&s[i+1]<='Z')||(s[i+1]>='a'&&s[i+1]<='z')||(s[i+1]>='0'&&s[i+1]<='9')||s[i+1]=='- '||s[i+1]=='_')
count2++;
else
break;
}
else
break;
}
if(count1>0&&count2>0)
{
for(int k=a[b]-count1;k<=a[b]+count2;k++)
{
cout<<s[k];
}
cout<<endl;
}
}
}
return 0;
}
思路:先遍历一次字符串,用一个数组记录下‘@’的位置,然后用循环遍历@的前后字符串,如果满足要求,则输出。