【Codeforces】716B Complete the Word (26个字母)

http://codeforces.com/contest/716/problem/B

给你一个字符串该字符串中是否存在长度为26且这26个字母没有重复

一个满足上述条件但是部分区域是问号的话,需要用剩下的字母覆盖掉问号,其余部分的问号可以随便赋值

没有的话输出-1

暴力即可。

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
char s[50010];
int main ()
{
	scanf("%s",&s);
	int len = strlen(s);
	set<char> ss;
	int F = 0;
	int f;
	int i,j,cnt;
	for(i=0;i<len-25;i++)
	{
		ss.clear();
		cnt = 0;
		f = 1;
		for(j=i;j<i+26;j++)
		{
			if(s[j]=='?')
				cnt++;
			else
			{
				if(ss.count(s[j])==0)
					ss.insert(s[j]);
				else
				{
					f = 0;
					break; 
				} 
			}
		} 
		if(f && ss.size()+cnt==26)
		{
			F = 1;
			break;
		}
	}
	if(F)
	{
		char k;
		for(j=i;j<i+26;j++)
		{
			if(s[j]=='?')
			{
				for(k='A';k<='Z';k++)
				{
					if(ss.count(k)==0)
					{
						ss.insert(k);
						s[j] = k;
						break;
					}
				}
			}
		}
		for(i=0;i<len;i++)
		{
			if(s[i]=='?')
				s[i] = 'A';
		}
		printf("%s",s);
	}
	else
	{
		printf("-1");
	}
	return 0;
} 
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int main ()
{
	string s;
	cin >> s;
	if(s.length()<26)
	{
		cout << "-1";
		return 0;
	}
	int flag = 0;
	int cnt;
	int i,j,k;
	for(i=0;i<s.length()-25;i++)
	{
		string t = s.substr(i,26);
		set<char> s1;
		map<char,int> m1;
		cnt = 0;
		for(j=0;j<26;j++)
		{
			if(t[j] == '?')
				cnt++;
			else
			{
				s1.insert(t[j]);
				m1[t[j]]++;
			}
		}
		if(s1.size()+cnt==26)
		{
			flag = 1;
			for(j=0;j<i;j++)
			{
				if(s[j]=='?')
					s[j] = 'A';
			}
			for(j=i+26;j<s.length();j++)
			{
				if(s[j]=='?')
					s[j] = 'A';
			}
			queue<char> q;
			for(j='A';j<='Z';j++)
			{
				if(!m1[j])
				{
					q.push(j);
				}
			}
			for(j=i;j<i+26;j++)
			{
				if(s[j]=='?')
				{
					s[j]=q.front();
					q.pop();
				}
			}
		}
		if(flag)
			break;
	} 
	if(flag)
		cout << s ;
	else
		cout << "-1";
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值