欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

A. Juggling Letters(字符串问题)Codeforces Round #666 (Div. 2)

程序员文章站 2022-03-08 14:17:27
...

原题链接: https://codeforces.com/contest/1397/problem/A

A. Juggling Letters(字符串问题)Codeforces Round #666 (Div. 2)

测试样例:

input
4
2
caa
cbb
3
cba
cba
cbb
4
ccab
cbac
bca
acbcc
4
acb
caf
c
cbafc
output
YES
NO
YES
NO

Note

In the first test case, you can do the following:

  • Remove the third character of the first string and insert it after the second character of
    the second string, making the two strings “ca” and “cbab” respectively.
  • Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to “cab”.

In the second test case, it is impossible to make all nnstrings equal.

题意: 给你若干个字符串,要求你判断能否经过若干操作将这些字符串都变成相同的字符串。

解题思路: 水题一道。由于字符串中的字符是可以任意变换的,所以我们可以随意分配,那么我们只要统计这些字符串中所有字符出现的次数,如果能平分nn次那么说明我们可以使得这些字符串都相同,否则不行

AC代码:

/*
*邮箱:aaa@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
        while(t--){
            map<char,int> p;
            cin>>n;
            string s;
            rep(i,0,n-1){
                cin>>s;
                int len=s.size();
                rep(j,0,len-1){
                    p[s[j]]++;
                }
            }
            int flag=true;
            rep(i,0,25){
                if(p['a'+i]%n!=0){
                    flag=false;
                    break;
                }
            }
            if(flag)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
	}
	return 0;
}

相关标签: # CF 字符串