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

这题就是二次dp,可以AC,附代妈

Posted by KatrineYang at 2016-09-08 05:06:15 on Problem 1239
In Reply To:不是二次dp ? 一次正向得到最小,一次反向是前面的最大,为什么就wa了? Posted by:achilles at 2005-09-09 09:58:00
注意:要字符串比较!!!不能用整形,long long也不行,会溢出导致OLE!!!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;


char str[100];

int compare(int start1, int end1, int start2, int end2){
	if(start1 == start2 && end1 == end2) return 0;
	while(start1 <= end1 && str[start1] == '0') start1++;
	while(start2 <= end2 && str[start2] == '0') start2++;
	int len1 = end1-start1+1, len2 = end2-start2+1;
	if(len1 > len2) return 1;
	if(len1 < len2) return -1;
	for(int i = 0; i < len1; i++){
		if(str[start1+i] > str[start2+i]) return 1;
		if(str[start1+i] < str[start2+i]) return -1;
	}
	return 0;
}


int main() {
	while(1){
		scanf("%s", str);
		int l = strlen(str);
		if(l == 1 && str[0] == '0') break;
		int minpos[100];
		minpos[0] = 0;
		for(int i = 1; i < l; i++){
			bool set = 0;
			for(int j = i; j > 0; j--){
				if(compare(j, i, minpos[j-1], j-1) == 1){
					set = 1;
					minpos[i] = j;
					break;
				}
			}
			if(!set) minpos[i] = 0;
		}
		//cout << minpos[l-1] << endl;
		int mark = l-1;
		bool ok[100] = {0};
		int maxpos[100];
		for(; mark >= 0; mark--){
			int cmpr = compare(mark, l-1, minpos[l-1], l-1);
			if(cmpr == -1){
				ok[mark] = 0;
				continue;
			}
			else if(cmpr == 0){
				ok[mark] = 1;
				maxpos[mark] = l-1;
			}
			else{
				break;
			}
		}
		for(int j = mark; j >= 0; j--){
			for(int k = j; k < l; k++){
				if(!ok[k+1]) continue;
				int cmpr = compare(j, k, k+1, maxpos[k+1]);
				if(cmpr == -1){
					ok[j] = 1;
					maxpos[j] = k;
				}
			}
		}
		int offset = 0;
		while(offset < l){
			int newOffset = maxpos[offset];
			for(int j = offset; j <= newOffset; j++) printf("%c", str[j]);
			if(newOffset != l-1) printf(",");
			offset = newOffset+1;
		}
		printf("\n");
	}
	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