Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这题OLE是什么状况?

Posted by majia1234 at 2008-05-01 21:01:04 on Problem 2001
#include	<iostream>
#include	<string>
#include	<algorithm>
#define MAX 1000
using namespace std;

struct word_t {
	string s;
	int pos;
}word[MAX];

int lastidx[MAX];
int	len;
int 
get_lastindex (int m)
{
	int idx1, idx2;
	if (m > 0) {
		int i = 0;
		while (word[m].s[i] == word[m - 1].s[i]
		 	 && i < word[m - 1].s.length()) 
		{
			i ++;
		}
		if (i == word[m].s.length()) {
			idx1 = i;
		} else {
			idx1 = i + 1;
		}
	} 
	
	if (m < len - 1) {
		int	i = 0;
		while (word[m].s[i] == word[m + 1].s[i]
				&& i < word[m].s.length())
		{
			i ++;
		}
		if (i == word[m].s.length()) {
			idx2 = i;
		} else {
			idx2 = i + 1;
		}
	}
	lastidx[word[m].pos] = max (idx1, idx2);
}

bool
cmp1 (word_t w1, word_t w2)
{
	return w1.s < w2.s;
}

bool
cmp2 (word_t w1, word_t w2)
{
	return w1.pos < w2.pos;
}

int
main ()
{
	//freopen ("test1.txt", "r", stdin);
	//freopen ("myout.txt", "w", stdout);
	len = 0;
	while (cin >> word[len ++].s) {
		word[len - 1].pos = len - 1;
	}
	-- len;
	
	sort (word, word + len, cmp1);
	for (int i = 0; i < len; ++ i) {
		get_lastindex (i);
	}
	sort (word, word + len, cmp2);
	for (int i = 0; i < len; ++ i) {
		cout << word[i].s << " ";
		word[i].s.resize(lastidx[i]);
		cout << word[i].s << endl;
	}
	//system ("pause");
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator