UVA - 1610 Party Games (字符串比较)】的更多相关文章

给你n(n为偶数)个字符串,让你找出一个长度最短且字典序尽量小的字符串,使得一半的字符串小于等于该串,一半的字符串大于该串. 紫薯上说这道题有坑,但其实思路对了就没什么坑. 很明显,只要取夹在中间两个字符串间的最小字符串就行了.可以从小到大枚举字符串的长度,如果该长度下存在满足条件的字符串则返回结果,否则把较小的字符串上对应位的字符加到末尾就行了. 因为想错思路WA了好几次,最后一次WA居然是忘了判断n=0的情况...(为什么我总是完美避开正确答案,QAQ) #include<bits/stdc…
题意: 给出一系列字符串,构造出一个字符串大于等于其中的一半,小于另一半. 分析: 取大小为中间的两个a,b(a<b).实际上就是找出第一个小于b的同时大于等于a的字符串,直接构造即可. 代码: #include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;string a[1010];string res;int n;int…
题意: 给出一系列字符串,构造出一个最短字符串(可以不在集合中)大于等于其中的一半,小于另一半. 析:首先找出中间的两个字符串,然后暴力找出最短的字符串,满足题意. 代码如下: #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cstdio> using namespace std; vector<string>…
题意:输入一个n(2<=n<=1000,n是偶数)个字符串的集合D,找一个长度最短的字符串S(不一定在D中出现),使得D中恰好一半串小于等于S,另一半串大于S.如果有多解,输出字典序最小的解. 分析:找到最中间的两个串,直接按位构造. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib…
题目 #include<iostream> #include<string> #include<algorithm> using namespace std; //生成字符串 /* 求出排序后位于中间的两个字符串较小的和较大的分别为s1,s2 s[i]表示字符串s的第i位 对于第1位,显然只需要考虑两个值:s1[1],s1[1]+1 如果单独一位两种方式不能满足,则考虑2位的情况 显然,如果需要考虑第2位,那么第1位应该取s1[1] 对于第i位,如果s1[i]和s2[i…
题意:有一个N个字符串(N≤1000,N为偶数)的集合,要求找一个长度最短的字符串(可不在集合内)S,使得集合中恰好一半的串小于等于S,另一半大于S.如果有多解,要求输出字典序最小的解. 解法:本来我是想分析情况用if else实现的,但是细节很多,特别容易错.结果果然如此.╮(╯_╰)╭ 那么便看看搜索行不行,由于要求字典序最小,也就是长度尽量小的情况下字符尽量小.而且要集合中恰好一半的串小于等于S,另一半大于S,也就是排序后>=中间靠左边的串且<中间靠右边的串.那么我们可以对排序后的中间的…
题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <vector> #include <map> #include <set> #define FRER() freopen…
 Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follo…
