zoukankan      html  css  js  c++  java
  • 【20.73%】【CF 716B】Complete the Word

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than 26, no such substring exists and thus it is not nice.

    Now, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him?

    Input

    The first and only line of the input contains a single string s (1 ≤ |s| ≤ 50 000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet ('A'-'Z') or is a question mark ('?'), where the question marks denotes the letters that ZS the Coder can't remember.

    Output

    If there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print  - 1 in the only line.

    Otherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters.

    If there are multiple solutions, you may print any of them.

    Examples
    input
    ABC??FGHIJK???OPQR?TUVWXY?
    
    output
    ABCDEFGHIJKLMNOPQRZTUVWXYS
    input
    WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO
    
    output
    -1
    input
    ??????????????????????????
    
    output
    MNBVCXZLKJHGFDSAQPWOEIRUYT
    input
    AABCDEFGHIJKLMNOPQRSTUVW??M
    
    output
    -1
    Note

    In the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length 26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.

    In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 that contains all the letters of the alphabet, so the answer is  - 1.

    In the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer.

    【题解】

    这题我用线段树+前缀和A的。。。

    看到别人直接模拟过。我只能说他们的心真大。

    可能我这个算法N的范围还能扩大100倍吧

    题意是说让你找一个长度为26的子串。要求这26个字符中包括了26个大写字母。如果不够可以用问号来替代任意一个字母凑够26个。如果不存在则输出-1;

    我们用前缀和来sum[i]来记录前i个字符里面有多少个问号。

    然后枚举起点。+25获得终点。

    用线段树快速获取起点到终点里面有多少种不同的字符(不包括问号);

    然后再用sum[终点]-sum[起点-1]获取这个区间里面的问号个数。

    如果不同字符加上问号个数为26则找到解。

    否则起点递增,继续找。

    这个不同的字符可以用bitset的|操作得到。

    具体看代码

    【代码】

    #include <cstdio>
    #include <cstring>
    #include <bitset>
    #define lson begin,m,rt<<1
    #define rson m+1,end,rt<<1|1
    
    using namespace std;
    
    const int MAX_SIZE = 50010;
    
    struct point
    {
    	bitset <27> jihe;
    };
    
    point a[MAX_SIZE * 4];
    char s[MAX_SIZE];
    int len,sum[MAX_SIZE];
    
    void push_up(int rt)
    {
    	a[rt].jihe.reset();
    	a[rt].jihe |= a[rt << 1].jihe;
    	a[rt].jihe |= a[rt << 1 | 1].jihe;
    }
    
    void build(int begin, int end,int rt)
    {
    	if (begin == end)
    	{
    		a[rt].jihe.reset();
    		if (s[begin - 1] != '?') //注意是问号就不记录
    		{
    			int x = s[begin - 1] - 'A' + 1;
    			a[rt].jihe[x] = 1;
    		}
    		return;
    	}
    	int m = (begin + end) >> 1;
    	build(lson);
    	build(rson);
    	push_up(rt);
    }
    
    bitset <27>  query(int l, int r, int begin, int end, int rt) //返回的是一个bitset
    {
    	if (l <= begin && end <= r)
    		return a[rt].jihe;
    	int m = (begin + end) >> 1;
    	bitset <27> temp1;
    	temp1.reset();
    	if (l <= m)
    		temp1 |= query(l, r, lson);
    	if (m < r)
    		temp1 |= query(l, r, rson);
    	return temp1;
    }
    
    void set_all()
    {
    	for (int i = 0; i <= len - 1; i++)
    		if (s[i] == '?')
    			s[i] = 'A';
    }
    
    int main()
    {
    	//freopen("F:\rush.txt", "r", stdin);
    	scanf("%s", s);
    	len = strlen(s);
    	if (len < 26)
    	{
    		printf("-1
    ");
    		return 0;
    	}
    	build(1, len,1);
    	sum[0] = 0;
    	for (int i = 1; i <= len; i++)
    	{
    		sum[i] = sum[i - 1];
    		if (s[i - 1] == '?')
    			sum[i]++;
    	}
    	bitset <27> temp;
    	for (int i = 1; i <= len-25; i++)
    	{
    		int left = i, right = i + 25;
    		int wenhao = sum[right] - sum[left - 1];
    		temp = query(left, right, 1, len, 1);
    		int num = temp.count();
    		if (temp == 26)
    		{
    			set_all();
    			printf("%s", s);
    			printf("
    ");
    			return 0;
    		}
    		else
    			if (num + wenhao == 26)
    			{
    				for (int j = left-1;j <= right-1;j++)
    					if (s[j] == '?')
    					{
    						for (int k = 1; k <= 26; k++)//哪些没填 就按顺序填上字母
    							if (temp[k] == 0)
    							{
    								s[j] = k+'A'-1;
    								temp[k] = 1;
    								break;
    							}
    					}
    				set_all();
    				printf("%s", s);
    				printf("
    ");
    				return 0;
    			}
    	}
    	printf("-1
    ");
    	return 0;
    }



  • 相关阅读:
    VC窗口类的销毁-是否需要delete
    ScrollView在调试状态一点击就挂的原因(OnMouseActivate)
    TextOut与DrawText的区别
    NOIP2010 引水入城
    欧拉回路
    BZOJ 1202: [HNOI2005]狡猾的商人
    codevs 2491 玉蟾宫
    BZOJ 1059: [ZJOI2007]矩阵游戏
    BZOJ 1024: [SCOI2009]生日快乐
    ural 1297. Palindrome
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632241.html
Copyright © 2011-2022 走看看