C. Beautiful Lyrics (analog)

C. Beautiful Lyrics

 

(1) ideas:

The n string divided into two categories,

The same number at the end of the vowel, and the end is divided into a class of vowels; t1 is the number of

Otherwise, the end of the same into a number of categories vowel; t2 is the number of

If t1 is greater than t2, t1 will be placed behind the back of the extra t2.

It should be evenly distributed (at that time may be wrong here, it has been an error).

 

(2) Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1e5+10;
string ss[maxn];
int vis[maxn]={0};
struct Node{
	int id,num;
	char ch;
}cur[maxn];
vector <Node> vc;
struct node{
	int p1,p2;
};
node v1[maxn],v2[maxn];
bool cmp(Node a,Node b){
	if(a.num!=b.num) return a.num<b.num;
	return a.ch<b.ch;
}
int MIN(int x,int y){
	return x<y?x:y;
}
int main(void){
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		cin>>ss[i];
		cur[i].id = i;
		cur[i].num = 0;
		int len = ss[i].length();
		for(int j=0;j<len;j++)
			if(ss[i][j]=='a'||ss[i][j]=='e'||ss[i][j]=='i'||ss[i][j]=='o'||ss[i][j]=='u'){
				cur[i].num++;cur[i].ch = ss[i][j];
			}
	}
	sort(cur+1,cur+1+n,cmp);
	int t1 = 0,t2 = 0;
	vc.clear();
	for(int i=2;i<=n;i++)
	if(vis[i]==0&&vis[i-1]==0&&cur[i].ch==cur[i-1].ch&&cur[i].num==cur[i-1].num){
		vis[i] = vis[i-1] = 1;
		v1[++t1] = {cur[i-1].id,cur[i].id};
	}
	for(int i=1;i<=n;i++)
	if(vis[i]==0) vc.push_back(cur[i]);
	else vis[i] = 0;
	
	int m = vc.size();
	for(int i=1;i<m;i++)
	if(vis[i]==0&&vis[i-1]==0&&vc[i-1].num==vc[i].num){
		vis[i] = vis[i-1] = 1;
		v2[++t2] = {vc[i-1].id,vc[i].id};
	}
	
	if(t1>t2){
		int x = (t1-t2)/2;
		for(int i=1;i<=x;i++) v2[t2+i] = v1[t1-i+1];
		t2 += x;
		t1 -= x;
	}
	int len = MIN(t1,t2);
	cout<<len<<endl;
	for(int i=1;i<=len;i++){
		cout<<ss[v2[i].p1]<<" "<<ss[v1[i].p1]<<endl;
		cout<<ss[v2[i].p2]<<" "<<ss[v1[i].p2]<<endl;
	}
	
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41829060/article/details/92077440