TCS Codevita 2020 Discussion

Do you really think some problems doesn’t has clear statement and has confusing test cases.
(though i don’t even like their interface) .

Anyone has “Election queue”, “String matching” set, please share your approach, it would we very helpful.

Election Queue solution Accepted one

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\n'
#define rep(i,n) for(int i=0;i<n;i++) 
#define all(v) v.begin(),v.end()
#define F first
#define S second
void solve()
{
	int n;
	cin>>n;
	char arr[n];
	for(int i=0;i<n;i++)
	{
		char c;
		cin>>c;
		arr[i]=c;
	}
	ll countA=0;
	ll countB=0;
	ll count=0;
	bool tempb=false;
	for(int i=0;i<n;i++)
	{
		if(arr[i]=='-')
		{
			if(i-1>=0 && arr[i-1]=='B' && !tempb)
			{
				tempb=true;
			}
			count++;
		}
		else if(arr[i]=='A')
		{
			countA++;
			if(count>0)
			{
				if(tempb)
				{
					countA+=count/2;
					countB+=count/2;
					count=0;
					tempb=false;
				}
				else
				{
					countA+=count;
					count=0;
				}
			}
		}
		else
		{
			countB++;
			if(count>0 && tempb)
			{
				countB+=count;
				tempb=false;
			}
			count=0;
		}
	}
	if(count>0 && tempb)
	{
		countB+=count;
	}
		//cout<<countA<<" "<<countB<<endl;
	
	if(countA==countB)
	{
		cout<<"Coalition government"<<endl;
	}
	else if(countA>countB)
	{
		cout<<"A"<<endl;
	}
	else
	{
		cout<<"B"<<endl;
	}
}
int main()
{
//	ios_base::sync_with_stdio(false); 
  //  cin.tie(NULL);  
	int t;
	t=1;
	while(t--)
	{
		solve();
	}
}

I tired string matching but it didnt got accepted

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\n'
#define rep(i,n) for(int i=0;i<n;i++) 
#define all(v) v.begin(),v.end()
#define F first
#define S second
ll fun(string s)
{
  ll count=0;
  for(int i=0;i<(int)s.length();i++)
  {
  	if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')
  	{
  		count++;
  	}
  }
  return count;
}
void solve()
{
  int n;
  cin>>n;
  int arr[n];
  string s[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen",
"fourteen","fifteen","sixteen","seventeen",	"eighteen",	"nineteen","twenty","twenty one","twenty two","twenty three","twenty four","twenty five",
"twenty six",
"twenty seven",
"twenty eight",
"twenty nine",
"thirty",
"thirty one",	
"thirty two",	
"thirty three",
"thirty four",
"thirty five",
"thirty six",	
"thirty seven",
"thirty eight",
"thirty nine",
"forty","forty one","forty two","forty three","forty four","forty five","forty six","forty seven","forty eight","forty nine","fifty","fifty one","fifty two",
"fifty three","fifty four","fifty five","fifty six","fifty seven","fifty eight","fifty nine","sixty","sixty one","sixty two","sixty three","sixty four","sixty five","sixty six","sixty seven",
"sixty eight","sixty nine","seventy","seventy one","seventy two","seventy three","seventy four","seventy five","seventy six","seventy seven","seventy eight","seventy nine",
"eighty","eighty one","eighty two","eighty three","eighty four","eighty five","eighty six","eighty seven","eighty eight","eighty nine","ninety","ninety one","ninety two","ninety three",
"ninety four","ninety five","ninety six","ninety seven","ninety eight","ninety nine","hundred"};
  for(int i=0;i<n;i++)
  {
  	cin>>arr[i];
  	//cout<<s[arr[i]]<<endl;
  }
  vector<ll> val;
  for(int i=0;i<n;i++)
  {
  	val.push_back(fun(s[arr[i]]));
  }
  ll sum=0;
  for(int i=0;i<(int)val.size();i++)
  {
  	sum+=val[i];
  }
  //sort(arr,arr+n);
  ll res=0;
  set<pair<ll,ll>> se;
  vector<bool> vis(n,false);
  for(int i=0;i<n;i++)
  {
  		for(int j=i+1;j<n;j++)
  		{
  		
  			if(!vis[i] && !vis[j] && arr[i]+arr[j]==sum)
  			{
  				res++;
  				vis[i]=true;
  				vis[j]=true;
  			}
  		}
  }
  if(res>100)
  {
  	cout<<"greater 100"<<endl;
  }
  else
  {
  	
  	cout<<s[res]<<endl;
  	
  }
  
}
int main()
{
//	ios_base::sync_with_stdio(false); 
//  cin.tie(NULL);  
  int t;
  t=1;
  while(t--)
  {
  	solve();
  }
}

same, i solved Election one as well but didn’t get string matching accepted.
may be there are some corner cases which we are missing.

string Matching description was not all cleared it does not says about to print like twenty-six or twenty size and also about the unordered pairs

My Election queue AC solution

/*
#pragma GCC optimize(“Ofast”)
#pragma GCC target(“avx,avx2,fma”)
#pragma GCC optimization (“unroll-loops”)
*/

// #pragma GCC optimize “trapv”

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define all(a) begin(a), end(a)
#define deb(x) cout << #x << " = " << x << endl
#define debu(x, y) cout << "( " << #x << ", " << #y << " ) = ( " << x << “, " << y << " )” << endl

typedef long long ll;
ll MOD=1000000007;

// template vector readVector(int n) { vector res(n); for (int i = 0 ; i < n ; i++) cin >> res[i]; return res; }

signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int a=0, b=0;

vector<int> mark(n+5, 0);
deque<int> A, B;
for(int i=0; i<n; i++) {
    if(s[i] == 'A') {
        a++;
        mark[i] = 1;
        A.pb(i);
    } else if(s[i] == 'B') {
        b++;
        mark[i] = 2;
        B.pb(i);
    }
}

int i, j;
i = j = -2;
i = 3*n;
while(A.size() > 0 || B.size() > 0) {

    //cout << "loop hell" << endl;
    if(A.size() > 0) {
        if(B.size() >= 2 && B[1] < A.front()) A.push_front(3*n);
        i = A.front();
    } else {
        /// i = 0;
    }
    if(B.size() > 0) {
        if(A.size() > 0 && B.front() > A.front()) B.push_front(-2);
        j = B.front();
    } else {
        /// j = n;
    }
    i--; j++;

    bool is = true;
    while(true) {
        if(!is) break;
        is = false;
        int ti = i;
        int tj = j;
        if(i >=0 && i < n) {

            if(mark[i] == 0 && j != i) {
                a++;
                mark[i] = 1;
                ti--;
                is = true;
            }
        }
        if(j < n && j >= 0) {
            if(mark[j] == 0 && i != j) {
                b++;
                mark[j] = 2;
                tj++;
                is = true;
            }
        }
        /// debu(i, j);
        i = ti;
        j = tj;
    }
    if(A.size() > 0) A.pop_front();
    if(B.size() > 0) B.pop_front();
}

/*
/// DEBUGING
for(int i=0; i<n; i++) {
    if(mark[i] == 0) cout << '-' << " ";
    else if(mark[i] == 1) cout << 'A' << " ";
    else cout << 'B' << " ";
}
cout << endl;
debu(a, b);
*/

if(a > b) {
    cout << 'A' << endl;
} else if( a < b) {
    cout << 'B' << endl;
} else {
    cout << "Coalition government" << endl;
}

return 0;

}

but way more complex than mine

do you know how many accepted solutions our set has.

def valid(pre, count, post):
counta= 0
countb = 0
if(pre==‘S’ and post==‘A’):
counta = count
elif(pre==‘S’ and post==‘B’):
countb = 0
elif(pre==‘A’ and post==‘A’):
counta = count
elif(pre==‘A’ and post==‘B’):
countb = 0
counta = 0
elif(pre==‘B’ and post==‘A’):
counta = count//2
countb = count//2
elif(pre==‘B’ and post==‘B’):
countb = count
elif(pre==‘A’ and post==‘E’):
counta = 0
elif(pre==‘B’ and post==‘E’):
countb = count
return(counta,countb)

n =int(input())
arr = “S”+input()+“E”
i = len(arr)
pre = arr[0]
count = 0
counta = 0
countb = 0
for i in range(len(arr)):
if(arr[i] == ‘-’):
count = count + 1
continue
else:
if(((arr[i] == ‘A’) or (arr[i] == ‘B’)) and pre==‘S’):
c = (valid(pre,count,arr[i]))
counta = counta + c[0]
countb = countb + c[1]
count = 0
pre = arr[i]
if((arr[i] == ‘A’) and (pre == ‘B’)):
c = (valid(pre,count,arr[i]))
counta = counta + c[0]
countb = countb + c[1]
count = 0
pre = arr[i]
elif((arr[i] == ‘A’) and (pre == ‘A’)):
c = valid(pre,count,arr[i])
counta = counta + c[0]
countb = countb + c[1]
count = 0
pre = arr[i]
elif((arr[i] == ‘B’) and (pre == ‘A’)):
c = valid(pre,count,arr[i])
count = 0
pre = arr[i]
elif((arr[i] == ‘B’) and (pre == ‘B’)):
c = valid(pre,count,arr[i])
counta = counta + c[0]
countb = countb + c[1]
count = 0
pre = arr[i]
elif((arr[i] == ‘E’)):
c = valid(pre,count,arr[i])
counta = counta + c[0]
countb = countb + c[1]
count = 0
pre = arr[i]
AC = counta + arr.count(‘A’)
BC = countb + arr.count(‘B’)
#print(AC,BC)
if(AC > BC):
print(‘A’,end="")
elif(AC < BC):
print(‘B’,end="")
elif(AC == BC):
print(‘Coalition government’,end="")

although its not a optimised code , bt passes private test case

#include
#include
using namespace std;

char election(string s){
int A=0, B=0;
char prev_char=’\0’;
int neutral=0;
int n=s.length();
for(int i=0; i<n; ++i){
if(s[i]==’-’) neutral++;
else{
if((prev_char==’\0’|| prev_char==‘A’) && s[i]==‘A’){A+=neutral+1; neutral=0;}
if((prev_char==’\0’|| prev_char==‘A’) && s[i]==‘B’){B++; neutral=0;}
if(prev_char==‘B’ && s[i]==‘B’){B+=neutral+1; neutral=0;}
if(prev_char==‘B’ && s[i]==‘A’){A+=neutral/2+1; B+=neutral/2; neutral=0;}
if(prev_char==‘B’ && s[i]==’\0’){B+=neutral; neutral=0;}
prev_char=s[i];
}
}
if(A>B) return ‘A’;
if(A<B) return ‘B’;
else return ‘\0’;
}

int main(){
string s="–AB–AB—A–";
char e=election(s);
if(e==’\0’) cout<<“Coalition government”;
else cout<<e;
return 0;}