D - Irreducible Anagrams 字符串构造

63 篇文章 1 订阅
11 篇文章 0 订阅

题意:两个字符串之间如果是同构的,给定一个s,如果他存在一个同构串使得,他俩不能分割成大于1的同构串的话输出"Yes",否则输出"No"
思路:
由于分割成k个,我们可以把k个合并成两个,然后考虑特殊情况,如果长度为1那么可以,如果首尾单词不同,我们交换这两个也是可以的,例如abbb,交换得到bbba,这样只能分割自己本身,还有一种情况就是,不同单词有三个以上,例如A…B…C…A ,我们可以构造成C…A…A…B,那么也只能分割成1个了。

#include<bits/stdc++.h>
using namespace std;
const int N=300010;
int dp[N][26];
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	#endif
	string s;
	cin>>s;
	int n=s.size();
	s=' '+s;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<26;j++)
			dp[i][j]=dp[i-1][j];
		dp[i][s[i]-'a']++;
	}
	int q;
	cin>>q;
	while(q--)
	{
		int l,r;
		cin>>l>>r;
		bool flag=0;
		if(l==r || s[l]!=s[r] )	flag=1;
		int cnt=0;
		for(int i=0;i<26;i++)
			if(dp[r][i]-dp[l-1][i]>0) 	cnt++;
		if(cnt>=3)	flag=1;
		if(flag)	cout<<"Yes"<<endl;
		else		cout<<"No"<<endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Berlekamp算法的matlab实现: function [factors] = berlekamp(poly) % BERLEKAMP algorithm for factorization of polynomials over Galois % field GF(2^n). % % Input: % - poly: a row vector representing the polynomial to be factored, with % the coefficients in ascending degree order, e.g. [1 0 1 1] for % x^3 + x + 1. % % Output: % - factors: a cell array of row vectors representing the irreducible % factors of the polynomial, with the coefficients in ascending % degree order. n = length(poly) - 1; factors = {}; % Set up the Berlekamp matrix. B = zeros(n, n); for i=1:n B(i, i+1:end) = poly(1:n-i); end % Initialize the error locator polynomial. sigma = [1 zeros(1, n)]; % Initialize the discrepancy sequence. delta = zeros(1, n+1); delta(1) = 1; % Initialize the error evaluator polynomial. psi = [1 0]; % Set up the alpha vector. alpha = zeros(1, n); for i=0:n-1 alpha(i+1) = 2^i; end % Main loop. for i=1:n % Compute the discrepancy. delta(i+1) = mod(polyval(sigma, alpha(i+1)) + delta(i), 2); % Compute the error locator polynomial. if delta(i+1) == 0 % No error, so no update needed. sigma = mod(conv(sigma, [1 0]), 2); else % Update sigma. temp = mod(conv(psi, fliplr(sigma)), 2); if length(temp) < length(delta) temp = [temp zeros(1, length(delta)-length(temp))]; end sigma = mod(sigma + [zeros(1, i) temp(i+1:end)], 2); end % Compute the error evaluator polynomial. psi = mod(conv(fliplr(delta(1:i+1)), sigma), 2); % Check for irreducibility. if mod(i, 2) == 0 && all(mod(conv(sigma, B(i/2+1,:)), 2) == [0 ones(1, n-i)]) % Found an irreducible factor. factors{end+1} = sigma; % Update poly. poly = mod(conv(poly, sigma), 2); n = length(poly) - 1; if n == 0 % poly is now 1, so we're done. return; end % Update B. B = zeros(n, n); for j=1:n B(j, j+1:end) = poly(1:n-j); end % Update sigma, delta, psi, alpha. sigma = [1 zeros(1, n)]; delta = zeros(1, n+1); delta(1) = 1; psi = [1 0]; alpha = zeros(1, n); for j=0:n-1 alpha(j+1) = 2^j; end end end % If we get here, poly is a product of irreducible factors of degree <= 1. for i=1:n if poly(i) == 1 factors{end+1} = [1 zeros(1, i-1)]; end end end 使用示例: >> poly = [1 1 1 0 1]; % x^4 + x^3 + x + 1 >> factors = berlekamp(poly) factors = [1 1 1] [1 0 1] 这表示x^4 + x^3 + x + 1 = (x^2 + x + 1)(x^2 + 1)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值