题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样,即n=1.但是如果循环出现了,比如abab,那就可以表示成(ab)2.还有一点,就是要使得n尽量大,那么当出现abababab时,应该要这么表示(ab)4,而不是(abab)2. 此题用神奇的KMP解决,也就是主要利用next数组.举例说明. 一般出现循环的都会大概是这样的:abcabcabc.而这…
https://vjudge.net/problem/UVA-1610 题意:输入一个n个字符串的集合D,找一个长度最短的字符串S,使得D中恰好有一半串小于等于S,另一半串大于S. 思路:先拍序,然后选择中间的两个,比较他们就可以了.可以用枚举法来比较. #include<string> #include<iostream> #include<algorithm> using namespace std; + ; string str[maxn]; int main()…
这道题我真的想的非常的复杂, 拿草稿纸一直在找规律,推公式, 然后总有一些特殊的情况. 然后就WA了N次.无奈之下看了别人的博客, 然后就惊了.直接暴力枚举两个相邻字符串 里面的所有可能就可以了--真的是暴力出奇迹! #include<cstdio> #include<iostream> #include<algorithm> #include<string> #define REP(i, a, b) for(int i = (a); i < (b);…
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1332 题目大意: 给定一个词典(已经按照字典序排好),要求找出其中所有的复合词,即恰好由两个单词连接而成的单词.(按字典序输出) 思路: 对于每个单词,存入Hash表,然后对每个单词拆分. Hash函数的选取可以看:https://www.byvoid.com/blog/string-has…
题意:給定兩個小寫的字串a與b,請印出皆出現在兩字串中的字母,出現的字母由a~z的順序印出,若同字母出現不只一次,請重複印出但不能超過任一字串中出現的次數.(from Ruby兔) 很水,直接比较输出即可. 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1001; int main() { char a[maxn],…
当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是一致的. #include <cstdio> + ; char s[maxn]; int f[maxn]; //失配函数 int main() { //freopen("in.txt", "r", stdin); ; && n) { getc…
题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #include<cctype> #include<cmath> #include<cstdio> #include<string> #include<stack> #include<list> #include<se…
There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. (S)He can make a big integer by appending those in…
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2631 题目大意: 给一个字符串, 要求把它分割成若干个子串,使得每个子串都是回文串.问最少可以分割成多少个. 分析: f[i]表示以i结尾的串最少可以分割的串数. f[i] = min{ f[j]+1, 串[j,i]是回文串&&1<=j<=i } #i…
Periodic Strings 模板 题意分析 判断字符串的最小周期 代码总览 /* Title:UVA.455 Author:pengwill Date:2016-12-16 */ #include <iostream> #include <cstdio> #include <cstring> using namespace std; char str[100]; int main() { int n; scanf("%d",&n); w…
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单dp 题目大意: 给一个字符串, 要求把它分割成若干个子串,使得每个子串都是回文串.问最少可以分割成多少个. 定义:dp[i]表示前0~i内的字符串划分成的最小回文串个数: dp[i] = min(dp[j]+1 | j+1~i是回文串); 先预处理flag[i][j]表示以i~j内的字符串为回文串…
UVA 11927 - Games Are Important option=com_onlinejudge&Itemid=8&page=show_problem&category=478&problem=3078&mosmsg=Submission+received+with+ID+13891171" target="_blank" style="">题目链接 题意:给定一个有向图,结点上有一些石头,两人轮流…
体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; + ; ; int n, m, pos; unsigned long long H[maxn], xp[maxn]; unsigned long long hash[maxn]…
一开始不懂啊,什么Home键,什么End键,还以为相当于括号,[]里的东西先打印出来呢.后来果断百度了一下. 悲催啊... 题意:给定一个字符串,内部含有'['和']'光标转移指令,'['代表光标移向文章头,']'代表光标移向文章尾,问最终在屏幕上显示的字符串序列是什么 思路:直接模拟一下即可,不过因为由于会可能移动到字符串开头以及结尾,所以我开了3个char数组. str1存储由于'['的原因而打印在开头的字符,str3存储由于']'的原因打印在结尾的字符,str2则是存储一开始在没遇到'['…
 L-system  A D0L (Deterministic Lindenmayer system without interaction) system consists of a finite set  of symbols (the alphabet), a finite set P of productions and a starting string  . The productions in P are of the form  , where  and  (u is calle…
UVa-272. Description: TEX is a typesetting language developed by Donald Knuth. It takes source text together with a few typesetting instructions and produces, one hopes, a beautiful document. Beautiful documents use " and " to delimit quotations…
Division  Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0through 9 once each, such that the first number divided by the second is equal to an integer N, where. That is, abcde / fghij = N where e…
题意:有n个单词,每个单词长度为k,顺时针将它们写成一个圆圈串.现在知道g个长度为k的单词,是否可以从这g个单词中选择n个形成这个圆圈串?如果有多个答案,任意输出一个. 思路 可以发现,如果枚举第一个串的开始位置,由于输入的g个串,长度都为k,那么每个串的位置就固定了.那么我只要知道,在主串上那一段位置的字符串,是否存在在g个串中,然后如果每个都存在,那么就是符合的.这一部分可以用字符串hash做到O(1)判断. 复杂度的话,枚举第一个串的复杂度是k,一共需要匹配n个字符串,所以总复杂度是O(n…
https://vjudge.net/problem/UVA-11107 题意:给定n个字符串,求出现在不小于n的一半个字符串的最长子串,如果有多个,则按字典序输出. 思路: 首先就是将这n个字符串连接起来,然后二分答案,每次只需要判断是否有一个长度为p的串在超过一半的串中连续出现,判断方法是扫描一遍height数组,把它分成若干段,每当height[i]小于p时开辟一个新段,则每一段的最初p个字符均相同.只要某一段中包含了超过n/2个原串的后缀,p就是满足条件的. #include<iostr…
  Updating a Dictionary  In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, and values are non-negative integers. Given an old dictionary and a new dictionary, find out what were changed. Each dictionar…
Description  Games Are Important  One of the primary hobbies (and research topics!) among Computing Science students at the University of Alberta is, of course, the playing of games. People here like playing games very much, but the problem is that t…
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: //二叉树的中序和后序遍历还原树并输出最短路径的叶子节点…