codeforces 1092C Prefixes and Suffixes【字符串+思维】

博客围绕Codeforces上的一道字符串题目展开。作者读错题意三小时,正确题意是判断2*n - 2个字符串是前缀还是后缀。解题思路为取出两个n - 1长的字符串,判断其与前缀的匹配程度确定所需字符串,同时要考虑回文串情况,用cnt数组标记,保证p和s数量为n - 1。

题目链接:点这里

题意:这题意我读错了三个小时...最后到网上找题解的时候才发现读错了QAQ暴击

题意是判断给你的2*n-2个字符串是前缀还是后缀,不是判断这个字符串的内容...我真的欲哭无泪,理解能力太菜了

思路:将两个n-1长的字符串取出,先判断第一个取出的字符串和给出的字符串前缀的匹配程度。如果匹配程度大于半数,则这个为所需字符串-1,否则就是另外一个。同时要注意回文串的情况,所以开了一个cnt数组进行标记,其中p和s的数量要刚好等于n-1

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<set>
#include<memory.h>
#include<algorithm>
using namespace std;
string s[200];
int cnt[200];
int main()
{
	int n;
	string st1 = "", st2 = "", d;
	cin >> n;
	memset(cnt, 0, sizeof(cnt));
	for (int i = 1; i <= 2 * n - 2; i++)
	{
		cin >> s[i];
		if (s[i].size() == n - 1)
		{
			if (st1.size() == 0)
				st1 = s[i];
			else
				st2 = s[i];
		}
	}
	int co = 0;
	for (int i = 1; i <= 2 * n - 2; i++)
	{
		if (st1.substr(0, s[i].size()) == s[i])
			co++;
	}
	if (co >= n - 1 && st1.substr(1, n - 1) == st2.substr(0, n - 2))
		d = st1;
	else
		d = st2;
	for (int i = 1; i <= 2 * n - 2; i++)
	{
		if (s[i] == d.substr(0, s[i].size()) && cnt[s[i].size()] != 1)
		{
			cnt[s[i].size()] = 1;
			cout << "P";
		}
		else
			cout << "S";
	}
}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值