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

递归+组合数就解决,大水题一个!0ms无压力

Posted by KatrineYang at 2016-08-31 10:34:34 on Problem 1240 and last updated at 2016-08-31 10:34:49
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
using namespace std;

long long int comb(int n, int m){
	if(m == 0 || m == n) return 1;
	if(m > n/2) m = n-m;
	long long int ans = 1;
	for(int i = n; i >= n-m+1; i--) ans *= i;
	for(int i = 2; i <= m; i++) ans /= i;
	return ans;
}

struct node{
	char bh;//a~z
	vector<node*> suc;
} pool[26];

void init(){
	for(int i = 0; i < 26; i++) {
		pool[i].bh = 'a'+i;
		pool[i].suc.clear();
	}
}

string s1, s2;
int M;
long long int ANS;

void buildTree(node *root, int start1, int end1, int start2, int end2){
	if(start1 > end1) return;
	while(start1 <= end1){
		char tar = s1[start1];
		int pos;
		for(int i = start2; i <= end2; i++){
			if(s2[i] == tar){
				pos = i;
				break;
			}
		}
		root->suc.push_back(&pool[tar-'a']);
		buildTree(&pool[tar-'a'], start1+1, start1+pos-start2, start2, pos-1);
		start1 = start1+pos-start2+1;
		start2 = pos+1;
	}
}

void getAns(node *root){
	int gs = root->suc.size();
	for(int i = 0; i < gs; i++) getAns(root->suc[i]);
	ANS *= comb(M, gs);
}

int main() {
	while(cin >> M){
		if(M == 0) break;
		init();
		node *root;
		cin >> s1 >> s2;
		int len = s1.length();
		root = &pool[s1[0]-'a'];
		buildTree(root, 1, len-1, 0, len-2);
		ANS = 1;
		getAns(root);
		cout << ANS << 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