| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:这是我写的这个算法的加强版,哪位兄弟能告诉我错在哪里啊In Reply To:Re:谁能告诉我这个程序为什么错了 Posted by:chengmingvictor at 2005-08-01 11:37:02
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
int difference(string a,string b)
{
for(int i = 0 ; i < a.length() ; i++)
{
if( a[i] != b[i])
return i;
}
return i-1;
}
int main()
{
string allString[1001];
string temp[1001];
map<string,string> prefix;
int count(0);
while( cin >> temp[count] )
{
allString[count] = temp[count];
count++;
}
if(count > 2)
{
sort(allString , allString + count);
int start = difference( allString[0] , allString[1] );
int maxnum;
if(allString[0] != allString[1])
prefix[allString[0]] = allString[0].substr(0,start + 1);
else
{
start = 0;
prefix[allString[0]] = allString[0].substr(0,start + 1);
}
for( int i = 1 ; i < count ; i++)
{
if( start == allString[i-1].length() - 1 && allString[i] != allString[i-1])
start++;
if(allString[i-1][0] != allString[i][0])
start = 0;
if(allString[i] != allString[i+1])
maxnum = difference(allString[i],allString[i+1]);
else
maxnum = start;
prefix[ allString[i] ] = allString[i].substr(0,1+(start > maxnum?start:maxnum));
start = maxnum;
}
for(i = 0 ; i < count ; i++)
cout << temp[i] <<' ' << prefix[temp[i]] << endl;
}
else
cout << temp[0] << endl;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator