sicily 1198 substring 组合问题

1198. Substring

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Dr lee cuts a string S into N pieces,s[1],…,s[N].

Now, Dr lee gives you these N sub-strings: s[1],…s[N]. There might be several possibilities that the string S could be. For example, if Dr. lee gives you three sub-strings {“a”,“ab”,”ac”}, the string S could be “aabac”,”aacab”,”abaac”,…

Your task is to output the lexicographically smallest S.

Input

The first line of the input is a positive integer T. T is the number of the test cases followed.

The first line of each test case is a positive integer N (1 <=N<= 8 ) which represents the number of sub-strings. After that, N lines followed. The i-th line is the i-th sub-string s[i]. Assume that the length of each sub-string is positive and less than 100.

Output

The output of each test is the lexicographically smallest S. No redundant spaces are needed.

Sample Input

1
3
a
ab
ac

Sample Output

aabac

Problem Source

ZSUACM Team Member

此题由于规模不大,所以可以直接用枚举出所有可能组合,然后选择按字典序最小的字符串输出,用到递归实现了一个字符数组的全排列,每次均进行拼接保存到一个vector中用于返回,vector记得使用引用

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

void swap(string &a, string &b);
void perminent(vector<string> &result, string substrs[], int subnum, int index);

int main() {
	int t;
	cin >> t;
	while (t-- > 0) {
		int subnum;
		string substr;
		vector<string> result;
		cin >> subnum;
		string substrs[subnum];
		for (int i = 0; i < subnum; i++) {
			cin >> substr;
			substrs[i] = substr;
		}
		perminent(result, substrs, subnum, 0);

		string minstr = result[0];
		//好该死呀!!!这里把result.size(),写成了subnum!!! 这样搞了一个多小时
		for (int i = 1; i < result.size(); i++) {
			if (minstr.compare(0, minstr.size(), result[i]) > 0) {
				minstr = result[i];
			}
		}
		cout << minstr << endl;
	}
	return 0;
}
void swap(string &a, string &b) {
	string temp = a;
	a = b;
	b = temp;
}

//获得该字符串数组的一个全排列
void perminent(vector<string> &result, string substrs[], int subnum, int index) {
	if (index == subnum-1) {
		string temp;
		for (int i = 0; i < subnum; i++) {
			temp += substrs[i];
		}
		result.push_back(temp);//result为引用调用
	} else {
		for (int i = index; i < subnum; i++) {
			//获得该数组的全排列的关键
			swap(substrs[i], substrs[index]); //把数组当前元素放在数组开头
			perminent(result, substrs, subnum, index+1);
			swap(substrs[i], substrs[index]);//还原回原来数组,继续递归,等下把数组下一个元素放在开头
		}
	}
}

  

时间: 2024-12-24 23:11:28

sicily 1198 substring 组合问题的相关文章

Sicily 1198 Substring

1198. Substring Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Dr lee cuts a string S into N pieces,s[1],…,s[N]. Now, Dr lee gives you these N sub-strings: s[1],…s[N]. There might be several possibilities that the string S could be.

sicily 1198. Substring (递归全排列+排序)

DescriptionDr lee cuts a string S into N pieces,s[1],…,s[N]. Now, Dr lee gives you these N sub-strings: s[1],…s[N]. There might be several possibilities that the string S could be. For example, if Dr. lee gives you three sub-strings {“a”,“ab”,”ac”},

【sicily系列】 1198 substring

Description Dr lee cuts a string S into N pieces,s[1],…,s[N]. Now, Dr lee gives you these N sub-strings: s[1],…s[N]. There might be several possibilities that the string S could be. For example, if Dr. lee gives you three sub-strings {“a”,“ab”,”ac”},

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

Java设计模式应用——组合模式

组合模式实际上是一种树形数据结构.以windows目录系统举例,怎么样用java语言描述一个文件夹? 定义一个文件夹类,文件夹类中包含若干个子文件类和若干个文件类. 进一步抽象,把文件夹和文件都看做节点,于是一个文件夹就可以描述为一个节点类,包含若干个子节点. 我们看看组合模式的代码 // 抽象节点 public abstract class Node { protected String name; abstract void add(Node node); abstract void rem

输出字符串所有组合

描述 输入字符串"abc",则输出a.b.c.ab.ac.bc.abc 共7种组合. 分析 方法一,递归法.遍历每个字符串,每个字符两种情况取或不取.时间复杂度为O(n的2次方). 方法二,根据每个字符只有取和不取两种情况,0表示不取,1表示取该字符,则原题可以解释为要求输001到111这个组合对应的字符串. 代码 方法一 public class Test { /* * 递归 * */ public static void combinate(char[] chars,int beg

[LeetCode] Substring with Concatenation of All Words

Substring with Concatenation of All Words You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any

C# 依据KeyEventArgs与组合键字符串相互转换

/// 快捷键相关的类 /// </summary> public static class HotKeyInfo { /// <summary> /// 依据KeyEventArgs生成组合键字符串 /// </summary> /// <param name="e"></param> /// <returns></returns> public static string GetStringByKe