UVA 1326 - Jurassic Remains(技巧枚举+位运算)

Paleontologists in Siberia have recently found a number of fragments of Jurassic period dinosaur skeleton. The paleontologists have decided to forward them to the paleontology museum. Unfortunately, the dinosaur was so huge, that there was no box that the fragments would fit into. Therefore it was decided to split the skeleton fragments into separate bones and forward them to the museum where they would be reassembled. To make reassembling easier, the joints where the bones were detached from each other were marked with special labels. Meanwhile, after packing the fragments, the new bones were found and it was decided to send them together with the main fragments. So the new bones were added to the package and it was sent to the museum.

However, when the package arrived to the museum some problems have shown up. First of all, not all labels marking the joints were distinct. That is, labels with letters `A' to `Z' were used, and each two joints that had to be connected were marked with the same letter, but there could be several pairs of joints marked with the same letter.

Moreover, the same type of labels was used to make some marks on the new bones added to the box. Therefore, there could be bones with marked joints that need not be connected to the other bones. The problem is slightly alleviated by the fact that each bone has at most one joint marked with some particular letter.

Your task is to help the museum workers to restore some possible dinosaur skeleton fragments. That is, you have to find such set of bones, that they can be connected to each other, so that the following conditions are true:

  • If some joint is connected to the other joint, they are marked with the same label.
  • For each bone from the set each joint marked with some label is connected to some other joint.
  • The number of bones used is maximal possible.

Note that two bones may be connected using several joints.

Input 

Input consists of several datasets. The first line of each dataset contains N - the number of bones (1$ \le$N$ \le$24). Next N lines contain bones descriptions: each line contains a non-empty sequence of different capital letters, representing labels marking the joints of the corresponding bone.

Output 

For each dataset, on the first line of the output print L - the maximal possible number of bones that could be used to reassemble skeleton fragments. After that output L integer numbers in ascending order - the bones to be used. Bones are numbered starting from one, as they are given in the input file.

Sample Input 

6
ABD
EG
GE
ABE
AC
BCD

Sample Output 

5
1 2 3 5 6

题意:说白了就是找最多字符串使得所有字符出现次数为偶数次。

思路:枚举一半的字符串取不取,可以用位运算表示,然后对于每个字母奇数为1偶数为0,然后在去枚举另一半,如果另一半出现一个状态之前出现过,就代表可以组合,因为相同状态奇数+奇数,偶数+偶数,最终得到都是偶数。

代码:

#include <stdio.h>
#include <string.h>
#include <map>
using namespace std;

const int N = 30;
int n, state[N];
char str[N];
map<int, int> vis;

int bitcount(int state) {
	return state ? bitcount(state/2) + (state&1) : 0;
}

void init() {
	vis.clear();
	memset(state, 0, sizeof(state));
	for (int i = 0; i < n; i++) {
		scanf("%s", str);
		for (int j = 0; j < strlen(str); j++)
			state[i] = (state[i]^(1<<(str[j] - 'A')));
	}
}

void solve() {
	int i, j, n1 = n / 2, n2 = n - n1, bit1 = 0, bit2 = 0;
	for (i = 0; i < (1<<n1); i++) {
		int s = 0;
		for (j = 0; j < n1; j++) {
			if (i&(1<<j)) {
				s = (s^state[j]);
			}
		}
		if (bitcount(i) > bitcount(vis[s]))
			vis[s] = i;
	}
	int ans = 0;
	for (i = 0; i < (1<<n2); i++) {
		int s = 0;
		for (j = 0; j < n2; j++) {
			if (i&(1<<j)) {
				s = (s^state[j + n1]);
			}
		}
		if (vis[s] && ans < bitcount(i) + bitcount(vis[s])) {
			ans = bitcount(i) + bitcount(vis[s]);
			bit1 = vis[s]; bit2 = i;
		}
	}
	int bo = 0;
	printf("%d\n", ans);
	for (i = 0; i < n1; i++)
		if (bit1&(1<<i)) {
			if (bo++) printf(" ");
			printf("%d", i + 1);
		}
	for (i = 0; i < n2; i++) 
		if (bit2&(1<<i)) {
			if (bo++) printf(" ");
			printf("%d", i + 1 + n1);
		}
	printf("\n");
}

