UVA 10941 - Words adjustment(BFS+字符串处理)

UVA 10941 - Words adjustment

题目链接

题意:给定两个字符串,在给定一些单词集合,问能否两个单词后面各添加一些单词,使得两个单词变成相同,问添加单词最少几次,单词要来自单词集合

思路:广搜,记录状态为两个字符串之间差的字符,利用set和string去乱搞。。即可

代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

int t;
set<string> s, vis;
string x, y;

void init() {
	cin >> x >> y;
	string tmp;
	int q;
	cin >> q;
	s.clear();
	while (q--) {
		cin >> tmp;
		s.insert(tmp);
	}
}

struct State {
	string str;
	int val;
	State() {}
	State (string str, int val) {
		this->str = str;
		this->val = val;
	}
};

int solve() {
	queue<State> Q;
	if (x.length() > y.length())
		Q.push(State(x.substr(y.length()), 0));
	else Q.push(State(y.substr(x.length()), 0));
	vis.clear();
	vis.insert(Q.front().str);
	while (!Q.empty()) {
		State u = Q.front();
		Q.pop();
		if (u.str == "")
			return u.val;
		set<string>::iterator now = s.lower_bound(u.str);
		for (set<string>::iterator it = now;
				it != s.end() && it->substr(0, u.str.length()) == u.str; it++) {
			string tmp = it->substr(u.str.length());
			if (vis.find(tmp) == vis.end()) {
				vis.insert(tmp);
				Q.push(State(tmp, u.val + 1));
			}
		}
		for (int i = u.str.length(); i > 0; i--) {
			set<string>::iterator it = s.find(u.str.substr(0, i));
			if (it != s.end()) {
				string tmp = u.str.substr(it->length());
				if (vis.find(tmp) == vis.end()) {
					vis.insert(tmp);
					Q.push(State(tmp, u.val + 1));
				}
			}
		}
	}
	return -1;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		init();
		cout << solve() << endl;
	}
	return 0;
}
时间: 2024-10-13 07:05:38

UVA 10941 - Words adjustment(BFS+字符串处理)的相关文章

uva 10391 Compound Words (字符串-hash)

Problem E: Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is theconcatenation of exactly two other words in the dictionary. Input Standard input consists of a

UVA 261 - The Window Property(字符串Hash)

UVA 261 - The Window Property 题目链接 题意:这题题意挺绕的..就是给定一个字符串长度n,扫描长度为k = [1,n],然后每次只能扫描连续k个字符的子串,要求所有扫描中,每次扫描中出现的不同字符串个数都不超过k + 1,那么这个字符串就是window property,如果不是的话,就还要找出下标最小的不符合的位置(就是n次扫描中找最小的) 思路:Hash大法好,长度才100,直接处理出hash,O(n^2)随便搞掉 代码: #include <cstdio>

UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: 1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子

uva 10603 Fill (BFS)

uva 10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third is completely filled with water. It is allowed to pour

UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; struct node{ int ft; int sta; }flo[1010][1010]; int vis[1010][1010]; st

UVA 706 LCD Display 液晶显示屏 (字符串模拟)

[题目链接]click here~~ [题目大意] 给定的数字序列,按照要求输出对应液晶显示屏上的数字 输入: 2 12345 3 67890 0 0 输出: -- -- -- | | | | | | | | | | | | -- -- -- -- | | | | | | | | | | -- -- -- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- | | | | | | | |

uva 1339 Ancient Cipher(字符串处理)

uva 1339 Ancient Cipher Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. T

UVA 11624 - Fire!(BFS)

UVA 11624 - Fire! 题目链接 题意:一个迷宫,一些格子着火了,火每秒向周围蔓延,现在J在一个位置,问他能走出迷宫的最小距离 思路:BFS2次,第一次预处理每个位置着火时间,第二次根据这个再BFS一次 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; co

UVa 401 Palindromes(简单字符串)

简单的判断是否是回文串.镜像串,然后自己写的真费劲,没逃掉刘汝佳的书,这里的代码很有技巧性,特别值得学习,额,其实他书上的代码都很精简 Character Reverse Character Reverse Character Reverse A A M M Y Y B   N   Z 5 C   O O 1 1 D   P   2 S E 3 Q   3 E F   R   4   G   S 2 5 Z H H T T 6   I I U U 7   J L V V 8 8 K   W W