“强智杯“2020年湖南省大学生计算机程序设计竞赛 D.String Commutativity

链接 https://ac.nowcoder.com/acm/problem/214395

Bobo has n strings s1, … , sn, and he would like to find the number of pairs i < j where si + sj = sj + si.
Note that a + b means the concatenation of the string a and b, i.e., writing the string a first, and the string b second.
输入描述:
The input consists of several test cases terminated by end-of-file.
The first line of each test case contains an integer n. The i-th of the following n lines contains a string si.
· 1 ≤ n ≤ 105
· |si| ≤ 106, si contains only lower case characters.
· The sum of strings does not exceed 5×106
输出描述:
For each test case, print an integer which denotes the result.
示例1
输入
2
a
ab
2
ab
ab
3
a
aa
aaa
输出
0
1
3

题意
从n个字符串中找出si,sj两个字符串进行拼接,满足i<j,如果si+sj = sj+si,则为一对,统计有多少对。

思路
我们将字符串化简,比如说aaa可以化简为a,将循环部分去掉,我们还发现只有化简后相同的才满足,那我们就自然做出来了,我们用map<string,int> vis 记录之前个数,每次答案先加,再vis自加。
我们现在唯一问题来了,怎么化简,我们想到可以用next数组来做,通过(len)/(len-nxt[len])来找到循环节,注意不能整除说明没有!这时候子串长度就是len除以循环节,也可以len-nxt[len]加判断求出。

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 1e6 + 5;

int Case,n,len;
string s;
int nxt[N];

void solve(){
	map<string,int> vis;
	ll res = 0;
	for(int i=1;i<=n;i++){
		cin>>s;
		len =s.size();
		int t = len;
		int j=0,ans,k=-1;
        len=s.size();
        nxt[0]=-1;
        while(j<=len){
            if(k==-1||s[j]==s[k])
                nxt[++j]=++k;
            else
                k=nxt[k];
        }
        ans = len%(len-nxt[len])?1:len/(len-nxt[len]);
        s=s.substr(0,len/ans);
		res += vis[s]; 
        vis[s]++;
	}
	printf("%lld\n",res);
}

int main(){
	//init();
	Case=1;
	//scanf("%d",&Case);
	while(~scanf("%d",&n)){
		solve();
 	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1) To prove that [B; ∪ , ∩ ,~] is a Boolean algebra, we need to verify the following axioms: - Closure under ∪ and ∩ operations: For any two sets X, Y in B, X ∪ Y and X ∩ Y are also in B. - Commutativity of ∪ and ∩ operations: For any two sets X, Y in B, X ∪ Y = Y ∪ X and X ∩ Y = Y ∩ X. - Associativity of ∪ and ∩ operations: For any three sets X, Y, Z in B, (X ∪ Y) ∪ Z = X ∪ (Y ∪ Z) and (X ∩ Y) ∩ Z = X ∩ (Y ∩ Z). - Distributivity of ∪ and ∩ operations: For any three sets X, Y, Z in B, X ∪ (Y ∩ Z) = (X ∪ Y) ∩ (X ∪ Z) and X ∩ (Y ∪ Z) = (X ∩ Y) ∪ (X ∩ Z). - Complementation: For any set X in B, there exists a complement set X' in B such that X ∪ X' = A (the universal set) and X ∩ X' = ∅ (the empty set). - Identity elements: A and ∅ are the identity elements for ∪ and ∩ operations, respectively. All of these axioms hold for [B; ∪ , ∩ ,~], so we can conclude that [B; ∪ , ∩ ,~] is a Boolean algebra. 2) The operation tables for the Boolean algebra [B; ∪ , ∩ ,~] can be written as follows: ∪ | a b c ------|----- a | a b c b | b b c c | c c c ∩ | a b c ------|----- a | a a a b | a b ∅ c | a ∅ c ~ | a b c ------|------- a| ∅ b c b| a ∅ c c| a b ∅ In the tables, ∪ represents the union operation, ∩ represents the intersection operation, and ~ represents the complement operation. The rows and columns represent the elements in set B (i.e., the power set of A). For example, in the ∪ table, the cell in the first row and second column (b) represents the result of taking the union of sets {a} and {b}, which is {a, b}.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值