int main() {
	while (~scanf("%d", &n)) {
		init();
		solve();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题已经记录下来了。以下是使用Scala语言和Spark SQL分析得出每种类型的评分最高10部电影的代码示例: ```scala import org.apache.spark.sql.functions._ import org.apache.spark.sql.types.{StructType, StructField, StringType, IntegerType} import org.apache.spark.sql.{SparkSession, functions} object TopMoviesByGenre { def main(args: Array[String]): Unit = { val spark = SparkSession.builder() .appName("TopMoviesByGenre") .getOrCreate() // 读取movies.csv文件 val moviesSchema = new StructType() .add(StructField("movieId", IntegerType, true)) .add(StructField("title", StringType, true)) .add(StructField("genres", StringType, true)) val movies = spark.read .option("header", "true") .schema(moviesSchema) .csv("movies.csv") // 读取ratings.csv文件 val ratingsSchema = new StructType() .add(StructField("userId", IntegerType, true)) .add(StructField("movieId", IntegerType, true)) .add(StructField("rating", IntegerType, true)) .add(StructField("timestamp", StringType, true)) val ratings = spark.read .option("header", "true") .schema(ratingsSchema) .csv("ratings.csv") // 使用explode函数将genres字段拆分成多行 val explodedMovies = movies.select(col("movieId"), col("title"), explode(split(col("genres"), "\\|")).as("genre")) // 计算每部电影的平均评分 val movieRatings = ratings.groupBy("movieId").agg(avg("rating").as("avgRating")) // 将电影的平均评分与电影信息进行关联 val joinedData = explodedMovies.join(movieRatings, Seq("movieId")) // 按照电影类型和平均评分进行分组,取出每种类型的评分最高10部电影 val topMoviesByGenre = joinedData.groupBy("genre") .agg(functions.collect_list(struct(col("title"), col("avgRating"))).as("movies")) .select(col("genre"), explode(array_repeat(col("movies"), 1)).as("movie")) .select(col("genre"), col("movie.title"), col("movie.avgRating")) .orderBy(col("genre"), col("avgRating").desc) .groupBy("genre") .agg(functions.collect_list(struct(col("title"), col("avgRating"))).as("topMovies")) .select(col("genre"), col("topMovies.title"), col("topMovies.avgRating")) .show(false) spark.stop() } } ``` 上述代码中,我们首先读取movies.csv和ratings.csv文件,并将genres字段拆分成多行。然后计算每部电影的平均评分,并将电影的平均评分与电影信息进行关联。最后按照电影类型和平均评分进行分组,取出每种类型的评分最高10部电影。最后的结果将以如下格式显示: ``` +-----------+----------------------------------+----------+ |genre |title |avgRating | +-----------+----------------------------------+----------+ |Action |[Terminator 2: Judgment Day, 4.163] |Action | | |[Matrix, The, 4.154] | | | |[Die Hard, 3.862] | | | |[Aliens, 3.973] | | | |[Star Wars: Episode IV - A New Hope, 4.231]| | | |[Raiders of the Lost Ark, 4.207] | | | |[Star Wars: Episode V - The Empire Strikes Back, 4.215]| | | |[Indiana Jones and the Last Crusade, 3.916]| | | |[Star Wars: Episode VI - Return of the Jedi, 4.137]| | | |[Batman, 3.428] | | +-----------+----------------------------------+----------+ |Adventure |[Raiders of the Lost Ark, 4.207] |Adventure | | |[Star Wars: Episode IV - A New Hope, 4.231]| | | |[Indiana Jones and the Last Crusade, 3.916]| | | |[Star Wars: Episode V - The Empire Strikes Back, 4.215]| | | |[Star Wars: Episode VI - Return of the Jedi, 4.137]| | | |[Jurassic Park, 3.706] | | | |[Back to the Future, 3.931] | | | |[Indiana Jones and the Temple of Doom, 3.676]| | | |[Lord of the Rings: The Fellowship of the Ring, The, 4.106]| | | |[Lord of the Rings: The Two Towers, The, 4.021]| | +-----------+----------------------------------+----------+ ... ``` 每一行代表一种类型的电影和该类型的评分最高10部电影。其中,title为电影名称,avgRating为平均评分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值