SRM556

1.ChocolateBar

#include<iostream>
#include<string>
#include<map>
using namespace std;

class ChocolateBar{
public:
	 int maxLength(string s){
		
		map<char,int>mp;
		memset(a,0,sizeof(a));
		for(int i = 0; i <= m; ){
			a[s[i]-'a']++;
			if(a[s[i]-'a']>1){
				j = mp[s[i]];
				int r = i-j;
				if(res<r)
					res = r;
				mp[s[i]]=i;
				i++;
			}
			else{
				mp[s[i]]=i;
				i++;
			}
		}
		return res;
	}
};
#include<iostream>
#include<string>
#include<map>
using namespace std;

class ChocolateBar{
public:
	 int maxLength(string s){
	
		 int m = s.length()-1;
		 int a[30],res=0;
		 memset(a,0,sizeof(a));
		 for(int i=0;i<=m;i++)
		 {
			 for(int k=0;k<26;k++) a[k]=0;
			 for(int j=i;j<=m;j++)
			 {
				 int r=s[j]-'a';
				 a[r]++;
				 if(a[r]>1){
					 int temp=j-i;
					 if(temp>res)
						res=temp;
					 break;
				 }
				 int temp=j-i+1;
				 if(temp>res)
					res=temp;
			 }
		
		 }
		 return res;
	}
};

 

2.XorTravelingSalesman

#include<iostream>
#include<queue>
#include<string>
#include <vector>
//#pragma   warning (disable: 4786)
using namespace std;

class XorTravelingSalesman{
public:
	int maxProfit(vector<int> cityValues,vector<string> roads)
	{
		bool mark[50][1024];
		int n=cityValues.size();
		int res=0;
		memset(mark,false,sizeof(mark));
		queue<pair<int,int> > qu;
		qu.push(make_pair(0,cityValues[0]));
		mark[0][cityValues[0]]=true;
		res=cityValues[0];
		while(!qu.empty()){
			pair<int,int>u=qu.front();
			qu.pop();
			for(int i=0;i<n;i++){
				if(i!=u.first&&(roads[i][u.first]=='Y'||roads[u.first][i]=='Y')){
					pair<int,int>v;
					v.first=i;
					v.second=u.second^cityValues[i];
					if(!mark[v.first][v.second]){
						mark[v.first][v.second]=true;
						if(res<v.second)
							res=v.second;
						qu.push(v);
					}
				}
			}

		}
		return res;
	}
};

 

3.LeftRightDigitsGame

#include<iostream>
#include<string>
using namespace std;
int login=0;
class LeftRightDigitsGame{
public:
         //也可用call调用
	/*string call(string s){
		int n=s.size();
		if(n==0)
			return "";
		int j=0;
		for(int i=0;i<n;i++){
			if(s[i]<=s[j])
				j=i;
		}
		return s.substr(j,1)+call(s.substr(0,j))+s.substr(j+1,n-j-1);
	}*/
	string minNumber(string  digits){
		int n=digits.length();
		if(n==0)
			return "";
		int j=0;
		for(int i=0;i<n;i++){
			if(login==0){
				if(digits[i]!='0' && (digits[j]=='0'||digits[i]<=digits[j])){
					j=i;
				}
			}
			else{
				if(digits[i]<=digits[j])
					j=i;
			}
		}
		login++;
		return digits.substr(j, 1)+minNumber(digits.substr(0, j))+digits.substr(j+1, n-j-1);
	}
};

 

4.LeftRightDigitsGame2

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

class LeftRightDigitsGame2 {
public:
	string 	digits, lowerBound;
	bool 	mark[60][60][3][3];
	string 	f[60][60][3][3];
	int		n;
	string min(string a,string b){
		if(a<b)
			return a;
		return b;
	}
	string call(int l, int r, int cmpl, int cmpr) {
		//if we visited this state, just return the value we calculated before
		if (mark[l][r][cmpl][cmpr]) return f[l][r][cmpl][cmpr];
		
		//if all n characters of digits were used, the result string must be equal to or larger than lowerBound
		if (l + r == n) {
			if (cmpl == 0 || (cmpl == 1 && cmpr == 0)) return "#";
			return "";
		}
		
		//marked this state
		f[l][r][cmpl][cmpr] = "#";
		mark[l][r][cmpl][cmpr] = true;
		
		//if the (l + r)th character of digits was put on the left
		int cmpl2 = cmpl;
		
		if (cmpl == 1) {
			if (digits[l + r] < lowerBound[l]) cmpl2 = 0;
			else if (digits[l + r] > lowerBound[l]) cmpl2 = 2;
		}
		if(cmpl==0){
			return "#";
		}		
		string tmp = call(l + 1, r, cmpl2, cmpr);
		if (tmp != "#") f[l][r][cmpl][cmpr] = digits[l + r] + tmp;
		
		//if the (l + r - 1)th character of digits was put on the right
		int cmpr2 = cmpr;
		
		if (digits[l + r] < lowerBound[n - r - 1]) cmpr2 = 0;
		else if (digits[l + r] > lowerBound[n - r - 1]) cmpr2 = 2;
		
		tmp = call(l, r + 1, cmpl, cmpr2);
		if (tmp != "#") {
			tmp = tmp + digits[l + r];
			if (f[l][r][cmpl][cmpr] == "#") f[l][r][cmpl][cmpr] = tmp;
			else f[l][r][cmpl][cmpr] = min(f[l][r][cmpl][cmpr], tmp);
		}
		
		return f[l][r][cmpl][cmpr];
	}
	
	string minNumber(string digits_, string lowerBound_) {
		lowerBound = lowerBound_;
		
		//reverse the order of characters in digits_
		n = digits_.size();
		digits = "";
		for (int i = 0; i < n; i++) digits = digits_[i] + digits; 

		memset(mark, false, sizeof(mark));
		string res = call(0, 0, 1, 1);
		
		//res == "#" means there is no solution
		if (res == "#") res = "";
		return res;
		
		return "";
	}
};



 



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值