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

老wa 那位帮助test下或者给几个数据

Posted by xiaxia at 2006-01-19 02:57:45 on Problem 2615
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

#define min(a, b)  ((a) < (b) ? (a):(b))

string a, b;
string insa;
string insb;

bool isPalindrome(string& s, string& ins, bool tail)
{    
	 int i, j;
	 if(tail)
	 {
		 for(i = ins.length(), j = s.length() - 1; i < j; i ++, j --)
		 {
			 if(s[i] != s[j])
				 break;
		 }
		 if(i >= j)
		 {
			 return true;
		 }
	 }
	 else
	 {
		 string temps = s + ins;
		 for(i = 0, j = temps.length() - 1; i < j; i ++, j --)
		 {
			  if(temps[i] != temps[j])
				 break;
		 }
		 if(i >= j)
		 {
			 return true;
		 }
	 }

	 return false;
}

bool ok()
{
	bool aaok = isPalindrome(a, insa, true);
	bool baok = isPalindrome(b, insa, false);
	bool abok = isPalindrome(a, insb, false);
	bool bbok = isPalindrome(b, insb, true);
	bool a = (aaok && !baok) || (!aaok && baok);
	bool b = (abok && !bbok) || (!abok && bbok);
	if(a && !b)
	{	
		cout << insa << endl;
		return true;
	}
	else if(!a && b)
	{	
        cout << insb << endl;
		return true;
	}
	else if(a && b)
	{
		cout << min(insa, insb) << endl;
		return true;
	}
	else
	{
		return false;
	}	
}
void main()
{

	while(cin >> a >> b)
	{
		int i;    

        if(a == b)
			cout << "No Solution." << endl;
		else
		{
			 int lena = a.length();
			 int lenb = b.length();
			 if(lena > lenb)
			 {
				 int ltemp = lena;
				 lena = lenb;
				 lenb = ltemp;
				 string stemp = a;
				 a = b;
				 b = stemp;							 
			 }
             insa = "";
			 insb = "";
			 for(i = 0; i <= lena; i ++)
			 {
				 if(ok())
				 {
					break;
				 }        
                 if(i < lena)
				 {
				     insa.insert(0, a[i]);
				     insb.insert(0, b[i]);
				 }				 
			 }
			 if(i == lena + 1) 
			 {
				 if(b[lena] == 'a')
				 {
					 insa.insert(0, 'b');
					 cout << insa << endl;
				 }
				 else
				 {  
					 insa.insert(0, 'a');
					 cout << insa << endl;
				 }
			 }			 
		}
	}

} 


		








		





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