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

各位大虾,为什么我用printf输出就错了,用cout就可以AC。。。。结果都一样啊

Posted by 781778304 at 2011-08-22 10:20:07 on Problem 2001
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define N 10005
struct STRING
{
	string s;
	string prefix;
	int no;
}str[N];
bool cmp(STRING a,STRING b)
{
	return a.s<b.s;
}
bool cmp2(STRING a,STRING b)
{
	return a.no<b.no;
}
void get_prefix(int n)
{
	int i,j,k;
	for(i=0;i<n;i++)
	{
		int temp=str[i].s.length();
		for(j=1;j<temp;j++)
		{
			string sub=str[i].s.substr(0,j);
			for(k=0;k<n;k++)
				if(k!=i&&sub==str[k].s.substr(0,j)) 
					break;			 //如果出现重复,则break	
				if(k==n)             //表示没有出现重复,可以将其作为前缀
				{
					str[i].prefix=str[i].s.substr(0,j);
					break;
				}
		}
		if(j==temp)                  //如果遍历整个串都无法找到当前串,则将整个串作为其前缀
			str[i].prefix=str[i].s;
	}
}
int main()
{
	char ch[20];
	int i=0,j;
	while(scanf("%s",ch)!=EOF)
	{
		str[i].s="";
		str[i].s+=ch;
		str[i].no=i;
		i++;
	}
	sort(str,str+i,cmp);
	//for(j=0;j<i;j++)
	//	cout<<str[j].s<<endl;
	//cout<<endl;
	get_prefix(i);
	sort(str,str+i,cmp2);
	for(j=0;j<i;j++)
	{
		char temp[20];
		char temp2[20];
		strcpy(temp2,str[j].s.c_str());
		strcpy(temp,str[j].prefix.c_str());
		printf("%s %s\n",temp2,temp);
	}
		//cout<<str[j].s<<" "<<str[j].prefix<<endl;
	return 0;
}

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