Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我用DFS做的,,不知道WA在哪里ToT,,哪位大牛给个测试数据啊#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef struct{ int from, to; }str; // 记录字符串的起止下标 str ans[80]; // 记录各子字符串的起止下标 char temp[81]; // 输入的字符串 int strnum; // 记录子字串数 bool cmpless(int f1, int t1, int f2, int t2) { int i=f1, j=f2, k; while(i <= t1 && temp[i] == '0') i++; while(j <= t2 && temp[j] == '0') j++; if(i > t1 || j > t2) return false; if(t1-i < t2-j) return true; if(t1-i == t2-j){ for(k=0; k<=t1-i; k++) if(temp[i+k] > temp[j+k]) break; if(k > t1-i){ if(temp[t1] != temp[t2]) return true; else return false; } else return false; } return false; } bool dfs(int end) { if(end < 0) return true; for(int i=0; i<=end; i++){ if(cmpless(i, end, ans[strnum-1].from, ans[strnum-1].to)){ ans[strnum].from = i; ans[strnum].to = end; strnum++; if(dfs(i-1)) return true; strnum--; } } return false; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(scanf("%s", temp) && strcmp(temp, "0")) { strnum = 0; int i, j; int tail = strlen(temp) - 1; for(i=tail; i>=0; i--){ ans[strnum].from = i; ans[strnum].to = tail; strnum++; if(dfs(i-1)) break; strnum--; } // ans数组是逆序记录子字符串的 for(i=strnum-1; i>0; i--){ for(j=ans[i].from; j<=ans[i].to; j++) printf("%c", temp[j]); printf(","); } for(j=ans[i].from; j<=ans[i].to; j++) printf("%c", temp[j]); printf("\n"); } return 0; } 注:测了好多数据都没看出什么问题,,怎么就WA了呢,,郁闷呐~~~~~~~~ Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator