#include <iostream>
#include <string>
using namespace std;
int NWD(int a, int b)
{
int pom;
while(b!=0)
{
pom = b;
b = a%b;
a = pom;
}
return a;
}
int main()
{
int n, m, tot, k, i, j, l, l2;
cin >> n ;
int t[n];
int pass[m];
string a;
int simple;
for (k = 0; k < n; k++)
{
cin >> t[k];
}
cin >> m;
tot = n*m / NWD(n,m);
simple =0;
cin >> a;
for (k = 0; k < m; k++)
{
if (a[k]=='W') pass[k]=1;
if (a[k]=='P') {pass[k]=-1;simple = 1;}
}
//cout << pass[2]<<endl;
//cout << tot;
i=0;
j=0;
l=1;
l2=1;
//cout << "stast" << pass[0] <<endl;
if (simple ==1){
//cout << tot << endl;
while (l<=tot){
//cout << pass[j] << endl;
if (pass[j]==1) t[i]=t[i]+1;
if (pass[j]==-1) {
t[i]=t[i]-1;
if (t[i]==0) {cout << l2;
break;}
}
//cout <<"po ruchu " << l << " w portfelu chlopca " << i << " jest "<< t[i] << endl;
i++; j++;l++;l2++;
if (i>=n) i=i%n;
if (j>=m) j=j%m;
if(l=tot) l=1;
}}
if ((t[i]>0)||(simple==0)) cout << -1;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <iostream> #include <string> using namespace std; int NWD(int a, int b) { int pom; while(b!=0) { pom = b; b = a%b; a = pom; } return a; } int main() { int n, m, tot, k, i, j, l, l2; cin >> n ; int t[n]; int pass[m]; string a; int simple; for (k = 0; k < n; k++) { cin >> t[k]; } cin >> m; tot = n*m / NWD(n,m); simple =0; cin >> a; for (k = 0; k < m; k++) { if (a[k]=='W') pass[k]=1; if (a[k]=='P') {pass[k]=-1;simple = 1;} } //cout << pass[2]<<endl; //cout << tot; i=0; j=0; l=1; l2=1; //cout << "stast" << pass[0] <<endl; if (simple ==1){ //cout << tot << endl; while (l<=tot){ //cout << pass[j] << endl; if (pass[j]==1) t[i]=t[i]+1; if (pass[j]==-1) { t[i]=t[i]-1; if (t[i]==0) {cout << l2; break;} } //cout <<"po ruchu " << l << " w portfelu chlopca " << i << " jest "<< t[i] << endl; i++; j++;l++;l2++; if (i>=n) i=i%n; if (j>=m) j=j%m; if(l=tot) l=1; }} if ((t[i]>0)||(simple==0)) cout << -1; return 0; } |
English