SGU 142.Keyword

时间限制:0.5s

空间限制:16M

题意

给出一个仅由‘a‘,‘b’组成的字符串S,长度小于500 000,求一个由‘a’,‘b’组成的不是S子串的字符串T。

输出T的长度和T。

Sample Input

11
aabaaabbbab

Sample Output

4
aaaa


 

Solution:

从字符串长度上看,我们需要一个O(n)的算法.

考虑串T的最大长度,在什么范围

长度为k的串,有2k个,占了k*2k位,即1*2+2*2k,+....+p*2P<=500 000;

可知串T的长度是小于log2(500000)<19,的.

因此只要从S串的开始,每一位往后最多枚举19位,就能筛选出所有有用的子串.

如果用 1 0 分别代表‘a‘,‘b‘ ;

那么可以用一个二进制长为19位的数代表这个串.

只需要开一个f[length][key]的数组(length<=19,key<=219),注意到内存只有16M,因此,这个二维数组的类型必须是bool型,不然将超出内存限制.

最后从数组中找到最短的那个没有出现过的串,输出即可.

时间复杂度正是O(n),满足我们的需要的.

参考代码

#include <cstdio>
#include <cmath>
char ch;
int g[1 << 19];
bool f[20][1 << 19];
int k, n, len, fid;
int main() {
	scanf ("%d", &n);
	ch = getchar();
	for (int i = 1; i <= n; i++)
		g[i] = (ch=getchar() == ‘a‘);
	for (int i = 1; i <= n; i++) {
		k = 0;
		for (int j = 0; j <= 18; j++) {
			if (i + j <= n) {
				k = k << 1 | g[i + j];
				f[j + 1][k] = 1;
			}
			else break;
		}
	}
	for (len = 1; len <= 19; len++) {
		for (k = 0; k < (1<<len); k++)
			if (!f[len][k]) {
				fid = 1;
				break;
			}
		if (fid) break;
	}
	printf ("%d\n", len);
	for (int i = len - 1; i >= 0; i--)
		printf ("%c", k & (1 << i) ? ‘a‘ : ‘b‘);
}

  

SGU 142.Keyword

时间: 2024-11-04 22:22:08

SGU 142.Keyword的相关文章

SGU 乱乱开

本解题报告 乱抄,乱写,随性随心,不喜多喷! SGU 142: 思路:一个string的字串不会超过2^20个,我们枚举出来就好了. 我出错点:数组RE 1 #include<stdio.h> 2 #include<math.h> 3 #include<algorithm> 4 #include<string.h> 5 #include<string> 6 #include<iostream> 7 #include<set>

今日SGU 5.19

SGU 142 题意:给你一个长度为n的串(由a,b组成),让你找出一个串不是n的子串,长度最下 收获:思维题,思路在代码里 #include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define r

SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流

The Glorious Karlutka River =) Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 438 Appoint description: Description A group of Mtourists are walking along the Karlutka river. They want to cross

sgu Kalevich Strikes Back

这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #define maxn 120100 6 using namespace std; 7 8 long long s[maxn]; 9 vector<int>g[maxn]; 10 i

sgu 463 - Walking around Berhattan

K - Walking around Berhattan Time Limit:250MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 463 Description As you probably know, Berhattan is a district of Berland's largest city and it consists of equal squar

C#+无unsafe的非托管大数组(large unmanaged array in c# without &#39;unsafe&#39; keyword)

C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一个大数组(Use a large array in C#) 在C#里,有时候我需要能够申请一个很大的数组.使用之.然后立即释放其占用的内存. Sometimes I need to allocate a large array, use it and then release its memory

[ES6] 03. The let keyword -- 1

var message = "Hi"; { var message = "Bye"; } console.log(message); //Bye The message inside the block still has impact on the outside. If you add function around the inside message: var message = "Hi"; function greeting(){ va

python2和python3中的关键字的区别--keyword模块

一.python3.6中的: 共33个关键字: 通过导入keyword模块,查看python所有的关键字.在ipython中: Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]Type "copyright", "credits" or "license" for more information.

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor