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

超时,哪位大神可以帮我看看程序?

Posted by zy666666666 at 2017-08-15 20:22:24 on Problem 1016
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <cstring>
#include <sstream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
string data;
int m[10];
map<string,int> v;
bool flag = 0;
int flagLength = 0;
string change(string data) {
	string re = "";
	int i;
	memset(m,0,sizeof(m));
	for(i = 0; i < data.length(); i++) {
			m[(data[i]-'0')] ++;
	}
	
	for(i = 0; i <= 9; i++) {
		if(m[i] != 0) {
			stringstream ss;
			ss << re << m[i] << i;
			re = ss.str();
			
		}	
	}
	
	return re;
}

int self_inventorying(string data) {
	v.clear();
	int i;
	string s = data;
	string temp;
	v[s] = 0;
	for(i = 0; i < 15; i++) {
		temp = change(s);
		//cout << temp << endl;
		if(temp == s) {
			break;
		}else {
			if(v.count(temp) && flag == 0) {
				flag = 1;
				flagLength = i+1-v[temp];
				s = temp;
				continue;
			}else {
				s = temp;
				v[temp] = i+1;
				//cout << temp << " " << v[temp] << endl;
			}
				
		}
	}	
	return i;
}
int main() {
	while(1) {
		cin >> data;
		if(data == "-1") {
			break;
		}else {
			flagLength = 0;
			flag = 0;
			int i= self_inventorying(data);
			if(i == 0) {
				cout << data << " is self-inventorying" << endl;
			}else if(i < 15) {
				cout << data << " is self-inventorying after " << i << " steps" << endl;
			}else if(i >= 15 && flag == 1) {
				cout << data << " enters an inventory loop of length " << flagLength << endl; 
			}else {
				cout << data << " can not be classified after 15 iterations" << 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