output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s;
int main(){
cin>>s;
int len=s.size();
for(int i=0;i<len-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
}
for(int i=0;i<len-2;i++){
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
return 0;
} | ### Prompt
Generate a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s;
int main(){
cin>>s;
int len=s.size();
for(int i=0;i<len-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
}
for(int i=0;i<len-2;i++){
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
return 0;
}
``` |
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 100005
char s[MAXN];
int main()
{
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<=len-2;i++)
{
if(s[i]==s[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
if(s[i]==s[i+1])
{
printf("%d %d\n",i+1,i+2);
return 0;
}
}
printf("-1 -1\n");
return 0;
} | ### Prompt
In CPP, your task is to solve the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 100005
char s[MAXN];
int main()
{
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<=len-2;i++)
{
if(s[i]==s[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
if(s[i]==s[i+1])
{
printf("%d %d\n",i+1,i+2);
return 0;
}
}
printf("-1 -1\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s; cin >> s;
int n = s.size();
for(int i = 0; i < n; i++) {
if(i<n-1 && s[i] == s[i+1]) {
cout << i+1 << " " << i+2;
return 0;
}
if(i<n-2 && s[i] == s[i+2]) {
cout << i+1 << " " << i+3;
return 0;
}
}
cout << "-1 -1";
} | ### Prompt
Please formulate a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s; cin >> s;
int n = s.size();
for(int i = 0; i < n; i++) {
if(i<n-1 && s[i] == s[i+1]) {
cout << i+1 << " " << i+2;
return 0;
}
if(i<n-2 && s[i] == s[i+2]) {
cout << i+1 << " " << i+3;
return 0;
}
}
cout << "-1 -1";
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define for_(i,a,b) for(int i=(a);i<(b);++i)
int main() {
string S;
cin >> S;
int n = S.size();
if (n == 2 && S[0] == S[1]) {
puts("1 2");
exit(0);
}
for_(i,0,n-2) {
if (S[i] == S[i+1] || S[i] == S[i+2]) {
cout << i+1 << " " << i + 3 << endl;
exit(0);
}
}
puts("-1 -1");
} | ### Prompt
Please formulate a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define for_(i,a,b) for(int i=(a);i<(b);++i)
int main() {
string S;
cin >> S;
int n = S.size();
if (n == 2 && S[0] == S[1]) {
puts("1 2");
exit(0);
}
for_(i,0,n-2) {
if (S[i] == S[i+1] || S[i] == S[i+2]) {
cout << i+1 << " " << i + 3 << endl;
exit(0);
}
}
puts("-1 -1");
}
``` |
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[100100];
int main(){
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++){
if(s[i-1]==s[i]){
cout<<i-1<<" "<<i;
return 0;
}
else if(s[i]==s[i-2]){
cout<<i-2<<" "<<i;
return 0;
}
else if(i==n){
cout<<"-1 -1";
return 0;
}
}
} | ### Prompt
Develop a solution in cpp to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[100100];
int main(){
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++){
if(s[i-1]==s[i]){
cout<<i-1<<" "<<i;
return 0;
}
else if(s[i]==s[i-2]){
cout<<i-2<<" "<<i;
return 0;
}
else if(i==n){
cout<<"-1 -1";
return 0;
}
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
for(int i = 1; i<s.size(); i++)
if(s[i] == s[i - 1]) {
cout << i << " " << i + 1 << endl;
return 0;
}
for(int i = 2; i < s.size(); i++)
if(s[i] == s[i - 2]) {
cout << i - 1 << " " << i + 1 << endl;
return 0;
}
cout << "-1 -1\n";
return 0;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
for(int i = 1; i<s.size(); i++)
if(s[i] == s[i - 1]) {
cout << i << " " << i + 1 << endl;
return 0;
}
for(int i = 2; i < s.size(); i++)
if(s[i] == s[i - 2]) {
cout << i - 1 << " " << i + 1 << endl;
return 0;
}
cout << "-1 -1\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
for (int i = 0; i + 1 < s.size(); i++) {
if (s[i] == s[i + 1]) {
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
if (i && s[i - 1] == s[i + 1]) {
cout << i << " " << i + 2 << endl;
return 0;
}
}
cout << "-1 -1";
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
for (int i = 0; i + 1 < s.size(); i++) {
if (s[i] == s[i + 1]) {
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
if (i && s[i - 1] == s[i + 1]) {
cout << i << " " << i + 2 << endl;
return 0;
}
}
cout << "-1 -1";
return 0;
}
``` |
#include "bits/stdc++.h"
using namespace std;
int main() {
string S;
cin >> S;
for (int i = 0; i < S.size() - 1; i++) {
if (S[i] == S[i + 1]) {
cout << i + 1 << " " << i + 2;
goto END;
}
}
for (int i = 0; i < S.size() - 2; i++) {
if (S[i] == S[i + 2]) {
cout << i + 1 << " " << i + 3;
goto END;
}
}
cout << "-1 -1";
END:;
} | ### Prompt
Develop a solution in cpp to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include "bits/stdc++.h"
using namespace std;
int main() {
string S;
cin >> S;
for (int i = 0; i < S.size() - 1; i++) {
if (S[i] == S[i + 1]) {
cout << i + 1 << " " << i + 2;
goto END;
}
}
for (int i = 0; i < S.size() - 2; i++) {
if (S[i] == S[i + 2]) {
cout << i + 1 << " " << i + 3;
goto END;
}
}
cout << "-1 -1";
END:;
}
``` |
#include<cstdio>
#include<cstring>
const int N=1e5+50;
char s[N];
int len,x,y,ans;
int main(){
scanf("%s",s);
len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]==s[i+1]){printf("%d %d\n",i+1,i+2);return 0;}
else if(s[i]==s[i+2]){printf("%d %d\n",i+1,i+3);return 0;}
}
printf("-1 -1\n");
} | ### Prompt
Please formulate a CPP solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<cstdio>
#include<cstring>
const int N=1e5+50;
char s[N];
int len,x,y,ans;
int main(){
scanf("%s",s);
len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]==s[i+1]){printf("%d %d\n",i+1,i+2);return 0;}
else if(s[i]==s[i+2]){printf("%d %d\n",i+1,i+3);return 0;}
}
printf("-1 -1\n");
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int n=s.length();
for(int i=0;i<n-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if(i!=n-3 && s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int n=s.length();
for(int i=0;i<n-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if(i!=n-3 && s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
``` |
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s;
cin>>s;
int l=s.size();
int i=0;
for(i=0;i<l;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}else if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1"<<" "<<"-1"<<endl;
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s;
cin>>s;
int l=s.size();
int i=0;
for(i=0;i<l;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}else if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1"<<" "<<"-1"<<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++){
if(i!=s.size()-2){
if(s[i]==s[i+2]){
cout<<i+1<<' '<<i+3<<endl;
return 0;
}
}
if(s[i]==s[i+1]){
cout<<i+1<<' '<<i+2<<endl;
return 0;
}
}
cout<<-1<<' '<<-1<<endl;
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++){
if(i!=s.size()-2){
if(s[i]==s[i+2]){
cout<<i+1<<' '<<i+3<<endl;
return 0;
}
}
if(s[i]==s[i+1]){
cout<<i+1<<' '<<i+2<<endl;
return 0;
}
}
cout<<-1<<' '<<-1<<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string s;cin>>s;
int len=s.length();
int l=-1,r=-1;
//bool flag=false;
for(int i=0;i<len-1;i++){
if(s[i]==s[i+1]) {l=i+1;r=l+1;}
}
for(int i=0;i<len-2;i++){
if(s[i]==s[i+2]) {l=i+1;r=l+2;}
}
cout<<l<<" "<<r<<endl;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string s;cin>>s;
int len=s.length();
int l=-1,r=-1;
//bool flag=false;
for(int i=0;i<len-1;i++){
if(s[i]==s[i+1]) {l=i+1;r=l+1;}
}
for(int i=0;i<len-2;i++){
if(s[i]==s[i+2]) {l=i+1;r=l+2;}
}
cout<<l<<" "<<r<<endl;
}
``` |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
char s[100005];
int main()
{
scanf("%s",s);
int len=strlen(s);
if(len==2)
{
if(s[0]==s[1]) printf("1 2\n");
else printf("-1 -1\n");
return 0;
}
for(int i=0;i<len-2;i++)
{
if(s[i]==s[i+1]||s[i]==s[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
}
printf("-1 -1\n");
return 0;
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
char s[100005];
int main()
{
scanf("%s",s);
int len=strlen(s);
if(len==2)
{
if(s[0]==s[1]) printf("1 2\n");
else printf("-1 -1\n");
return 0;
}
for(int i=0;i<len-2;i++)
{
if(s[i]==s[i+1]||s[i]==s[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
}
printf("-1 -1\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int N = s.size();
for (int i = 0; i < N-1;i++){
if(s[i]==s[i+1]) {
cout << i +1<< ' '<<i + 2 << endl;
return 0;
}
}
for (int i = 0; i < N - 2;i++){
if(s[i]==s[i+2]){
cout << i+1 << ' '<<i + 3 << endl;
return 0;
}
}
cout << -1 <<' '<< -1 << endl;
} | ### Prompt
Develop a solution in Cpp to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int N = s.size();
for (int i = 0; i < N-1;i++){
if(s[i]==s[i+1]) {
cout << i +1<< ' '<<i + 2 << endl;
return 0;
}
}
for (int i = 0; i < N - 2;i++){
if(s[i]==s[i+2]){
cout << i+1 << ' '<<i + 3 << endl;
return 0;
}
}
cout << -1 <<' '<< -1 << endl;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
}
for(int i=0;i<s.size()-2;i++){
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
} | ### Prompt
In CPP, your task is to solve the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
}
for(int i=0;i<s.size()-2;i++){
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s[100010];
int main()
{
int i,l,ch,left=-1,right=-1;
scanf("%s",s+1);
l=strlen(s+1);
for(i=1;i<l;i++)
if(s[i]==s[i+1]){
left=i;right=i+1;
break;
}
if(left==-1)
for(i=1;i<l-1;i++)
if(s[i]==s[i+2]){
left=i;right=i+2;
break;
}
printf("%d %d\n",left,right);
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[100010];
int main()
{
int i,l,ch,left=-1,right=-1;
scanf("%s",s+1);
l=strlen(s+1);
for(i=1;i<l;i++)
if(s[i]==s[i+1]){
left=i;right=i+1;
break;
}
if(left==-1)
for(i=1;i<l-1;i++)
if(s[i]==s[i+2]){
left=i;right=i+2;
break;
}
printf("%d %d\n",left,right);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s[500003];
int main()
{
scanf("%s",s);
int a;
a=strlen(s);
for(int i=0;i<a-1;i++)
{
if(s[i]==s[i+1]){cout<<i+1<<" "<<i+2<<endl;return 0;}
else if(i+2<a&&s[i]==s[i+2]){cout<<i+1<<" "<<i+3<<endl;return 0;}
//else if(i+3<a&&s[i]==s[i+3]){cout<<i+1<<" "<<i+4<<endl;return 0;}
}
cout<<-1<<" "<<-1<<endl;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[500003];
int main()
{
scanf("%s",s);
int a;
a=strlen(s);
for(int i=0;i<a-1;i++)
{
if(s[i]==s[i+1]){cout<<i+1<<" "<<i+2<<endl;return 0;}
else if(i+2<a&&s[i]==s[i+2]){cout<<i+1<<" "<<i+3<<endl;return 0;}
//else if(i+3<a&&s[i]==s[i+3]){cout<<i+1<<" "<<i+4<<endl;return 0;}
}
cout<<-1<<" "<<-1<<endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
string s;
cin >> s;
bool isE=false;
for(int i = 1;i < s.size();i ++){
if(s[i]==s[i-1]) {cout << i << " " << i+1 << endl;isE=1;break;}
if(i-1&&s[i]==s[i-2]) {cout << i-1 << " " << i+1 << endl;isE=1;break;}
}
if(!isE) cout << -1 << " " << -1 << endl;
return 0;
} | ### Prompt
Your challenge is to write a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
string s;
cin >> s;
bool isE=false;
for(int i = 1;i < s.size();i ++){
if(s[i]==s[i-1]) {cout << i << " " << i+1 << endl;isE=1;break;}
if(i-1&&s[i]==s[i-2]) {cout << i-1 << " " << i+1 << endl;isE=1;break;}
}
if(!isE) cout << -1 << " " << -1 << endl;
return 0;
}
``` |
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
//aa axa
for(char c = 'a'; c <= 'z'; c++){
for(size_t i = 0; i < s.size() - 1; i++){
if(s[i] == s[i + 1]){
cout << i + 1 << ' ' << i + 2 << endl;
return 0;
}else if(i + 2 < s.size() && s[i] == s[i + 2]){
cout << i + 1 << ' ' << i + 3 << endl;
return 0;
}
}
}
cout << -1 << ' ' << -1 << endl;
} | ### Prompt
Please create a solution in Cpp to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
//aa axa
for(char c = 'a'; c <= 'z'; c++){
for(size_t i = 0; i < s.size() - 1; i++){
if(s[i] == s[i + 1]){
cout << i + 1 << ' ' << i + 2 << endl;
return 0;
}else if(i + 2 < s.size() && s[i] == s[i + 2]){
cout << i + 1 << ' ' << i + 3 << endl;
return 0;
}
}
}
cout << -1 << ' ' << -1 << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
int n = s.length();
for (int i=0; i<n-1; i++) {
if (s[i] == s[i+1]) {
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if (i+2 < n && s[i] == s[i+2]) {
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
} | ### Prompt
Please create a solution in CPP to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
int n = s.length();
for (int i=0; i<n-1; i++) {
if (s[i] == s[i+1]) {
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if (i+2 < n && s[i] == s[i+2]) {
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
}
for (int i = 0; i < n - 2; i++) {
if (s[i] == s[i + 2]) {
cout << i + 1 << " " << i + 3 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
} | ### Prompt
Your task is to create a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
}
for (int i = 0; i < n - 2; i++) {
if (s[i] == s[i + 2]) {
cout << i + 1 << " " << i + 3 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int f[27];
int main () {
scanf("%s", s);
int n=strlen(s);
for (int i=0; i<n-1; i++) {
if (s[i]==s[i+1]) {
printf("%d %d\n", i+1, i+2);
return 0;
}
if (s[i]==s[i+2]) {
printf("%d %d\n", i+1, i+3);
return 0;
}
}
printf("-1 -1\n");
return 0;
} | ### Prompt
Develop a solution in CPP to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int f[27];
int main () {
scanf("%s", s);
int n=strlen(s);
for (int i=0; i<n-1; i++) {
if (s[i]==s[i+1]) {
printf("%d %d\n", i+1, i+2);
return 0;
}
if (s[i]==s[i+2]) {
printf("%d %d\n", i+1, i+3);
return 0;
}
}
printf("-1 -1\n");
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
string a;
int main()
{
cin>>a;
for(int i=1;i<a.size();i++)
if(a[i]==a[i-1])
{
cout<<i<<" "<<i+1<<endl;
return 0;
}
for(int i=2;i<a.size();i++)
if(a[i]==a[i-2])
{
cout<<i-1<<" "<<i+1<<endl;
return 0;
}
cout<<-1<<" "<<-1<<endl;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
string a;
int main()
{
cin>>a;
for(int i=1;i<a.size();i++)
if(a[i]==a[i-1])
{
cout<<i<<" "<<i+1<<endl;
return 0;
}
for(int i=2;i<a.size();i++)
if(a[i]==a[i-2])
{
cout<<i-1<<" "<<i+1<<endl;
return 0;
}
cout<<-1<<" "<<-1<<endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
for (int a = 0; a + 1 < S.length(); a++) {
if (S[a] == S[a + 1]) {
printf("%d %d\n", a + 1, a + 2);
return 0;
}
}
for (int a = 0; a + 2 < S.length(); a++) {
if (S[a] == S[a + 2]) {
printf("%d %d\n", a + 1, a + 3);
return 0;
}
}
printf("-1 -1\n");
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
for (int a = 0; a + 1 < S.length(); a++) {
if (S[a] == S[a + 1]) {
printf("%d %d\n", a + 1, a + 2);
return 0;
}
}
for (int a = 0; a + 2 < S.length(); a++) {
if (S[a] == S[a + 2]) {
printf("%d %d\n", a + 1, a + 3);
return 0;
}
}
printf("-1 -1\n");
return 0;
}
``` |
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int a, b, len;
string s;
cin >> s; len = s.length();
a = b = -2;
for(int i=1;i<len;i++) {
if(s[i-1] == s[i]) {
a = i-1; b = i;
break;
}
if(i >= 2 && s[i-2] == s[i]) {
a = i - 2; b = i;
break;
}
}
a++; b++;
cout << a << " " << b << endl;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int a, b, len;
string s;
cin >> s; len = s.length();
a = b = -2;
for(int i=1;i<len;i++) {
if(s[i-1] == s[i]) {
a = i-1; b = i;
break;
}
if(i >= 2 && s[i-2] == s[i]) {
a = i - 2; b = i;
break;
}
}
a++; b++;
cout << a << " " << b << endl;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
char a[100010];
int main(){
cin>>a+1;
int n=strlen(a+1);
for(int i=1;i<n;i++){
if(a[i]==a[i+1]) return cout<<i<<' '<<i+1<<'\n',0;
}
for(int i=1;i+2<=n;i++){
if(a[i]==a[i+2]) return cout<<i<<' '<<i+2<<'\n',0;
}
cout<<-1<<' '<<-1<<'\n';
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
char a[100010];
int main(){
cin>>a+1;
int n=strlen(a+1);
for(int i=1;i<n;i++){
if(a[i]==a[i+1]) return cout<<i<<' '<<i+1<<'\n',0;
}
for(int i=1;i+2<=n;i++){
if(a[i]==a[i+2]) return cout<<i<<' '<<i+2<<'\n',0;
}
cout<<-1<<' '<<-1<<'\n';
}
``` |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
signed main(){
string s;
cin >> s;
for(int i=0;i<s.size()-1;i++){
if(s[i]==s[i+1]){
cout << i+1<<" "<<i+2<<endl;
return 0;
}else if(i!=0&&s[i-1]==s[i+1]){
cout << i<<" "<<i+2<<endl;
return 0;
}
}
cout << "-1 -1\n";
}
| ### Prompt
In cpp, your task is to solve the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
signed main(){
string s;
cin >> s;
for(int i=0;i<s.size()-1;i++){
if(s[i]==s[i+1]){
cout << i+1<<" "<<i+2<<endl;
return 0;
}else if(i!=0&&s[i-1]==s[i+1]){
cout << i<<" "<<i+2<<endl;
return 0;
}
}
cout << "-1 -1\n";
}
``` |
#include<cstdio>
#include<cstring>
#define MAXN 200000
char str[MAXN+5];
int main(){
scanf("%s",str+1);
int len=strlen(str+1);
for(int i=1;i<=len;i++){
if(str[i]==str[i+1]){
printf("%d %d",i,i+1);
return 0;
}
if(str[i]==str[i+2]){
printf("%d %d",i,i+2);
return 0;
}
}
printf("-1 -1");
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<cstdio>
#include<cstring>
#define MAXN 200000
char str[MAXN+5];
int main(){
scanf("%s",str+1);
int len=strlen(str+1);
for(int i=1;i<=len;i++){
if(str[i]==str[i+1]){
printf("%d %d",i,i+1);
return 0;
}
if(str[i]==str[i+2]){
printf("%d %d",i,i+2);
return 0;
}
}
printf("-1 -1");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
for (int i = 0; i < s.length(); i++)
{
if (i + 1 < s.length() && s[i] == s[i+1])
{
cout << i + 1 << ' ' << i + 2 << '\n';
return 0;
}
if (i + 2 < s.length() && s[i] == s[i+2])
{
cout << i + 1 << ' ' << i + 3 << '\n';
return 0;
}
}
cout << "-1 -1\n";
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
for (int i = 0; i < s.length(); i++)
{
if (i + 1 < s.length() && s[i] == s[i+1])
{
cout << i + 1 << ' ' << i + 2 << '\n';
return 0;
}
if (i + 2 < s.length() && s[i] == s[i+2])
{
cout << i + 1 << ' ' << i + 3 << '\n';
return 0;
}
}
cout << "-1 -1\n";
return 0;
}
``` |
#include <iostream>
#include <cstdio>
using namespace std;
int n;
string a;
int main()
{
int i;
cin >> a;
a = ' ' + a;
for (i = 1; i < a.size() - 1; i++) {
if (a[i] == a[i + 1]) {
printf("%d %d", i, i + 1);
return 0;
}
if (i + 2 < a.size() && a[i] == a[i + 2]) {
printf("%d %d", i, i + 2);
return 0;
}
}
printf("-1 -1");
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <iostream>
#include <cstdio>
using namespace std;
int n;
string a;
int main()
{
int i;
cin >> a;
a = ' ' + a;
for (i = 1; i < a.size() - 1; i++) {
if (a[i] == a[i + 1]) {
printf("%d %d", i, i + 1);
return 0;
}
if (i + 2 < a.size() && a[i] == a[i + 2]) {
printf("%d %d", i, i + 2);
return 0;
}
}
printf("-1 -1");
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
char s[maxn];
int main()
{
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++)
{
if(i<n&&s[i]==s[i+1])
{
printf("%d %d\n",i,i+1);
return 0;
}
if(i<n-1&&s[i]==s[i+2])
{
printf("%d %d\n",i,i+2);
return 0;
}
}
printf("-1 -1\n");
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
char s[maxn];
int main()
{
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++)
{
if(i<n&&s[i]==s[i+1])
{
printf("%d %d\n",i,i+1);
return 0;
}
if(i<n-1&&s[i]==s[i+2])
{
printf("%d %d\n",i,i+2);
return 0;
}
}
printf("-1 -1\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define for_(i,a,b) for(int i=(a);i<(b);++i)
int main() {
string S;
cin >> S;
int n = S.size();
for_(x,1,3) for_(i,0,n-x) {
if (S[i] == S[i + x]) {
cout << i+1 << " " << i+x+1 << endl;
exit(0);
}
}
puts("-1 -1");
} | ### Prompt
Construct a CPP code solution to the problem outlined:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define for_(i,a,b) for(int i=(a);i<(b);++i)
int main() {
string S;
cin >> S;
int n = S.size();
for_(x,1,3) for_(i,0,n-x) {
if (S[i] == S[i + x]) {
cout << i+1 << " " << i+x+1 << endl;
exit(0);
}
}
puts("-1 -1");
}
``` |
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s;
cin>>s;
int i=0,j=0;
for(int l=0;l<s.length()-1;l++)
{
if(s[l]==s[l+1])
{
i=l+1;
j=l+2;
}
}
for(int p=0;p<s.length()-2;p++)
{
if(s[p]==s[p+2])
{
i=p+1;
j=p+3;
}
}
if(i==0 && j==0)
{
i=-1;
j=-1;
}
cout<<i<<" "<<j<<endl;
} | ### Prompt
Please create a solution in cpp to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s;
cin>>s;
int i=0,j=0;
for(int l=0;l<s.length()-1;l++)
{
if(s[l]==s[l+1])
{
i=l+1;
j=l+2;
}
}
for(int p=0;p<s.length()-2;p++)
{
if(s[p]==s[p+2])
{
i=p+1;
j=p+3;
}
}
if(i==0 && j==0)
{
i=-1;
j=-1;
}
cout<<i<<" "<<j<<endl;
}
``` |
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int N = s.length();
for (int i = 0; i < N - 1; i++) {
if (s[i] == s[i + 1]) {
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
if (i + 2 < N && s[i] == s[i + 2]) {
cout << i + 1 << " " << i + 3 << endl;
return 0;
}
}
cout << "-1 -1" << endl;
} | ### Prompt
Your challenge is to write a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int N = s.length();
for (int i = 0; i < N - 1; i++) {
if (s[i] == s[i + 1]) {
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
if (i + 2 < N && s[i] == s[i + 2]) {
cout << i + 1 << " " << i + 3 << endl;
return 0;
}
}
cout << "-1 -1" << endl;
}
``` |
#include <cstdio>
char s[100100];
int main() {
scanf("%s", s+1);
for(int i = 1; s[i]; i ++ ) if(s[i] == s[i+1] || s[i] == s[i+2]) return printf("%d %d\n", i, s[i+2]?i+2:i+1), 0;
puts("-1 -1");
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <cstdio>
char s[100100];
int main() {
scanf("%s", s+1);
for(int i = 1; s[i]; i ++ ) if(s[i] == s[i+1] || s[i] == s[i+2]) return printf("%d %d\n", i, s[i+2]?i+2:i+1), 0;
puts("-1 -1");
}
``` |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
signed main() {
string s;
cin >> s;
s += '?';
for (int i = 1; i < s.length()-1; i++) {
if (s[i-1] == s[i]) {
cout << i << ' ' << i+1;
return 0;
}
if (s[i-1] == s[i+1]) {
cout << i << ' ' << i+2;
return 0;
}
}
cout << -1 << ' ' << -1;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
signed main() {
string s;
cin >> s;
s += '?';
for (int i = 1; i < s.length()-1; i++) {
if (s[i-1] == s[i]) {
cout << i << ' ' << i+1;
return 0;
}
if (s[i-1] == s[i+1]) {
cout << i << ' ' << i+2;
return 0;
}
}
cout << -1 << ' ' << -1;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
string s;
cin >> s;
int sz = s.size();
int a = -1, b = -1;
for (int i = 1; i < sz; i++) {
if (s[i] == s[i - 1]) {
a = i, b = i + 1;
} else if (i + 1 < sz && s[i - 1] == s[i + 1]) {
a = i, b = i + 2;
}
}
cout << a << " " << b << endl;
return 0;
} | ### Prompt
Please formulate a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(void) {
string s;
cin >> s;
int sz = s.size();
int a = -1, b = -1;
for (int i = 1; i < sz; i++) {
if (s[i] == s[i - 1]) {
a = i, b = i + 1;
} else if (i + 1 < sz && s[i - 1] == s[i + 1]) {
a = i, b = i + 2;
}
}
cout << a << " " << b << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s[100020];
int main() {
scanf("%s", s);
for (int i = 0; s[i]; i++) {
if (s[i] == s[i + 1]) {
printf("%d %d\n", i + 1, i + 2);
return 0;
}
if (s[i] == s[i + 2]) {
printf("%d %d\n", i + 1, i + 3);
return 0;
}
}
printf("-1 -1\n");
return 0;
} | ### Prompt
Create a solution in CPP for the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[100020];
int main() {
scanf("%s", s);
for (int i = 0; s[i]; i++) {
if (s[i] == s[i + 1]) {
printf("%d %d\n", i + 1, i + 2);
return 0;
}
if (s[i] == s[i + 2]) {
printf("%d %d\n", i + 1, i + 3);
return 0;
}
}
printf("-1 -1\n");
return 0;
}
``` |
#include<iostream>
using namespace std;
int main()
{
string s;
cin>>s;
for(int i=1;i<s.length();i++)
{
if(s[i]==s[i-1])
{
cout<<i<<" "<<i+1<<endl;
return 0;
}
}
for(int i=2;i<=s.length();i++)
{
if(s[i]==s[i-2])
{
cout<<i-1<<" "<<i+1<<endl;
return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
} | ### Prompt
Create a solution in Cpp for the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
using namespace std;
int main()
{
string s;
cin>>s;
for(int i=1;i<s.length();i++)
{
if(s[i]==s[i-1])
{
cout<<i<<" "<<i+1<<endl;
return 0;
}
}
for(int i=2;i<=s.length();i++)
{
if(s[i]==s[i-2])
{
cout<<i-1<<" "<<i+1<<endl;
return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
``` |
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 1000005
char s[MAXN];
int main()
{
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<=len-1;i++)
{
if(s[i]==s[i+1])
{
printf("%d %d\n",i+1,i+2);
return 0;
}
if(s[i]==s[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
}
printf("-1 -1\n");
return 0;
} | ### Prompt
Please formulate a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 1000005
char s[MAXN];
int main()
{
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<=len-1;i++)
{
if(s[i]==s[i+1])
{
printf("%d %d\n",i+1,i+2);
return 0;
}
if(s[i]==s[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
}
printf("-1 -1\n");
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
string s;
cin >> s;
ll n = s.size();
for (ll i = 1; i < n; ++i) {
if (s[i - 1] == s[i]) {
cout << i << " " << i + 1 << endl;
return 0;
}
}
for (ll i = 2; i < n; ++i) {
if (s[i - 2] == s[i]) {
cout << i - 1 << " " << i + 1 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
string s;
cin >> s;
ll n = s.size();
for (ll i = 1; i < n; ++i) {
if (s[i - 1] == s[i]) {
cout << i << " " << i + 1 << endl;
return 0;
}
}
for (ll i = 2; i < n; ++i) {
if (s[i - 2] == s[i]) {
cout << i - 1 << " " << i + 1 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
}
``` |
#include "bits/stdc++.h"
using namespace std;
int main(){
string s;
cin>>s;
for(int i=0;i<s.size()-2;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
if(s[s.size()-2]==s[s.size()-1]){
cout<<s.size()-1<<" "<<s.size()<<endl;
return 0;
}
cout<<-1<<" "<<-1<<endl;
return 0;
} | ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include "bits/stdc++.h"
using namespace std;
int main(){
string s;
cin>>s;
for(int i=0;i<s.size()-2;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
if(s[s.size()-2]==s[s.size()-1]){
cout<<s.size()-1<<" "<<s.size()<<endl;
return 0;
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(void){
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
}
for(int i=0;i<s.size()-2;i++){
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(void){
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++){
if(s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
}
for(int i=0;i<s.size()-2;i++){
if(s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
}
``` |
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
if(s.size()==2&&s[0]==s[1]){
cout << "1 2";
return 0;
}
for(int i = 0;i < s.size() - 2;i++){
int j = i;
if(s[j]==s[j+1]||s[j]==s[j+2]||s[j+1]==s[j+2]){
cout << j+1 << " " << j+3;
return 0;
}
}
cout << "-1 -1";
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
if(s.size()==2&&s[0]==s[1]){
cout << "1 2";
return 0;
}
for(int i = 0;i < s.size() - 2;i++){
int j = i;
if(s[j]==s[j+1]||s[j]==s[j+2]||s[j+1]==s[j+2]){
cout << j+1 << " " << j+3;
return 0;
}
}
cout << "-1 -1";
}
``` |
#include<bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
for(int i = 1; i<s.size(); i++)
if(s[i] == s[i - 1]) {
cout << i << " " << i + 1 << endl;
return 0;
}
for(int i = 2; i < s.size(); i++)
if(s[i] == s[i - 2]) {
cout << i - 1 << " " << i + 1 << endl;
return 0;
}
cout << "-1 -1\n" <<endl;
return 0;
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
for(int i = 1; i<s.size(); i++)
if(s[i] == s[i - 1]) {
cout << i << " " << i + 1 << endl;
return 0;
}
for(int i = 2; i < s.size(); i++)
if(s[i] == s[i - 2]) {
cout << i - 1 << " " << i + 1 << endl;
return 0;
}
cout << "-1 -1\n" <<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
int n = s.length();
for(int i = 0; i + 2 < n; i++) {
if(s[i] == s[i+1] || s[i] == s[i+2]) return cout << i + 1 << " " << i + 3 << endl, 0;
}
if(n >= 2 && (s[n-1] == s[n-2])) return cout << n - 1 << " " << n << endl, 0;
cout << "-1 -1" << endl;
} | ### Prompt
Please formulate a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
int n = s.length();
for(int i = 0; i + 2 < n; i++) {
if(s[i] == s[i+1] || s[i] == s[i+2]) return cout << i + 1 << " " << i + 3 << endl, 0;
}
if(n >= 2 && (s[n-1] == s[n-2])) return cout << n - 1 << " " << n << endl, 0;
cout << "-1 -1" << endl;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int n=s.size();
for(int i=0;i<n-1;i++){
if(s[i]==s[i+1]){
cout << i+1 << " " << i+2 << endl;
return 0;
}
}
for(int i=0;i<n-2;i++){
if(s[i]==s[i+2]){
cout << i+1 << " " << i+3 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
return 0;
} | ### Prompt
Create a solution in CPP for the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int n=s.size();
for(int i=0;i<n-1;i++){
if(s[i]==s[i+1]){
cout << i+1 << " " << i+2 << endl;
return 0;
}
}
for(int i=0;i<n-2;i++){
if(s[i]==s[i+2]){
cout << i+1 << " " << i+3 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
return 0;
}
``` |
#include<cstdio>
#include<iostream>
using namespace std;
string s;
int main()
{
cin>>s;
for(int i=1;i<s.size();i++)
if(s[i-1]==s[i])
{
printf("%d %d",i,i+1);
return 0;
}
for(int i=1;i<s.size()-1;i++)
if(s[i-1]==s[i+1])
{
printf("%d %d",i,i+2);
return 0;
}
printf("-1 -1");
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<cstdio>
#include<iostream>
using namespace std;
string s;
int main()
{
cin>>s;
for(int i=1;i<s.size();i++)
if(s[i-1]==s[i])
{
printf("%d %d",i,i+1);
return 0;
}
for(int i=1;i<s.size()-1;i++)
if(s[i-1]==s[i+1])
{
printf("%d %d",i,i+2);
return 0;
}
printf("-1 -1");
}
``` |
#include<bits/stdc++.h>
using namespace std;
char s[100100];
int main()
{
scanf("%s",s);
if(strlen(s)==2)
{
if(s[0]==s[1])
{
printf("1 2\n");
return 0;
}
}
else
for(int i=2;s[i];i++)
{
if(s[i]==s[i-1]||s[i]==s[i-2]||s[i-1]==s[i-2])
{
printf("%d %d\n",i-2+1,i+1);
return 0;
}
}
printf("-1 -1\n");
} | ### Prompt
Please provide a CPP coded solution to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
char s[100100];
int main()
{
scanf("%s",s);
if(strlen(s)==2)
{
if(s[0]==s[1])
{
printf("1 2\n");
return 0;
}
}
else
for(int i=2;s[i];i++)
{
if(s[i]==s[i-1]||s[i]==s[i-2]||s[i-1]==s[i-2])
{
printf("%d %d\n",i-2+1,i+1);
return 0;
}
}
printf("-1 -1\n");
}
``` |
#include<stdio.h>
int main(){
char s[111111];
int i,a=-1,b=-1;
for(i=0;i<111111;i++)
s[i]='\0';
scanf("%s",s);
if(s[0]==s[1]){
a=1;
b=2;
}
for(i=2;s[i]!='\0';i++){
if(s[i]==s[i-1]){
a=i;
b=i+1;
}
if(s[i]==s[i-2]){
a=i-1;
b=i+1;
}
}
printf("%d %d\n",a,b);
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<stdio.h>
int main(){
char s[111111];
int i,a=-1,b=-1;
for(i=0;i<111111;i++)
s[i]='\0';
scanf("%s",s);
if(s[0]==s[1]){
a=1;
b=2;
}
for(i=2;s[i]!='\0';i++){
if(s[i]==s[i-1]){
a=i;
b=i+1;
}
if(s[i]==s[i-2]){
a=i-1;
b=i+1;
}
}
printf("%d %d\n",a,b);
return 0;
}
``` |
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char a[100000+5];
int alen;
int main()
{
scanf("%s",a);
alen=strlen(a);
for(int i=0;i<alen-1;i++)
if(a[i]==a[i+1])
{
printf("%d %d\n",i+1,i+2);
return 0;
}
for(int i=0;i<alen-2;i++)
if(a[i]==a[i+1]||a[i]==a[i+2]||a[i+1]==a[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
printf("-1 -1\n");
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char a[100000+5];
int alen;
int main()
{
scanf("%s",a);
alen=strlen(a);
for(int i=0;i<alen-1;i++)
if(a[i]==a[i+1])
{
printf("%d %d\n",i+1,i+2);
return 0;
}
for(int i=0;i<alen-2;i++)
if(a[i]==a[i+1]||a[i]==a[i+2]||a[i+1]==a[i+2])
{
printf("%d %d\n",i+1,i+3);
return 0;
}
printf("-1 -1\n");
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
char s[MAXN];
int main(){
scanf("%s",s);
int len=strlen(s);
int a=-1,b=-1;
for(int i=1;i<len;i++){
if(s[i]==s[i-1]){
a=i-1,b=i;
break;
}
}
if(a==-1){
for(int i=2;i<len;i++){
if(s[i]==s[i-2]){
a=i-2,b=i;
break;
}
}
}
if(a==-1){
printf("-1 -1\n");
}
else{
printf("%d %d\n",a+1,b+1);
}
return 0;
} | ### Prompt
In Cpp, your task is to solve the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
char s[MAXN];
int main(){
scanf("%s",s);
int len=strlen(s);
int a=-1,b=-1;
for(int i=1;i<len;i++){
if(s[i]==s[i-1]){
a=i-1,b=i;
break;
}
}
if(a==-1){
for(int i=2;i<len;i++){
if(s[i]==s[i-2]){
a=i-2,b=i;
break;
}
}
}
if(a==-1){
printf("-1 -1\n");
}
else{
printf("%d %d\n",a+1,b+1);
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;cin>>s;
for(int i=0;i<26;i++){
char a='a'+i;
int c=-10;
for(int j=0;j<s.size();j++){
if(s[j]==a){
if(j-c<=2){
cout<<c+1<<" "<<j+1<<endl;
return 0;
}
else c=j;
}
}
}
cout<<-1<<" "<<-1<<endl;
} | ### Prompt
Your task is to create a cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;cin>>s;
for(int i=0;i<26;i++){
char a='a'+i;
int c=-10;
for(int j=0;j<s.size();j++){
if(s[j]==a){
if(j-c<=2){
cout<<c+1<<" "<<j+1<<endl;
return 0;
}
else c=j;
}
}
}
cout<<-1<<" "<<-1<<endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
int n = s.length();
for (int i=0; i<n-1; i++) {
if (s[i] == s[i+1]) {
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if (i == n-2) {
break;
}
if (s[i] == s[i+2]) {
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
} | ### Prompt
Create a solution in Cpp for the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
int n = s.length();
for (int i=0; i<n-1; i++) {
if (s[i] == s[i+1]) {
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if (i == n-2) {
break;
}
if (s[i] == s[i+2]) {
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++)
{
if(s[i]==s[i+1])
{
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if(i+2<s.size())
{
if(s[i]==s[i+2])
{
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
}
cout<<-1<<" "<<-1<<endl;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
for(int i=0;i<s.size()-1;i++)
{
if(s[i]==s[i+1])
{
cout<<i+1<<" "<<i+2<<endl;
return 0;
}
if(i+2<s.size())
{
if(s[i]==s[i+2])
{
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
}
cout<<-1<<" "<<-1<<endl;
}
``` |
#include<iostream>
using namespace std;
bool vis[50];
int main()
{
string s;
cin>>s;
for(int i=0;i<s.length();++i){
if(i+1<s.length()&&s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;return 0;
}
if(i+2<s.length()&&s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<iostream>
using namespace std;
bool vis[50];
int main()
{
string s;
cin>>s;
for(int i=0;i<s.length();++i){
if(i+1<s.length()&&s[i]==s[i+1]){
cout<<i+1<<" "<<i+2<<endl;return 0;
}
if(i+2<s.length()&&s[i]==s[i+2]){
cout<<i+1<<" "<<i+3<<endl;return 0;
}
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char s[N];
int main()
{
scanf("%s",s+1);
int len=strlen(s+1);
if(len==2&&s[1]==s[2]){
printf("1 2\n");
return 0;
}
int l=-1,r=-1;
for(int i=1;i<len-1;i++){
if(s[i]==s[i+1]||s[i]==s[i+2]||s[i+1]==s[i+2]){
l=i,r=i+2;
}
}
printf("%d %d\n",l,r);
} | ### Prompt
Please create a solution in cpp to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char s[N];
int main()
{
scanf("%s",s+1);
int len=strlen(s+1);
if(len==2&&s[1]==s[2]){
printf("1 2\n");
return 0;
}
int l=-1,r=-1;
for(int i=1;i<len-1;i++){
if(s[i]==s[i+1]||s[i]==s[i+2]||s[i+1]==s[i+2]){
l=i,r=i+2;
}
}
printf("%d %d\n",l,r);
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
string s;
cin>>s;
n=s.size();
for(int i=1;i<n;++i){
if(i>1&&s[i-2]==s[i])cout<<i-1<<" "<<i+1,exit(0);
else if(s[i-1]==s[i])cout<<i<<" "<<i+1,exit(0);
}
cout<<"-1 -1";
} | ### Prompt
In cpp, your task is to solve the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
string s;
cin>>s;
n=s.size();
for(int i=1;i<n;++i){
if(i>1&&s[i-2]==s[i])cout<<i-1<<" "<<i+1,exit(0);
else if(s[i-1]==s[i])cout<<i<<" "<<i+1,exit(0);
}
cout<<"-1 -1";
}
``` |
#include <iostream>
using namespace std;
int main(){
string s;cin>>s;
if(s.size()==2&&s[0]==s[1]){
cout << 1 << " " << 2 << endl;
return 0;
}
for(int i = 0; s.size() > i+2; i++){
if(s[i]==s[i+1] || s[i]==s[i+2] || s[i+1]==s[i+2]){
cout << i+1 << " " << i+3 << endl;
return 0;
}
}
cout << "-1 -1" << endl;
} | ### Prompt
Please formulate a Cpp solution to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <iostream>
using namespace std;
int main(){
string s;cin>>s;
if(s.size()==2&&s[0]==s[1]){
cout << 1 << " " << 2 << endl;
return 0;
}
for(int i = 0; s.size() > i+2; i++){
if(s[i]==s[i+1] || s[i]==s[i+2] || s[i+1]==s[i+2]){
cout << i+1 << " " << i+3 << endl;
return 0;
}
}
cout << "-1 -1" << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(){
string a;
cin >> a;
int n = a.size();
for(int i = 0;i < n-1;i++){
if(a[i] == a[i+1]){
i++;
cout << i << " " << i+1 << endl;
return 0;
}
if(i != n-2 && a[i] == a[i+2]){
i++;
cout << i << " " << i+2 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
return 0;
} | ### Prompt
Please create a solution in Cpp to the following problem:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
string a;
cin >> a;
int n = a.size();
for(int i = 0;i < n-1;i++){
if(a[i] == a[i+1]){
i++;
cout << i << " " << i+1 << endl;
return 0;
}
if(i != n-2 && a[i] == a[i+2]){
i++;
cout << i << " " << i+2 << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(){
string s; cin>>s;
for(int i=0; i<s.size()-2; i++){
if(s[i] == s[i+1] || s[i+2] == s[i+1] || s[i] == s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
if(s.size() == 2 && s[0] == s[1]){
cout<<1<<" "<<2<<endl;
return 0;
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both `voodoo` and `melee` are unbalanced, while neither `noon` nor `a` is.
You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
* 2 ≦ |s| ≦ 10^5
* s consists of lowercase letters.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print `-1 -1`.
If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print `a b`. If there exists more than one such substring, any of them will be accepted.
Examples
Input
needed
Output
2 5
Input
atcoder
Output
-1 -1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
string s; cin>>s;
for(int i=0; i<s.size()-2; i++){
if(s[i] == s[i+1] || s[i+2] == s[i+1] || s[i] == s[i+2]){
cout<<i+1<<" "<<i+3<<endl;
return 0;
}
}
if(s.size() == 2 && s[0] == s[1]){
cout<<1<<" "<<2<<endl;
return 0;
}
cout<<-1<<" "<<-1<<endl;
return 0;
}
``` |
#include <string>
#include <iostream>
using namespace std;
/*
<expr> ::= <term> [ ('+'|'-') <term> ]*
<term> ::= <factor> [ ('*'|'/') <factor> ]*
<factor> ::= <number> | '(' <expr> ')'
<number> :== 1?????\????????°???
*/
int expr(string& s, int& i);
int term(string& s, int& i);
int factor(string& s, int& i);
int number(string& s, int& i);
int expr(string& s, int& i) {
int val = term(s, i);
while (s[i] == '+' || s[i] == '-') {
char op = s[i];
i++;
int val2 = term(s, i);
if (op == '+')
val += val2;
else
val -= val2;
}
return val;
}
int term(string& s, int& i) {
int val = factor(s, i);
while (s[i] == '*' || s[i] == '/') {
char op = s[i];
i++;
int val2 = factor(s, i);
if (op == '*')
val *= val2;
else
val /= val2;
}
return val;
}
int factor(string& s, int& i) {
int val;
if (s[i] != '(') {
val = number(s, i);
} else {
i++;
val = expr(s, i);
i++;
}
return val;
}
int number(string& s, int& i) {
int val = 0;
while(isdigit(s[i])) {
val = val*10 + (s[i] - '0');
i++;
}
return val;
}
int main() {
int n;
cin >> n;
for (int j=0; j<n; j++) {
int i = 0;
string str;
cin >> str;
cout << expr(str, i) << endl;
}
} | ### Prompt
Generate a Cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <string>
#include <iostream>
using namespace std;
/*
<expr> ::= <term> [ ('+'|'-') <term> ]*
<term> ::= <factor> [ ('*'|'/') <factor> ]*
<factor> ::= <number> | '(' <expr> ')'
<number> :== 1?????\????????°???
*/
int expr(string& s, int& i);
int term(string& s, int& i);
int factor(string& s, int& i);
int number(string& s, int& i);
int expr(string& s, int& i) {
int val = term(s, i);
while (s[i] == '+' || s[i] == '-') {
char op = s[i];
i++;
int val2 = term(s, i);
if (op == '+')
val += val2;
else
val -= val2;
}
return val;
}
int term(string& s, int& i) {
int val = factor(s, i);
while (s[i] == '*' || s[i] == '/') {
char op = s[i];
i++;
int val2 = factor(s, i);
if (op == '*')
val *= val2;
else
val /= val2;
}
return val;
}
int factor(string& s, int& i) {
int val;
if (s[i] != '(') {
val = number(s, i);
} else {
i++;
val = expr(s, i);
i++;
}
return val;
}
int number(string& s, int& i) {
int val = 0;
while(isdigit(s[i])) {
val = val*10 + (s[i] - '0');
i++;
}
return val;
}
int main() {
int n;
cin >> n;
for (int j=0; j<n; j++) {
int i = 0;
string str;
cin >> str;
cout << expr(str, i) << endl;
}
}
``` |
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int num(string&, int&);
int fact(string&, int&);
int mul(string&, int&);
int expr(string&, int&);
int num(string &s, int &k) {
int ret = 0;
while ('0' <= s[k] && s[k] <= '9') {
ret *= 10;
ret += s[k] - '0';
k ++;
}
return ret;
}
int fact(string &s, int &k) {
int ret;
if (s[k] == '(') {
ret = expr(s, ++k);
k ++;
} else if (s[k] == '-') {
ret = -num(s, ++k);
} else if (s[k] == '+') {
ret = num(s, ++k);
} else {
ret = num(s, k);
}
return ret;
}
int mul(string &s, int &k) {
int ret = fact(s, k);
while (1) {
if (s[k] == '*') {
ret *= fact(s, ++k);
} else if (s[k] == '/') {
ret /= fact(s, ++k);
} else {
break;
}
}
return ret;
}
int expr(string &s, int &k) {
int ret = mul(s, k);
while (1) {
if (s[k] == '+') {
ret += mul(s, ++k);
} else if (s[k] == '-') {
ret -= mul(s, ++k);
} else {
break;
}
}
return ret;
}
int main() {
int n;
cin >> n;
string s;
while (n--) {
cin >> s;
int pos = 0;
cout << expr(s, pos) << endl;
}
return 0;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int num(string&, int&);
int fact(string&, int&);
int mul(string&, int&);
int expr(string&, int&);
int num(string &s, int &k) {
int ret = 0;
while ('0' <= s[k] && s[k] <= '9') {
ret *= 10;
ret += s[k] - '0';
k ++;
}
return ret;
}
int fact(string &s, int &k) {
int ret;
if (s[k] == '(') {
ret = expr(s, ++k);
k ++;
} else if (s[k] == '-') {
ret = -num(s, ++k);
} else if (s[k] == '+') {
ret = num(s, ++k);
} else {
ret = num(s, k);
}
return ret;
}
int mul(string &s, int &k) {
int ret = fact(s, k);
while (1) {
if (s[k] == '*') {
ret *= fact(s, ++k);
} else if (s[k] == '/') {
ret /= fact(s, ++k);
} else {
break;
}
}
return ret;
}
int expr(string &s, int &k) {
int ret = mul(s, k);
while (1) {
if (s[k] == '+') {
ret += mul(s, ++k);
} else if (s[k] == '-') {
ret -= mul(s, ++k);
} else {
break;
}
}
return ret;
}
int main() {
int n;
cin >> n;
string s;
while (n--) {
cin >> s;
int pos = 0;
cout << expr(s, pos) << endl;
}
return 0;
}
``` |
#include <iostream>
#include <sstream>
using namespace std;
int pos;
string str;
int str_to_int(string str) {
if (str == "") return 0;
int ret = 0;
stringstream ss;
ss << str;
ss >> ret;
return ret;
}
int keisan();
int kou();
int number();
int keisan() {
int left = kou();
while (str[pos] != '=' && str[pos] != ')') {
char c = str[pos++];
int right = kou();
if (c == '+') left += right;
else left -= right;
}
++pos;
return left;
}
int kou() {
int left = number();
while (str[pos] == '*' || str[pos] == '/') {
char c = str[pos++];
int right = number();
if (c == '*') left *= right;
else left /= right;
}
return left;
}
int number() {
string ret = "";
if (str[pos] == '(') {
++pos;
return keisan();
}
while (str[pos] >= '0' && str[pos] <= '9')
ret += str[pos++];
return str_to_int(ret);
}
int main() {
int n; cin >> n;
for (int i = 0; i < n; ++i) {
cin >> str;
pos = 0;
cout << keisan() << endl;
}
} | ### Prompt
Your challenge is to write a cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <sstream>
using namespace std;
int pos;
string str;
int str_to_int(string str) {
if (str == "") return 0;
int ret = 0;
stringstream ss;
ss << str;
ss >> ret;
return ret;
}
int keisan();
int kou();
int number();
int keisan() {
int left = kou();
while (str[pos] != '=' && str[pos] != ')') {
char c = str[pos++];
int right = kou();
if (c == '+') left += right;
else left -= right;
}
++pos;
return left;
}
int kou() {
int left = number();
while (str[pos] == '*' || str[pos] == '/') {
char c = str[pos++];
int right = number();
if (c == '*') left *= right;
else left /= right;
}
return left;
}
int number() {
string ret = "";
if (str[pos] == '(') {
++pos;
return keisan();
}
while (str[pos] >= '0' && str[pos] <= '9')
ret += str[pos++];
return str_to_int(ret);
}
int main() {
int n; cin >> n;
for (int i = 0; i < n; ++i) {
cin >> str;
pos = 0;
cout << keisan() << endl;
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define int long long
int expression(string,int&);
int term(string,int&);
int factor(string,int&);
int number(string,int&);
bool f;
int expression(string s,int& p){
int res=term(s,p);
while(p<(int)s.size()){
if(s[p]=='+'){
p++;
res+=term(s,p);
continue;
}
if(s[p]=='-'){
p++;
res-=term(s,p);
continue;
}
break;
}
return res;
}
int term(string s,int& p){
int res=factor(s,p);
while(p<(int)s.size()){
if(s[p]=='*'){
p++;
res*=factor(s,p);
continue;
}
if(s[p]=='/'){
p++;
int tmp=factor(s,p);
if(tmp==0){
f=1;
break;
}
res/=tmp;
continue;
}
break;
}
return res;
}
int factor(string s,int& p){
int res;
if(s[p]=='('){
p++;
res=expression(s,p);
p++;
}else{
res=number(s,p);
}
return res;
}
int number(string s,int& p){
int res=0;
while(p<(int)s.size()&&isdigit(s[p]))
res=res*10+s[p++]-'0';
return res;
}
signed main(){
int n;
cin>>n;
while(n--){
string s;
int p=0;
cin>>s;s.pop_back();
cout<<expression(s,p)<<endl;
}
return 0;
}
/*
verified on 2017/04/26
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?lang=jp&id=0109
*/ | ### Prompt
Construct a cpp code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
int expression(string,int&);
int term(string,int&);
int factor(string,int&);
int number(string,int&);
bool f;
int expression(string s,int& p){
int res=term(s,p);
while(p<(int)s.size()){
if(s[p]=='+'){
p++;
res+=term(s,p);
continue;
}
if(s[p]=='-'){
p++;
res-=term(s,p);
continue;
}
break;
}
return res;
}
int term(string s,int& p){
int res=factor(s,p);
while(p<(int)s.size()){
if(s[p]=='*'){
p++;
res*=factor(s,p);
continue;
}
if(s[p]=='/'){
p++;
int tmp=factor(s,p);
if(tmp==0){
f=1;
break;
}
res/=tmp;
continue;
}
break;
}
return res;
}
int factor(string s,int& p){
int res;
if(s[p]=='('){
p++;
res=expression(s,p);
p++;
}else{
res=number(s,p);
}
return res;
}
int number(string s,int& p){
int res=0;
while(p<(int)s.size()&&isdigit(s[p]))
res=res*10+s[p++]-'0';
return res;
}
signed main(){
int n;
cin>>n;
while(n--){
string s;
int p=0;
cin>>s;s.pop_back();
cout<<expression(s,p)<<endl;
}
return 0;
}
/*
verified on 2017/04/26
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?lang=jp&id=0109
*/
``` |
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
typedef string::const_iterator State;
class ParseError{};
int number(State&);
int term(State&);
int expression(State&);
int factor(State&);
int number(State& begin){
int ret = 0;
while(isdigit(*begin)){
ret*=10;
ret+=*begin-'0';
++begin;
}
return ret;
}
int term(State& begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
++begin;
ret*=factor(begin);
}
else if(*begin == '/'){
++begin;
ret/=factor(begin);
}
else{
break;
}
}
return ret;
}
int factor(State& begin){
int ret;
if(*begin == '('){
++begin;
ret = expression(begin);
++begin;
}
else ret = number(begin);
return ret;
}
int expression(State& begin){
int ret = term(begin);
for(;;){
if(*begin == '+'){
++begin;
ret+=term(begin);
}
else if(*begin == '-'){
++begin;
ret-=term(begin);
}
else{
break;
}
}
return ret;
}
int main(){
int n;
cin >> n;
cin.ignore();
while(n--){
string s;
getline(cin,s);
State b = s.begin();
int ans = expression(b);
cout << ans << endl;
}
return 0;
} | ### Prompt
Create a solution in CPP for the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
typedef string::const_iterator State;
class ParseError{};
int number(State&);
int term(State&);
int expression(State&);
int factor(State&);
int number(State& begin){
int ret = 0;
while(isdigit(*begin)){
ret*=10;
ret+=*begin-'0';
++begin;
}
return ret;
}
int term(State& begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
++begin;
ret*=factor(begin);
}
else if(*begin == '/'){
++begin;
ret/=factor(begin);
}
else{
break;
}
}
return ret;
}
int factor(State& begin){
int ret;
if(*begin == '('){
++begin;
ret = expression(begin);
++begin;
}
else ret = number(begin);
return ret;
}
int expression(State& begin){
int ret = term(begin);
for(;;){
if(*begin == '+'){
++begin;
ret+=term(begin);
}
else if(*begin == '-'){
++begin;
ret-=term(begin);
}
else{
break;
}
}
return ret;
}
int main(){
int n;
cin >> n;
cin.ignore();
while(n--){
string s;
getline(cin,s);
State b = s.begin();
int ans = expression(b);
cout << ans << endl;
}
return 0;
}
``` |
#include <iostream>
#include <vector>
#include <sstream>
#include <cctype>
using namespace std;
typedef string::const_iterator State;
int factor(State &begin);
int number(State &begin) {
int ret = 0;
while(isdigit(*begin)) {
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
int term(State &begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
begin++;
ret *= factor(begin);
} else if (*begin == '/') {
begin++;
ret /= factor(begin);
} else {
break;
}
}
return ret;
}
int expression (State &begin) {
int ret = term(begin);
for (;;) {
if(*begin == '+') {
begin++;
ret += term(begin);
} else if (*begin == '-') {
begin++;
ret -= term(begin);
} else {
break;
}
}
return ret;
}
int factor (State &begin) {
if(*begin == '(') {
begin++;
int ret = expression(begin);
begin++;
return ret;
} else {
return number(begin);
}
}
int main(){
int n;
cin >> n;
for(;n;n--){
string s;
cin >> s;
State begin = s.begin();
int ans = expression(begin);
cout << ans << endl;
}
return 0;
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <vector>
#include <sstream>
#include <cctype>
using namespace std;
typedef string::const_iterator State;
int factor(State &begin);
int number(State &begin) {
int ret = 0;
while(isdigit(*begin)) {
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
int term(State &begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
begin++;
ret *= factor(begin);
} else if (*begin == '/') {
begin++;
ret /= factor(begin);
} else {
break;
}
}
return ret;
}
int expression (State &begin) {
int ret = term(begin);
for (;;) {
if(*begin == '+') {
begin++;
ret += term(begin);
} else if (*begin == '-') {
begin++;
ret -= term(begin);
} else {
break;
}
}
return ret;
}
int factor (State &begin) {
if(*begin == '(') {
begin++;
int ret = expression(begin);
begin++;
return ret;
} else {
return number(begin);
}
}
int main(){
int n;
cin >> n;
for(;n;n--){
string s;
cin >> s;
State begin = s.begin();
int ans = expression(begin);
cout << ans << endl;
}
return 0;
}
``` |
#include <cstdio>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
typedef string::iterator State;
bool isdigit(State& pos);
int number(State& pos);
int factor(State& pos);
int expression(State& pos);
int term(State& pos);
bool isdigit(State& pos)
{
if('0'<=*pos&&*pos<='9')return true;
else return false;
}
int number(State& pos)
{
int res = 0;
while(isdigit(pos))
{
res*=10;
res += (*pos)-'0';
pos++;
}
return res;
}
int term(State& pos)
{
int ret = factor(pos);
while(*pos == '*'||*pos == '/')
{
bool mul = (*pos == '*');
pos++;
int k = factor(pos);
if(mul)ret *= k;
else ret/=k;
}
return ret;
}
int factor(State& pos)
{
int ret;
if(*pos=='(')
{
pos++;
ret = expression(pos);
pos++;
}
else ret = number(pos);
return ret;
}
int expression(State& pos)
{
int ret = term(pos);
while(*pos == '+'||*pos == '-')
{
bool plus = (*pos == '+');
pos++;
int k = term(pos);
if(plus)ret += k;
else ret -= k;
}
return ret;
}
int main()
{
int n;
cin >> n;
for(int i=0;i<n;i++)
{
string s;
cin >> s;
State pos = s.begin();
cout << expression(pos) << endl;
}
return 0;
} | ### Prompt
Create a solution in Cpp for the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <cstdio>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
typedef string::iterator State;
bool isdigit(State& pos);
int number(State& pos);
int factor(State& pos);
int expression(State& pos);
int term(State& pos);
bool isdigit(State& pos)
{
if('0'<=*pos&&*pos<='9')return true;
else return false;
}
int number(State& pos)
{
int res = 0;
while(isdigit(pos))
{
res*=10;
res += (*pos)-'0';
pos++;
}
return res;
}
int term(State& pos)
{
int ret = factor(pos);
while(*pos == '*'||*pos == '/')
{
bool mul = (*pos == '*');
pos++;
int k = factor(pos);
if(mul)ret *= k;
else ret/=k;
}
return ret;
}
int factor(State& pos)
{
int ret;
if(*pos=='(')
{
pos++;
ret = expression(pos);
pos++;
}
else ret = number(pos);
return ret;
}
int expression(State& pos)
{
int ret = term(pos);
while(*pos == '+'||*pos == '-')
{
bool plus = (*pos == '+');
pos++;
int k = term(pos);
if(plus)ret += k;
else ret -= k;
}
return ret;
}
int main()
{
int n;
cin >> n;
for(int i=0;i<n;i++)
{
string s;
cin >> s;
State pos = s.begin();
cout << expression(pos) << endl;
}
return 0;
}
``` |
#include<iostream>
#include<string>
#include<map>
using namespace std;
#define ans pair<int, int>
ans equation(string s, int p = 0);
ans factor(string s, int p = 0);
ans term(string s, int p = 0);
int main(){
int n;
cin >>n;
while(n--){
string s;
cin >>s;
ans r = equation(s);
cout <<r.first<<endl;
}
return 0;
}
ans equation(string s, int p){
ans r = factor(s,p);
while(s[r.second] == '+' || s[r.second] == '-'){
ans r_dash = factor(s,r.second+1);
if(s[r.second] == '+'){r.first += r_dash.first;}
if(s[r.second] == '-'){r.first -= r_dash.first;}
r.second = r_dash.second;
}
return r;
}
ans factor(string s, int p){
ans r = term(s,p);
while(s[r.second] == '*' || s[r.second] == '/'){
ans r_dash = term(s,r.second+1);
if(s[r.second] == '*'){r.first *= r_dash.first;}
if(s[r.second] == '/'){r.first /= r_dash.first;}
r.second = r_dash.second;
}
return r;
}
ans term(string s, int p){
if(s[p] == '('){
ans r = equation(s,p+1);
r.second++;
return r;
}
else{
int v = 0;
while(isdigit(s[p])){
v = v*10+s[p++]-'0';
}
return ans(v,p);
}
} | ### Prompt
Develop a solution in CPP to the problem described below:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<iostream>
#include<string>
#include<map>
using namespace std;
#define ans pair<int, int>
ans equation(string s, int p = 0);
ans factor(string s, int p = 0);
ans term(string s, int p = 0);
int main(){
int n;
cin >>n;
while(n--){
string s;
cin >>s;
ans r = equation(s);
cout <<r.first<<endl;
}
return 0;
}
ans equation(string s, int p){
ans r = factor(s,p);
while(s[r.second] == '+' || s[r.second] == '-'){
ans r_dash = factor(s,r.second+1);
if(s[r.second] == '+'){r.first += r_dash.first;}
if(s[r.second] == '-'){r.first -= r_dash.first;}
r.second = r_dash.second;
}
return r;
}
ans factor(string s, int p){
ans r = term(s,p);
while(s[r.second] == '*' || s[r.second] == '/'){
ans r_dash = term(s,r.second+1);
if(s[r.second] == '*'){r.first *= r_dash.first;}
if(s[r.second] == '/'){r.first /= r_dash.first;}
r.second = r_dash.second;
}
return r;
}
ans term(string s, int p){
if(s[p] == '('){
ans r = equation(s,p+1);
r.second++;
return r;
}
else{
int v = 0;
while(isdigit(s[p])){
v = v*10+s[p++]-'0';
}
return ans(v,p);
}
}
``` |
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void select_cal(char c, int *tmp, int val){
if(c == '+') *tmp += val;
else if(c == '-') *tmp -= val;
else if(c == '*') *tmp = floor(*tmp * val);
else if(c == '/' && val != 0) *tmp = floor(*tmp / val);
}
int calculation(string str, int len, int begin){
int ans, tmp, cnt, val;
char c;
ans = 0;
tmp = 0;
c = '+';
for(int i=begin; i<len; i++){
if(str[i] == '+' || str[i] == '-'){
ans += tmp;
tmp = 0;
c = str[i];
}
else if(str[i] == '*' || str[i] == '/'){
c = str[i];
}
else if(str[i] == '('){
select_cal(c, &tmp, calculation(str, len, i+1));
cnt = 1;
do{
i++;
if(str[i] == '(') cnt++;
else if(str[i] == ')') cnt--;
}while(cnt != 0);
}
else if(str[i] == ')' || str[i] == '='){
break;
}
else {
cnt = 1;
while(str[i+cnt] >= '0' && str[i+cnt] <= '9'){
cnt++;
}
val = 0;
for(int j=0; j<cnt; j++){
val += (str[i+cnt-j-1] - '0') * pow(10, j);
}
select_cal(c, &tmp, val);
i += cnt - 1;
}
}
ans += tmp;
return ans;
}
int main(){
int n;
string str;
cin >> n;
for(int i=0; i<n; i++){
cin >> str;
cout << calculation(str, str.length(), 0) << endl;
}
return 0;
} | ### Prompt
In cpp, your task is to solve the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void select_cal(char c, int *tmp, int val){
if(c == '+') *tmp += val;
else if(c == '-') *tmp -= val;
else if(c == '*') *tmp = floor(*tmp * val);
else if(c == '/' && val != 0) *tmp = floor(*tmp / val);
}
int calculation(string str, int len, int begin){
int ans, tmp, cnt, val;
char c;
ans = 0;
tmp = 0;
c = '+';
for(int i=begin; i<len; i++){
if(str[i] == '+' || str[i] == '-'){
ans += tmp;
tmp = 0;
c = str[i];
}
else if(str[i] == '*' || str[i] == '/'){
c = str[i];
}
else if(str[i] == '('){
select_cal(c, &tmp, calculation(str, len, i+1));
cnt = 1;
do{
i++;
if(str[i] == '(') cnt++;
else if(str[i] == ')') cnt--;
}while(cnt != 0);
}
else if(str[i] == ')' || str[i] == '='){
break;
}
else {
cnt = 1;
while(str[i+cnt] >= '0' && str[i+cnt] <= '9'){
cnt++;
}
val = 0;
for(int j=0; j<cnt; j++){
val += (str[i+cnt-j-1] - '0') * pow(10, j);
}
select_cal(c, &tmp, val);
i += cnt - 1;
}
}
ans += tmp;
return ans;
}
int main(){
int n;
string str;
cin >> n;
for(int i=0; i<n; i++){
cin >> str;
cout << calculation(str, str.length(), 0) << endl;
}
return 0;
}
``` |
#include<iostream>
#include<string>
#include<ctype.h>
std::string s;
std::string::iterator it;
int term();
int fact();
// 計算
int expr()
{
int p = term();
// + or -
while( *it == '+' || *it == '-' ){
if( *it == '+' ){
it++;
int q = term();
p += q;
}
else{
it++;
int q = term();
p -= q;
}
}
return p;
}
// 項を計算
int term()
{
int p = fact();
while( *it == '*' || *it == '/' ){
if( *it == '*' ){
it++;
int q = fact();
p *= q;
}
else{
it++;
int q = fact();
p /= q;
}
}
return p;
}
// 括弧内 or 数字
int fact()
{
if( *it == '(' ){
it++;
int p = expr();
it++;
return p;
}
else{
int p = 0;
while( isdigit( *it ) ){
p *= 10;
p += *it - '0';
it++;
}
return p;
}
}
int main()
{
int n;
std::cin >> n;
while( n-- ){
std::cin >> s;
s[s.size()-1] = '\0';
it = s.begin();
std::cout << expr() << std::endl;
}
return 0;
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<iostream>
#include<string>
#include<ctype.h>
std::string s;
std::string::iterator it;
int term();
int fact();
// 計算
int expr()
{
int p = term();
// + or -
while( *it == '+' || *it == '-' ){
if( *it == '+' ){
it++;
int q = term();
p += q;
}
else{
it++;
int q = term();
p -= q;
}
}
return p;
}
// 項を計算
int term()
{
int p = fact();
while( *it == '*' || *it == '/' ){
if( *it == '*' ){
it++;
int q = fact();
p *= q;
}
else{
it++;
int q = fact();
p /= q;
}
}
return p;
}
// 括弧内 or 数字
int fact()
{
if( *it == '(' ){
it++;
int p = expr();
it++;
return p;
}
else{
int p = 0;
while( isdigit( *it ) ){
p *= 10;
p += *it - '0';
it++;
}
return p;
}
}
int main()
{
int n;
std::cin >> n;
while( n-- ){
std::cin >> s;
s[s.size()-1] = '\0';
it = s.begin();
std::cout << expr() << std::endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int p;
string s;
int number();
int factor();
int term();
int expression(){
int num=term();
while(s.size()>p){
if(s[p]=='+'){
p++;
num+=term();
}else if(s[p]=='-'){
p++;
num-=term();
}else{
break;
}
}
return num;
}
int term(){
int num=factor();
while(s.size()>p){
if(s[p]=='*'){
p++;
num*=factor();
}else if(s[p]=='/'){
p++;
num/=factor();
}else{
break;
}
}
return num;
}
int factor(){
int num;
if(s.size()>p and s[p]=='('){
p++;
num=expression();
p++;
}else{
return number();
}
return num;
}
int number(){
int num=0;
while(s.size()>p and isdigit(s[p])){
num*=10;
num+=s[p]-'0';
p++;
}
return num;
}
int main(void){
int n,i;
cin>>n;
for(i=0;i<n;i++){
cin>>s;
p=0;
cout<<expression()<<endl;
}
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int p;
string s;
int number();
int factor();
int term();
int expression(){
int num=term();
while(s.size()>p){
if(s[p]=='+'){
p++;
num+=term();
}else if(s[p]=='-'){
p++;
num-=term();
}else{
break;
}
}
return num;
}
int term(){
int num=factor();
while(s.size()>p){
if(s[p]=='*'){
p++;
num*=factor();
}else if(s[p]=='/'){
p++;
num/=factor();
}else{
break;
}
}
return num;
}
int factor(){
int num;
if(s.size()>p and s[p]=='('){
p++;
num=expression();
p++;
}else{
return number();
}
return num;
}
int number(){
int num=0;
while(s.size()>p and isdigit(s[p])){
num*=10;
num+=s[p]-'0';
p++;
}
return num;
}
int main(void){
int n,i;
cin>>n;
for(i=0;i<n;i++){
cin>>s;
p=0;
cout<<expression()<<endl;
}
}
``` |
#include<string>
#include<cstdlib>
#include<iostream>
using namespace std;
int prior[128];
int parse(string s)
{
if(s[0]=='(' && s[s.length()-1]==')'){
bool f=true;
for(int i=1,d=1;i<s.length()-1;i++){
if (s[i]=='(') d++;
else if(s[i]==')') d--;
if(d==0){ f=false; break; }
}
if(f) return parse(s.substr(1,s.length()-2));
}
int divprr=3,divpos=10000000;
for(int i=s.length()-1,d=0;i>=1;i--){
if (s[i]==')') d--;
else if(s[i]=='(') d++;
else if(d==0 && prior[s[i]]!=0){
if(divprr>prior[s[i]])
divprr=prior[s[i]],divpos=i;
}
}
if(divpos==10000000) return atoi(s.c_str());
switch(s[divpos]){
case '+': return parse(s.substr(0,divpos)) + parse(s.substr(divpos+1));
case '-': return parse(s.substr(0,divpos)) - parse(s.substr(divpos+1));
case '*': return parse(s.substr(0,divpos)) * parse(s.substr(divpos+1));
case '/': return parse(s.substr(0,divpos)) / parse(s.substr(divpos+1));
}
while(1);
}
int main()
{
prior['+']=prior['-']=1;
prior['*']=prior['/']=2;
int n; cin>>n;
while(n--){
string s; cin>>s;
s=s.substr(0,s.length()-1);
cout<<parse(s)<<endl;
}
return 0;
} | ### Prompt
Please create a solution in CPP to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<string>
#include<cstdlib>
#include<iostream>
using namespace std;
int prior[128];
int parse(string s)
{
if(s[0]=='(' && s[s.length()-1]==')'){
bool f=true;
for(int i=1,d=1;i<s.length()-1;i++){
if (s[i]=='(') d++;
else if(s[i]==')') d--;
if(d==0){ f=false; break; }
}
if(f) return parse(s.substr(1,s.length()-2));
}
int divprr=3,divpos=10000000;
for(int i=s.length()-1,d=0;i>=1;i--){
if (s[i]==')') d--;
else if(s[i]=='(') d++;
else if(d==0 && prior[s[i]]!=0){
if(divprr>prior[s[i]])
divprr=prior[s[i]],divpos=i;
}
}
if(divpos==10000000) return atoi(s.c_str());
switch(s[divpos]){
case '+': return parse(s.substr(0,divpos)) + parse(s.substr(divpos+1));
case '-': return parse(s.substr(0,divpos)) - parse(s.substr(divpos+1));
case '*': return parse(s.substr(0,divpos)) * parse(s.substr(divpos+1));
case '/': return parse(s.substr(0,divpos)) / parse(s.substr(divpos+1));
}
while(1);
}
int main()
{
prior['+']=prior['-']=1;
prior['*']=prior['/']=2;
int n; cin>>n;
while(n--){
string s; cin>>s;
s=s.substr(0,s.length()-1);
cout<<parse(s)<<endl;
}
return 0;
}
``` |
#include <string>
#include <iostream>
#include <cassert>
#include <cctype>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
string S = "3*(12+3)";
size_t cur = 0;
int expression();
int digit() {
assert(isdigit(S[cur]));
int n = S[cur] - '0';
cur++;
return n;
}
int number() {
int n = digit();
while (cur < S.size() && isdigit(S[cur]))
n = n*10 + digit();
return n;
}
int factor() {
if (S[cur] != '(') return number();
cur += 1;
int n = expression();
assert(S[cur] == ')');
cur++;
return n;
}
int term() {
int a = factor();
while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
char op = S[cur++];
int b = factor();
if (op == '*') a *= b; else a/= b;
}
return a;
}
int expression() {
int a = term();
while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
char op = S[cur++];
int b = term();
if (op == '+') a += b; else a -= b;
}
return a;
}
int parse() {
cur = 0;
return expression();
}
int main (int argc, char const* argv[]) {
int n; cin>>n;
rep(i,n) {
cin>>S;
S.erase(S.size()-1);
//cout<<S<<endl;
cout<<parse()<<endl;
}
return 0;
} | ### Prompt
Please create a solution in CPP to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <string>
#include <iostream>
#include <cassert>
#include <cctype>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
string S = "3*(12+3)";
size_t cur = 0;
int expression();
int digit() {
assert(isdigit(S[cur]));
int n = S[cur] - '0';
cur++;
return n;
}
int number() {
int n = digit();
while (cur < S.size() && isdigit(S[cur]))
n = n*10 + digit();
return n;
}
int factor() {
if (S[cur] != '(') return number();
cur += 1;
int n = expression();
assert(S[cur] == ')');
cur++;
return n;
}
int term() {
int a = factor();
while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
char op = S[cur++];
int b = factor();
if (op == '*') a *= b; else a/= b;
}
return a;
}
int expression() {
int a = term();
while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
char op = S[cur++];
int b = term();
if (op == '+') a += b; else a -= b;
}
return a;
}
int parse() {
cur = 0;
return expression();
}
int main (int argc, char const* argv[]) {
int n; cin>>n;
rep(i,n) {
cin>>S;
S.erase(S.size()-1);
//cout<<S<<endl;
cout<<parse()<<endl;
}
return 0;
}
``` |
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
typedef string::const_iterator State;
int expression(State&);
int term(State&);
int number(State&);
int factor(State&);
int expression(State &begin){
int ret = term(begin);
for(;;){
if(*begin == '+'){
++begin;
ret += term(begin);
}
else if(*begin == '-'){
++begin;
ret -= term(begin);
}
else break;
}
return ret;
}
int term(State &begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
++begin;
ret *= factor(begin);
}
else if(*begin == '/'){
++begin;
ret /= factor(begin);
}
else {
break;
}
}
return ret;
}
int factor(State &begin){
int ret;
if(*begin == '('){
++begin;
ret = expression(begin);
++begin;
}
else{
ret = number(begin);
}
return ret;
}
int number(State &begin){
int ret = 0;
while(isdigit(*begin)){
ret *= 10;
ret += *begin - '0';
++begin;
}
return ret;
}
int main(){
int n;
string q;
cin >> n;
cin.ignore();
for(int i(0);i < n;++i){
getline(cin, q);
State begin = q.begin();
cout << expression(begin) << endl;
}
return 0;
} | ### Prompt
Construct a cpp code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
typedef string::const_iterator State;
int expression(State&);
int term(State&);
int number(State&);
int factor(State&);
int expression(State &begin){
int ret = term(begin);
for(;;){
if(*begin == '+'){
++begin;
ret += term(begin);
}
else if(*begin == '-'){
++begin;
ret -= term(begin);
}
else break;
}
return ret;
}
int term(State &begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
++begin;
ret *= factor(begin);
}
else if(*begin == '/'){
++begin;
ret /= factor(begin);
}
else {
break;
}
}
return ret;
}
int factor(State &begin){
int ret;
if(*begin == '('){
++begin;
ret = expression(begin);
++begin;
}
else{
ret = number(begin);
}
return ret;
}
int number(State &begin){
int ret = 0;
while(isdigit(*begin)){
ret *= 10;
ret += *begin - '0';
++begin;
}
return ret;
}
int main(){
int n;
string q;
cin >> n;
cin.ignore();
for(int i(0);i < n;++i){
getline(cin, q);
State begin = q.begin();
cout << expression(begin) << endl;
}
return 0;
}
``` |
#include <cstdio>
#include <cctype>
using namespace std;
char s[111];
int cur,len;
int shiki();
int kazu()
{
if( s[cur] == '(' ) {
cur += 1;
int s = shiki();
cur += 1;
return s;
} else {
int a = s[cur]-'0';
cur += 1;
while( isdigit(s[cur]) ) {
a *= 10;
a += s[cur]-'0';
cur += 1;
}
return a;
}
}
int kou()
{
int a = kazu();
while( s[cur] == '*' || s[cur] == '/' ) {
char o = s[cur]; cur += 1;
int b = kazu();
o=='*'?(a*=b):(a/=b);
}
return a;
}
int shiki()
{
int a = kou();
while( s[cur] == '+' || s[cur] == '-' ) {
char o = s[cur]; cur += 1;
int b = kou();
if( o == '+' ) a += b;
else a -= b;
}
return a;
}
int parse()
{
len = 0;
while( s[len] ) len++;
len--;
cur = 0;
return shiki();
}
int main(void)
{
int n; scanf("%d",&n);
while( n-- ) {
scanf("%s",s);
printf("%d\n",parse());
}
return 0;
} | ### Prompt
Please create a solution in CPP to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <cstdio>
#include <cctype>
using namespace std;
char s[111];
int cur,len;
int shiki();
int kazu()
{
if( s[cur] == '(' ) {
cur += 1;
int s = shiki();
cur += 1;
return s;
} else {
int a = s[cur]-'0';
cur += 1;
while( isdigit(s[cur]) ) {
a *= 10;
a += s[cur]-'0';
cur += 1;
}
return a;
}
}
int kou()
{
int a = kazu();
while( s[cur] == '*' || s[cur] == '/' ) {
char o = s[cur]; cur += 1;
int b = kazu();
o=='*'?(a*=b):(a/=b);
}
return a;
}
int shiki()
{
int a = kou();
while( s[cur] == '+' || s[cur] == '-' ) {
char o = s[cur]; cur += 1;
int b = kou();
if( o == '+' ) a += b;
else a -= b;
}
return a;
}
int parse()
{
len = 0;
while( s[len] ) len++;
len--;
cur = 0;
return shiki();
}
int main(void)
{
int n; scanf("%d",&n);
while( n-- ) {
scanf("%s",s);
printf("%d\n",parse());
}
return 0;
}
``` |
#include <stdio.h>
#include <vector>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
using ll = long long;
string str;
int id;
int expr();
int number(){
int res = 0;
for (; isdigit(str[id]); id++) {
res *= 10;
res += str[id] - '0';
}
return res;
}
int factor(){
int res = 0;
if(str[id] == '('){
id++;
res = expr();
if(str[id] == ')') ++id;
return res;
}else {
return number();
}
}
int term(){
int res = factor();
while(str[id] == '*' || str[id] == '/') {
if (str[id] =='*') {
++id;
res *= factor();
} else {
++id;
res /= factor();
}
}
return res;
}
int expr(){
int res = term();
while (str[id] == '+' || str[id] == '-') {
if(str[id] == '+'){
++id;
res += term();
} else{
++id;
res -= term();
}
}
return res;
}
int main(){
int n;
cin >> n;
for (int i = 0; i < n; i++) {
id = 0;
cin >> str;
int ans = expr();
cout << ans << endl;
}
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <stdio.h>
#include <vector>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
using ll = long long;
string str;
int id;
int expr();
int number(){
int res = 0;
for (; isdigit(str[id]); id++) {
res *= 10;
res += str[id] - '0';
}
return res;
}
int factor(){
int res = 0;
if(str[id] == '('){
id++;
res = expr();
if(str[id] == ')') ++id;
return res;
}else {
return number();
}
}
int term(){
int res = factor();
while(str[id] == '*' || str[id] == '/') {
if (str[id] =='*') {
++id;
res *= factor();
} else {
++id;
res /= factor();
}
}
return res;
}
int expr(){
int res = term();
while (str[id] == '+' || str[id] == '-') {
if(str[id] == '+'){
++id;
res += term();
} else{
++id;
res -= term();
}
}
return res;
}
int main(){
int n;
cin >> n;
for (int i = 0; i < n; i++) {
id = 0;
cin >> str;
int ans = expr();
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
#define LL long long
using namespace std;
typedef string::const_iterator State;
class ParseError{};
int factor(State &begin);
int number(State &begin){
int ret=0;
while(isdigit(*begin)){
ret*=10;
ret+=*begin -'0';
begin++;
}
return ret;
}
int term(State &begin){
int ret=factor(begin);
while(true){
if(*begin=='*')ret*=factor(++begin);
else if(*begin=='/')ret/=factor(++begin);
else break;
}
return ret;
}
int expression(State &begin){
int ret=term(begin);
while(true){
if(*begin=='+')ret+=term(++begin);
else if(*begin=='-')ret-=term(++begin);
else break;
}
return ret;
}
int factor(State &begin){
int ret;
if(*begin=='('){
ret=expression(++begin);
begin++;
}else{
ret =number(begin);
}
return ret;
}
int main(){
int N;
cin>>N;
cin.ignore();
string s[N];
REP(i,N)getline(cin,s[i]);
REP(i,N){
s[i]=s[i].substr(0,s[i].size()-1);
State begin=s[i].begin();
int ret=expression(begin);
cout<<ret<<endl;
}
return 0;
} | ### Prompt
Please create a solution in CPP to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
#define LL long long
using namespace std;
typedef string::const_iterator State;
class ParseError{};
int factor(State &begin);
int number(State &begin){
int ret=0;
while(isdigit(*begin)){
ret*=10;
ret+=*begin -'0';
begin++;
}
return ret;
}
int term(State &begin){
int ret=factor(begin);
while(true){
if(*begin=='*')ret*=factor(++begin);
else if(*begin=='/')ret/=factor(++begin);
else break;
}
return ret;
}
int expression(State &begin){
int ret=term(begin);
while(true){
if(*begin=='+')ret+=term(++begin);
else if(*begin=='-')ret-=term(++begin);
else break;
}
return ret;
}
int factor(State &begin){
int ret;
if(*begin=='('){
ret=expression(++begin);
begin++;
}else{
ret =number(begin);
}
return ret;
}
int main(){
int N;
cin>>N;
cin.ignore();
string s[N];
REP(i,N)getline(cin,s[i]);
REP(i,N){
s[i]=s[i].substr(0,s[i].size()-1);
State begin=s[i].begin();
int ret=expression(begin);
cout<<ret<<endl;
}
return 0;
}
``` |
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int expression(void);
string str;
int pos;
int factor(void){
int num = 0;
if( isdigit( str[pos] ) ){ // '0' <= str[pos] && str[pos] <= '9'
while( isdigit( str[pos] ) ){
num *= 10;
num += str[pos] - '0';
pos++;
}
return num;
}else if( str[pos] == '(' ){
pos++;
int res = expression();
pos++; // ')' テ」ツ?ョテ・ツ按?
return res;
}
}
int term(void){
int res = factor();
while(1){
if( str[pos] == '*' ){
pos++;
res *= factor();
}else if( str[pos] == '/' ){
pos++;
res /= factor();
}else break;
}
return res;
}
int expression(void){
int res = term();
while(1){
if( str[pos] == '+' ){
pos++;
res += term();
}else if( str[pos] == '-' ){
pos++;
res -= term();
}else break; // '=' テ」ツ?禿」ツ?禿」ツ?ァテ」ツつケテ」ツδォテ」ツδシ
}
return res;
}
int main(void){
int dn;
cin >> dn;
while(dn--){
pos = 0;
cin >> str;
cout << expression() << endl;
}
return 0;
} | ### Prompt
Create a solution in cpp for the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int expression(void);
string str;
int pos;
int factor(void){
int num = 0;
if( isdigit( str[pos] ) ){ // '0' <= str[pos] && str[pos] <= '9'
while( isdigit( str[pos] ) ){
num *= 10;
num += str[pos] - '0';
pos++;
}
return num;
}else if( str[pos] == '(' ){
pos++;
int res = expression();
pos++; // ')' テ」ツ?ョテ・ツ按?
return res;
}
}
int term(void){
int res = factor();
while(1){
if( str[pos] == '*' ){
pos++;
res *= factor();
}else if( str[pos] == '/' ){
pos++;
res /= factor();
}else break;
}
return res;
}
int expression(void){
int res = term();
while(1){
if( str[pos] == '+' ){
pos++;
res += term();
}else if( str[pos] == '-' ){
pos++;
res -= term();
}else break; // '=' テ」ツ?禿」ツ?禿」ツ?ァテ」ツつケテ」ツδォテ」ツδシ
}
return res;
}
int main(void){
int dn;
cin >> dn;
while(dn--){
pos = 0;
cin >> str;
cout << expression() << endl;
}
return 0;
}
``` |
#include <iostream>
#include <cctype>
#include<cassert>
using namespace std;
size_t cur=0;
string S;
int digit(){
assert(isdigit(S[cur]));
int n=S[cur]-'0';
cur=cur+1;
return n;
}
int number(){
int n=digit();
while(cur<S.size() && isdigit(S[cur])){
n=n*10+digit();
}
return n;
}
int expression();
int factor(){
if(S[cur]!='(') return number();
cur=cur+1;
int n=expression();
assert(S[cur]==')');
cur=cur+1;
return n;
}
int term(){
int a=factor();
while(cur<S.size() && (S[cur]=='*' || S[cur]=='/')){
char op = S[cur++];
int b= factor();
if (op== '*')a*=b;
else a/=b;
}
return a;
}
int expression(){
int a=term();
while((S[cur]=='+'||S[cur]=='-')&&cur<S.size()){
char op =S[cur++];
int b=term();
if (op=='+'){
a=a+b;
}
else{
a=a-b;
}
}
return a;
}
int parse(){ return expression();}
int main() {
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>S;
cur=0;
S.resize(S.size()-1);
int a=parse();
cout<<a<<endl;
}
return 0;
} | ### Prompt
In Cpp, your task is to solve the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <cctype>
#include<cassert>
using namespace std;
size_t cur=0;
string S;
int digit(){
assert(isdigit(S[cur]));
int n=S[cur]-'0';
cur=cur+1;
return n;
}
int number(){
int n=digit();
while(cur<S.size() && isdigit(S[cur])){
n=n*10+digit();
}
return n;
}
int expression();
int factor(){
if(S[cur]!='(') return number();
cur=cur+1;
int n=expression();
assert(S[cur]==')');
cur=cur+1;
return n;
}
int term(){
int a=factor();
while(cur<S.size() && (S[cur]=='*' || S[cur]=='/')){
char op = S[cur++];
int b= factor();
if (op== '*')a*=b;
else a/=b;
}
return a;
}
int expression(){
int a=term();
while((S[cur]=='+'||S[cur]=='-')&&cur<S.size()){
char op =S[cur++];
int b=term();
if (op=='+'){
a=a+b;
}
else{
a=a-b;
}
}
return a;
}
int parse(){ return expression();}
int main() {
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>S;
cur=0;
S.resize(S.size()-1);
int a=parse();
cout<<a<<endl;
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef string::const_iterator State;
class ParseError{};
int expr(State& begin);
int term(State& begin);
int number(State& begin);
int fact(State& begin);
int number(State& begin){
int res = 0;
while(isdigit(*begin)){
res *= 10;
res += *begin - '0';
begin++;
}
return res;
}
int term(State& begin){
int res = fact(begin);
while(true){
if(*begin == '*'){
begin++;
res *= fact(begin);
}else if(*begin == '/'){
begin++;
res /= fact(begin);
}else{
break;
}
}
return res;
}
int expr(State& begin){
int res = term(begin);
while(true){
if(*begin == '+'){
begin++;
res += term(begin);
}else if(*begin == '-'){
begin++;
res -= term(begin);
}else{
break;
}
}
return res;
}
int fact(State& begin){
if(*begin == '('){
begin++;
int res = expr(begin);
begin++;
return res;
}
return number(begin);
}
int main(){
int n;
cin >> n;
for(int i = 0;i < n;i++){
string s;
cin >> s;
s = s.substr(0,s.size()-1);
State begin = s.begin();
cout << expr(begin) << endl;
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef string::const_iterator State;
class ParseError{};
int expr(State& begin);
int term(State& begin);
int number(State& begin);
int fact(State& begin);
int number(State& begin){
int res = 0;
while(isdigit(*begin)){
res *= 10;
res += *begin - '0';
begin++;
}
return res;
}
int term(State& begin){
int res = fact(begin);
while(true){
if(*begin == '*'){
begin++;
res *= fact(begin);
}else if(*begin == '/'){
begin++;
res /= fact(begin);
}else{
break;
}
}
return res;
}
int expr(State& begin){
int res = term(begin);
while(true){
if(*begin == '+'){
begin++;
res += term(begin);
}else if(*begin == '-'){
begin++;
res -= term(begin);
}else{
break;
}
}
return res;
}
int fact(State& begin){
if(*begin == '('){
begin++;
int res = expr(begin);
begin++;
return res;
}
return number(begin);
}
int main(){
int n;
cin >> n;
for(int i = 0;i < n;i++){
string s;
cin >> s;
s = s.substr(0,s.size()-1);
State begin = s.begin();
cout << expr(begin) << endl;
}
}
``` |
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
typedef string::iterator si;
int expr(si& cur, si end);
int num(si& cur, si end){
int ans = 0;
while(cur != end && isdigit(*cur)){
ans *= 10;
ans += *cur-'0';
cur++;
}
return ans;
}
int fact(si& cur, si end){
int ans = 1;
if(*cur == '('){
cur++;
ans *= expr(cur, end);
cur++;
} else {
ans *= num(cur, end);
}
return ans;
}
int term(si& cur, si end){
int ans = 1;
char op = '*';
while(1){
int f = fact(cur,end);
if(op == '*') ans *= f;
else ans /= f;
if(cur == end || *cur == ')' || (*cur != '*' && *cur != '/')) break;
else op = *cur;
cur++;
}
return ans;
}
int expr(si& cur, si end){
int ans = 0;
char op = '+';
while(1){
int t = term(cur, end);
if(op == '+') ans += t;
else ans -= t;
if(cur == end || *cur == ')') break;
else op = *cur;
cur++;
}
return ans;
}
int main(){
int n; cin>>n;
while(n--){
string s; cin>> s;
si b = s.begin();
cout << expr(b, s.end()-1) << endl;
}
} | ### Prompt
Develop a solution in CPP to the problem described below:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
typedef string::iterator si;
int expr(si& cur, si end);
int num(si& cur, si end){
int ans = 0;
while(cur != end && isdigit(*cur)){
ans *= 10;
ans += *cur-'0';
cur++;
}
return ans;
}
int fact(si& cur, si end){
int ans = 1;
if(*cur == '('){
cur++;
ans *= expr(cur, end);
cur++;
} else {
ans *= num(cur, end);
}
return ans;
}
int term(si& cur, si end){
int ans = 1;
char op = '*';
while(1){
int f = fact(cur,end);
if(op == '*') ans *= f;
else ans /= f;
if(cur == end || *cur == ')' || (*cur != '*' && *cur != '/')) break;
else op = *cur;
cur++;
}
return ans;
}
int expr(si& cur, si end){
int ans = 0;
char op = '+';
while(1){
int t = term(cur, end);
if(op == '+') ans += t;
else ans -= t;
if(cur == end || *cur == ')') break;
else op = *cur;
cur++;
}
return ans;
}
int main(){
int n; cin>>n;
while(n--){
string s; cin>> s;
si b = s.begin();
cout << expr(b, s.end()-1) << endl;
}
}
``` |
#include <iostream>
#include <cmath>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;
int expr();
int term();
int fact();
char *p;
char inp[101];
int main(){
int n;
cin >> n;
cin.ignore();
while (n--){
cin.getline(inp, sizeof(inp));
p = inp;
cout << expr() << endl;
}
}
int expr(){
int m = 0;
m = term();
while (*p == '+' || *p == '-'){
if (*p == '+')
p++, m += term();
else if (*p == '-')
p++, m -= term();
}
return m;
}
int term(){
int m = 0;
m = fact();
while (*p == '*' || *p == '/'){
if (*p == '*')
p++, m *= fact();
else if (*p == '/')
p++, m /= fact();
}
return m;
}
int fact(){
int m = 0;
if (*p == '('){
p++;
m = expr();
p++;
return m;
}
else {
while ('0' <= *p && *p <= '9'){
m *= 10;
m += *p - '0';
p++;
}
}
return m;
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <cmath>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;
int expr();
int term();
int fact();
char *p;
char inp[101];
int main(){
int n;
cin >> n;
cin.ignore();
while (n--){
cin.getline(inp, sizeof(inp));
p = inp;
cout << expr() << endl;
}
}
int expr(){
int m = 0;
m = term();
while (*p == '+' || *p == '-'){
if (*p == '+')
p++, m += term();
else if (*p == '-')
p++, m -= term();
}
return m;
}
int term(){
int m = 0;
m = fact();
while (*p == '*' || *p == '/'){
if (*p == '*')
p++, m *= fact();
else if (*p == '/')
p++, m /= fact();
}
return m;
}
int fact(){
int m = 0;
if (*p == '('){
p++;
m = expr();
p++;
return m;
}
else {
while ('0' <= *p && *p <= '9'){
m *= 10;
m += *p - '0';
p++;
}
}
return m;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int exp(string&, int&);
int term(string&, int&);
int factor(string&, int&);
int exp(string& str, int& p){
int res = term(str, p);
while(str[p] == '+' || str[p] == '-'){
if(str[p] == '+'){
p++;
res += term(str, p);
}else{
p++;
res -= term(str, p);
}
}
return res;
}
int term(string& str, int& p){
int res = factor(str, p);
while(str[p] == '*' || str[p] == '/'){
if(str[p] == '*'){
p++;
res *= factor(str, p);
}else{
p++;
res /= factor(str, p);
}
}
return res;
}
int factor(string& str, int& p){
int res = 0;
int keta = 1;
int initp = p;
if(str[p] == '('){
while(str[p] != ')'){
p++;
res = exp(str, p);
}
p++;
}else{
while(isdigit(str[p])){
p++;
}
for(int i=p-1; i >= initp; i--){
res += keta * (str[i] - '0');
keta *= 10;
}
}
return res;
}
int main(void){
int n; cin >> n;
while(n--){
int p=0;
string str;
cin >> str;
cout << exp(str, p) << endl;
}
return 0;
} | ### Prompt
Your challenge is to write a cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int exp(string&, int&);
int term(string&, int&);
int factor(string&, int&);
int exp(string& str, int& p){
int res = term(str, p);
while(str[p] == '+' || str[p] == '-'){
if(str[p] == '+'){
p++;
res += term(str, p);
}else{
p++;
res -= term(str, p);
}
}
return res;
}
int term(string& str, int& p){
int res = factor(str, p);
while(str[p] == '*' || str[p] == '/'){
if(str[p] == '*'){
p++;
res *= factor(str, p);
}else{
p++;
res /= factor(str, p);
}
}
return res;
}
int factor(string& str, int& p){
int res = 0;
int keta = 1;
int initp = p;
if(str[p] == '('){
while(str[p] != ')'){
p++;
res = exp(str, p);
}
p++;
}else{
while(isdigit(str[p])){
p++;
}
for(int i=p-1; i >= initp; i--){
res += keta * (str[i] - '0');
keta *= 10;
}
}
return res;
}
int main(void){
int n; cin >> n;
while(n--){
int p=0;
string str;
cin >> str;
cout << exp(str, p) << endl;
}
return 0;
}
``` |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef string::const_iterator State;
#define rep(i,n) for(int i=0;i<(n);i++)
class Parser{
public:
int expression(State &begin){
int ret = term(begin);
for(;;){
if(*begin == '+'){
begin++;
ret += term(begin);
}else if(*begin == '-'){
begin++;
ret -= term(begin);
}else{
break;
}
}
return ret;
}
int term(State &begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
begin++;
ret *= factor(begin);
}else if(*begin == '/'){
begin++;
ret /= factor(begin);
}else{
break;
}
}
return ret;
}
int factor(State &begin){
int ret;
if(*begin == '('){
begin++;//'('?????????
ret = expression(begin);
begin++;//')'?????????
}else{
ret = number(begin);
}
return ret;
}
int number(State &begin){
int ret = 0;
while(isdigit(*begin)){
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
};
int main(void){
int n; cin >> n;
rep(i, n){
Parser ps;
string s; cin >> s;
string tmp = s.substr(0, s.size() - 1);
// cout << tmp << endl;
State begin = s.begin();
int ret = ps.expression(begin);
printf("%d\n", ret);
}
return 0;
} | ### Prompt
Create a solution in cpp for the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef string::const_iterator State;
#define rep(i,n) for(int i=0;i<(n);i++)
class Parser{
public:
int expression(State &begin){
int ret = term(begin);
for(;;){
if(*begin == '+'){
begin++;
ret += term(begin);
}else if(*begin == '-'){
begin++;
ret -= term(begin);
}else{
break;
}
}
return ret;
}
int term(State &begin){
int ret = factor(begin);
for(;;){
if(*begin == '*'){
begin++;
ret *= factor(begin);
}else if(*begin == '/'){
begin++;
ret /= factor(begin);
}else{
break;
}
}
return ret;
}
int factor(State &begin){
int ret;
if(*begin == '('){
begin++;//'('?????????
ret = expression(begin);
begin++;//')'?????????
}else{
ret = number(begin);
}
return ret;
}
int number(State &begin){
int ret = 0;
while(isdigit(*begin)){
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
};
int main(void){
int n; cin >> n;
rep(i, n){
Parser ps;
string s; cin >> s;
string tmp = s.substr(0, s.size() - 1);
// cout << tmp << endl;
State begin = s.begin();
int ret = ps.expression(begin);
printf("%d\n", ret);
}
return 0;
}
``` |
#include <iostream>
#include <string>
using namespace std;
int calc1();
int calc2();
int calc3();
int p;
string eq;
int calc1() {
int a = calc2();
while (eq[p]=='+' || eq[p]=='-') {
if (eq[p]=='+') {
p++;
a += calc2();
} else if (eq[p]=='-') {
p++;
a -= calc2();
}
}
return a;
}
int calc2() {
int a = calc3();
while (eq[p]=='*' || eq[p]=='/') {
if (eq[p]=='*') {
p++;
a *= calc3();
} else if (eq[p]=='/') {
p++;
a /= calc3();
}
}
return a;
}
int calc3() {
int a;
if (eq[p]=='(') {
p++;
a = calc1();
p++;
} else {
a = 0;
while (eq[p]>='0'&&eq[p]<='9') {
a *= 10;
a += eq[p++] - '0';
}
}
return a;
}
int main() {
int n, r;
cin >> n;
for (int i=0; i<n; i++) {
cin >> eq;
p = 0;
r = calc1();
cout << r << endl;
}
} | ### Prompt
Please create a solution in cpp to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
using namespace std;
int calc1();
int calc2();
int calc3();
int p;
string eq;
int calc1() {
int a = calc2();
while (eq[p]=='+' || eq[p]=='-') {
if (eq[p]=='+') {
p++;
a += calc2();
} else if (eq[p]=='-') {
p++;
a -= calc2();
}
}
return a;
}
int calc2() {
int a = calc3();
while (eq[p]=='*' || eq[p]=='/') {
if (eq[p]=='*') {
p++;
a *= calc3();
} else if (eq[p]=='/') {
p++;
a /= calc3();
}
}
return a;
}
int calc3() {
int a;
if (eq[p]=='(') {
p++;
a = calc1();
p++;
} else {
a = 0;
while (eq[p]>='0'&&eq[p]<='9') {
a *= 10;
a += eq[p++] - '0';
}
}
return a;
}
int main() {
int n, r;
cin >> n;
for (int i=0; i<n; i++) {
cin >> eq;
p = 0;
r = calc1();
cout << r << endl;
}
}
``` |
#include<iostream>
#include<string>
using namespace std;
int exp();
int term();
int factor();
string str;
int p;
int factor(){
int val=0;
while(str[p]>='0' && str[p]<='9'){
val*=10;
val+=str[p]-'0';
p++;
}
if(val!=0) return val;
else if(str[p]=='('){
p++;
val=exp();
}
if(str[p]==')'){
p++;
return val;
}
if(val==0) return 0;
}
int term(){
int val=factor();
while(str[p]=='*' || str[p]=='/'){
if(str[p]=='*'){
p++;
val*=factor();
}else if(str[p]=='/'){
p++;
val/=factor();
}
}
return val;
}
int exp(){
int val=term();
while(str[p]=='+' || str[p]=='-'){
if(str[p]=='+'){
p++;
val+=term();
}else if(str[p]=='-'){
p++;
val-=term();
}
}
return val;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
p=0;
cin>>str;
cout<<exp()<<endl;
}
return 0;
} | ### Prompt
Please formulate a CPP solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<iostream>
#include<string>
using namespace std;
int exp();
int term();
int factor();
string str;
int p;
int factor(){
int val=0;
while(str[p]>='0' && str[p]<='9'){
val*=10;
val+=str[p]-'0';
p++;
}
if(val!=0) return val;
else if(str[p]=='('){
p++;
val=exp();
}
if(str[p]==')'){
p++;
return val;
}
if(val==0) return 0;
}
int term(){
int val=factor();
while(str[p]=='*' || str[p]=='/'){
if(str[p]=='*'){
p++;
val*=factor();
}else if(str[p]=='/'){
p++;
val/=factor();
}
}
return val;
}
int exp(){
int val=term();
while(str[p]=='+' || str[p]=='-'){
if(str[p]=='+'){
p++;
val+=term();
}else if(str[p]=='-'){
p++;
val-=term();
}
}
return val;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
p=0;
cin>>str;
cout<<exp()<<endl;
}
return 0;
}
``` |
#include <cmath>
#include <cassert>
#include <iostream>
using namespace std;
string S;
size_t cur = 0;
int digit() {
int n;
assert(isdigit(S[cur]));
n = S[cur] -'0';
++cur;
return n;
}
int number() {
int n = digit();
while (cur < S.size() && isdigit(S[cur])) {
n = n*10 + digit();
}
return n;
}
int expression();
int factor() {
int n;
if (S[cur] != '(') n = number();
else {
++cur;
n = expression();
assert(S[cur] == ')');
++cur;
}
return n;
}
int term() {
int t = factor();
while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
char op = S[cur++];
int b = factor();
if(op == '*') t *= b; else t = (int)trunc(1.0*t/b);
//cout << t << endl;
}
return t;
}
/*int expression() {
int sum = number();
while (S[cur] == '+' || S[cur] == '-') {
char op = S[cur++];
int b = number();
if(op == +) sum += b; else return sum -= b;
}
return sum;
}*/
int expression() {
int sum = term();
while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
char op = S[cur++];
int b = term();
if(op == '+') sum += b; else sum -= b;
}
return sum;
}
int main() {
int N;
cin >> N;
for (int i=0; i<N; ++i) {
cur = 0;
cin >> S;
S.resize(S.size()-1);
cout << expression() << endl;
}
return 0;
} | ### Prompt
Your task is to create a CPP solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <cmath>
#include <cassert>
#include <iostream>
using namespace std;
string S;
size_t cur = 0;
int digit() {
int n;
assert(isdigit(S[cur]));
n = S[cur] -'0';
++cur;
return n;
}
int number() {
int n = digit();
while (cur < S.size() && isdigit(S[cur])) {
n = n*10 + digit();
}
return n;
}
int expression();
int factor() {
int n;
if (S[cur] != '(') n = number();
else {
++cur;
n = expression();
assert(S[cur] == ')');
++cur;
}
return n;
}
int term() {
int t = factor();
while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
char op = S[cur++];
int b = factor();
if(op == '*') t *= b; else t = (int)trunc(1.0*t/b);
//cout << t << endl;
}
return t;
}
/*int expression() {
int sum = number();
while (S[cur] == '+' || S[cur] == '-') {
char op = S[cur++];
int b = number();
if(op == +) sum += b; else return sum -= b;
}
return sum;
}*/
int expression() {
int sum = term();
while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
char op = S[cur++];
int b = term();
if(op == '+') sum += b; else sum -= b;
}
return sum;
}
int main() {
int N;
cin >> N;
for (int i=0; i<N; ++i) {
cur = 0;
cin >> S;
S.resize(S.size()-1);
cout << expression() << endl;
}
return 0;
}
``` |
#include <stdio.h>
#include <assert.h>
char buf[128];
int pos;
#define NOW (buf[pos])
bool isdigit(char c) {
return ('0' <= c && c <= '9');
}
int digit() {
if ( !isdigit(NOW) ) {
fprintf(stderr, "%c: ", NOW);
assert( isdigit(NOW) );
}
int ret = NOW - '0';
pos++;
return ret;
}
int number() {
int ret = digit();
while( isdigit(NOW) ) {
ret = ret*10 + digit();
}
return ret;
}
int expression();
int factor() {
if ( NOW=='(' ) {
pos++;
int ret = expression();
assert(NOW==')');
pos++;
return ret;
}
return number();
}
int term() {
int ret = factor();
while ( NOW=='*' || NOW=='/' ) {
char oper = NOW;
pos++;
int right = factor();
if ( oper == '*' ) {
ret *= right;
} else {
ret /= right;
}
}
return ret;
}
int expression() {
int ret = term();
while ( NOW=='+' || NOW=='-' ) {
char oper = NOW;
pos++;
int right = term();
if ( oper == '+' ) {
ret += right;
} else {
ret -= right;
}
}
return ret;
}
int main(void) {
int set;
scanf("%d", &set);
while(set--) {
scanf("%s", buf);
pos = 0;
printf("%d\n", expression());
}
} | ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <stdio.h>
#include <assert.h>
char buf[128];
int pos;
#define NOW (buf[pos])
bool isdigit(char c) {
return ('0' <= c && c <= '9');
}
int digit() {
if ( !isdigit(NOW) ) {
fprintf(stderr, "%c: ", NOW);
assert( isdigit(NOW) );
}
int ret = NOW - '0';
pos++;
return ret;
}
int number() {
int ret = digit();
while( isdigit(NOW) ) {
ret = ret*10 + digit();
}
return ret;
}
int expression();
int factor() {
if ( NOW=='(' ) {
pos++;
int ret = expression();
assert(NOW==')');
pos++;
return ret;
}
return number();
}
int term() {
int ret = factor();
while ( NOW=='*' || NOW=='/' ) {
char oper = NOW;
pos++;
int right = factor();
if ( oper == '*' ) {
ret *= right;
} else {
ret /= right;
}
}
return ret;
}
int expression() {
int ret = term();
while ( NOW=='+' || NOW=='-' ) {
char oper = NOW;
pos++;
int right = term();
if ( oper == '+' ) {
ret += right;
} else {
ret -= right;
}
}
return ret;
}
int main(void) {
int set;
scanf("%d", &set);
while(set--) {
scanf("%s", buf);
pos = 0;
printf("%d\n", expression());
}
}
``` |
#include <iostream>
#include <string>
using namespace std;
typedef pair<int, int> result;
#define value first
#define p second
result equation(const string &s, int p = 0);
result factor(const string &s, int p = 0);
result term(const string &s, int p = 0);
result equation(const string &s, int p) {
result r = factor(s, p);
while( s[r.p] == '+' || s[r.p] == '-' ){
result r_ = factor(s, r.p+1);
if( s[r.p] == '+' )
r.value += r_.value;
else
r.value -= r_.value;
r.p = r_.p;
}
return r;
}
result factor(const string &s, int p) {
result r = term(s, p);
while( s[r.p] == '*' || s[r.p] == '/' ) {
result r_ = term(s, r.p+1);
if( s[r.p] == '*' )
r.value *= r_.value;
else
r.value /= r_.value;
r.p = r_.p;
}
return r;
}
result term(const string &s, int p) {
if( s[p] == '(' ) {
result r = equation(s, p+1);
r.p += 1; // skip ')'
return r;
}else{
int value = 0;
while ( isdigit(s[p]) )
value = value * 10 + (s[p++] - '0');
return result(value, p);
}
}
int main() {
string s;
int n;
cin >> n;
for(int i=0 ; i < n ; i++ ){
cin >> s;
result r = equation( s );
cout << r.value << endl;
}
} | ### Prompt
Construct a cpp code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
using namespace std;
typedef pair<int, int> result;
#define value first
#define p second
result equation(const string &s, int p = 0);
result factor(const string &s, int p = 0);
result term(const string &s, int p = 0);
result equation(const string &s, int p) {
result r = factor(s, p);
while( s[r.p] == '+' || s[r.p] == '-' ){
result r_ = factor(s, r.p+1);
if( s[r.p] == '+' )
r.value += r_.value;
else
r.value -= r_.value;
r.p = r_.p;
}
return r;
}
result factor(const string &s, int p) {
result r = term(s, p);
while( s[r.p] == '*' || s[r.p] == '/' ) {
result r_ = term(s, r.p+1);
if( s[r.p] == '*' )
r.value *= r_.value;
else
r.value /= r_.value;
r.p = r_.p;
}
return r;
}
result term(const string &s, int p) {
if( s[p] == '(' ) {
result r = equation(s, p+1);
r.p += 1; // skip ')'
return r;
}else{
int value = 0;
while ( isdigit(s[p]) )
value = value * 10 + (s[p++] - '0');
return result(value, p);
}
}
int main() {
string s;
int n;
cin >> n;
for(int i=0 ; i < n ; i++ ){
cin >> s;
result r = equation( s );
cout << r.value << endl;
}
}
``` |
#include <iostream>
#include <cassert>
#include <cctype>
using namespace std;
string S;
size_t cur = 0;
int term();
int factor();
int expression();
int digit(){
assert(isdigit(S[cur]));
int n = S[cur] - '0';
cur = cur + 1;
return n;
}
int number(){
int n = digit();
while(cur<S.size() && isdigit(S[cur]))
n = n*10 + digit();
return n;
}
int expression(){
int a =term();
while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')){
char op = S[cur++];
int b = term();
if (op == '+') a += b;
else a -= b;
}
return a;
}
int term(){
int a =factor();
while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')){
char op = S[cur++];
int b = factor();
if (op == '*') a *= b; else a /= b;
}
return a;
}
int factor(){
if (S[cur] != '(') return number();
cur += 1;
int n = expression();
assert(S[cur] == ')');
cur +=1;
return n;
}
int main() {
int N;
cin >> N;
for (int i=0; i<N;++i){
cur = 0;
cin >> S;
S.resize(S.size()-1);
cout << expression() << endl;
}
return 0;
} | ### Prompt
Construct a cpp code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <cassert>
#include <cctype>
using namespace std;
string S;
size_t cur = 0;
int term();
int factor();
int expression();
int digit(){
assert(isdigit(S[cur]));
int n = S[cur] - '0';
cur = cur + 1;
return n;
}
int number(){
int n = digit();
while(cur<S.size() && isdigit(S[cur]))
n = n*10 + digit();
return n;
}
int expression(){
int a =term();
while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')){
char op = S[cur++];
int b = term();
if (op == '+') a += b;
else a -= b;
}
return a;
}
int term(){
int a =factor();
while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')){
char op = S[cur++];
int b = factor();
if (op == '*') a *= b; else a /= b;
}
return a;
}
int factor(){
if (S[cur] != '(') return number();
cur += 1;
int n = expression();
assert(S[cur] == ')');
cur +=1;
return n;
}
int main() {
int N;
cin >> N;
for (int i=0; i<N;++i){
cur = 0;
cin >> S;
S.resize(S.size()-1);
cout << expression() << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int expr(string& s, int& i);
int term(string& s, int& i);
int factor(string& s, int& i);
int number(string& s, int& i);
int expr(string& s, int& i) {
int val = term(s, i);
while (s[i] == '+' || s[i] == '-') {
char op = s[i];
++i;
int val2 = term(s, i);
if (op == '+') val += val2;
else val -= val2;
}
return val;
}
int term(string& s, int& i) {
int val = factor(s, i);
while (s[i] == '*' || s[i] == '/') {
char op = s[i];
++i;
int val2 = factor(s, i);
if (op == '*') val *= val2;
else val /= val2;
}
return val;
}
int factor(string& s, int& i) {
if (isdigit(s[i])) return number(s, i);
++i;
int res = expr(s, i);
++i;
return res;
}
int number(string& s, int& i) {
int res = s[i++] - '0';
while (isdigit(s[i])) res = res * 10 + s[i++] - '0';
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
s = s.substr(0, s.size() - 1);
int j = 0;
cout << expr(s, j) << endl;
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int expr(string& s, int& i);
int term(string& s, int& i);
int factor(string& s, int& i);
int number(string& s, int& i);
int expr(string& s, int& i) {
int val = term(s, i);
while (s[i] == '+' || s[i] == '-') {
char op = s[i];
++i;
int val2 = term(s, i);
if (op == '+') val += val2;
else val -= val2;
}
return val;
}
int term(string& s, int& i) {
int val = factor(s, i);
while (s[i] == '*' || s[i] == '/') {
char op = s[i];
++i;
int val2 = factor(s, i);
if (op == '*') val *= val2;
else val /= val2;
}
return val;
}
int factor(string& s, int& i) {
if (isdigit(s[i])) return number(s, i);
++i;
int res = expr(s, i);
++i;
return res;
}
int number(string& s, int& i) {
int res = s[i++] - '0';
while (isdigit(s[i])) res = res * 10 + s[i++] - '0';
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
s = s.substr(0, s.size() - 1);
int j = 0;
cout << expr(s, j) << endl;
}
}
``` |
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int expression();
int term();
int factor();
int number();
int now;
string s;
int expression(){
int res=term();
while(true){
if(s[now]=='+')now++,res+=term();
else if(s[now]=='-')now++,res-=term();
else break;
}
return res;
}
int term(){
int res=factor();
while(true){
if(s[now]=='*')now++,res*=factor();
else if(s[now]=='/')now++,res/=factor();
else break;
}
return res;
}
int factor(){
int res=0;
if(s[now]=='('){
now++;
res=expression();
now++;
}
else return number();
return res;
}
int number(){
int res=0;
while('0'<=s[now] && s[now]<='9'){
res*=10;
res+=s[now++]-'0';
}
return res;
}
int main(void){
int n;
cin >> n;
while(n--){
cin >> s;
now=0;
cout << expression() << endl;
}
return 0;
} | ### Prompt
Generate a cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int expression();
int term();
int factor();
int number();
int now;
string s;
int expression(){
int res=term();
while(true){
if(s[now]=='+')now++,res+=term();
else if(s[now]=='-')now++,res-=term();
else break;
}
return res;
}
int term(){
int res=factor();
while(true){
if(s[now]=='*')now++,res*=factor();
else if(s[now]=='/')now++,res/=factor();
else break;
}
return res;
}
int factor(){
int res=0;
if(s[now]=='('){
now++;
res=expression();
now++;
}
else return number();
return res;
}
int number(){
int res=0;
while('0'<=s[now] && s[now]<='9'){
res*=10;
res+=s[now++]-'0';
}
return res;
}
int main(void){
int n;
cin >> n;
while(n--){
cin >> s;
now=0;
cout << expression() << endl;
}
return 0;
}
``` |
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>
using namespace std;
string Str;
unsigned int cur = 0;
int digit();
int number();
int term();
int facter();
int expression();
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cur = 0;
cin >> Str;
Str.resize(Str.size()-1);
int ans = expression();
cout << ans << endl;
}
return 0;
}
int digit()
{
assert(isdigit(Str[cur]));
int n = Str[cur] - '0';
cur++;
return n;
}
int number()
{
int n = digit();
while(cur < Str.size() && isdigit(Str[cur])) n = n*10 + digit();
return n;
}
int factor()
{
if(Str[cur] != '(') return number();
cur++;
int n = expression();
assert(Str[cur] == ')');
cur++;
return n;
}
int term()
{
int a = factor();
while(cur < Str.size() && (Str[cur] == '*' || Str[cur] == '/')){
char op = Str[cur++];
int b = factor();
if(op == '*') a *= b;
else a /= b;
}
return a;
}
int expression()
{
int a = term();
while(cur < Str.size() && (Str[cur] == '+' || Str[cur] == '-')){
char op = Str[cur++];
int b = term();
if(op == '+') a += b;
else a -= b;
}
return a;
} | ### Prompt
In CPP, your task is to solve the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>
using namespace std;
string Str;
unsigned int cur = 0;
int digit();
int number();
int term();
int facter();
int expression();
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cur = 0;
cin >> Str;
Str.resize(Str.size()-1);
int ans = expression();
cout << ans << endl;
}
return 0;
}
int digit()
{
assert(isdigit(Str[cur]));
int n = Str[cur] - '0';
cur++;
return n;
}
int number()
{
int n = digit();
while(cur < Str.size() && isdigit(Str[cur])) n = n*10 + digit();
return n;
}
int factor()
{
if(Str[cur] != '(') return number();
cur++;
int n = expression();
assert(Str[cur] == ')');
cur++;
return n;
}
int term()
{
int a = factor();
while(cur < Str.size() && (Str[cur] == '*' || Str[cur] == '/')){
char op = Str[cur++];
int b = factor();
if(op == '*') a *= b;
else a /= b;
}
return a;
}
int expression()
{
int a = term();
while(cur < Str.size() && (Str[cur] == '+' || Str[cur] == '-')){
char op = Str[cur++];
int b = term();
if(op == '+') a += b;
else a -= b;
}
return a;
}
``` |
#include <iostream>
using namespace std;
int cal(string&);
int expression(string&, int&);
int kou(string&, int&);
int num(string&, int&);
int cal(string& s) {
int pos = 0;
return expression(s, pos);
}
int expression(string& s, int& pos) {
int left = kou(s, pos);
while (1) {
char op = s[pos++];
if (op == '+') {
int right = kou(s, pos);
left += right;
}
else if (op == '-') {
int right = kou(s, pos);
left -= right;
}
else if (op == '=') {
return left;
}
else if (op == ')') {
return left;
}
}
}
int kou(string& s, int& pos) {
int left = num(s, pos);
while (1) {
char op = s[pos++];
if (op == '*') {
left *= num(s, pos);
}
else if (op == '/') {
left /= num(s, pos);
}
else {
--pos;
return left;
}
}
}
int num(string& s, int& pos) {
int ret = 0;
if (s[pos] == '(') {
++pos;
return expression(s, pos);
}
else {
while ('0' <= s[pos] && s[pos] <= '9') {
ret *= 10;
ret += s[pos] - '0';
++pos;
}
}
return ret;
}
int main() {
int n; cin >> n;
for (int i = 0; i < n; ++i) {
string s; cin >> s;
cout << cal(s) << endl;
}
} | ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
using namespace std;
int cal(string&);
int expression(string&, int&);
int kou(string&, int&);
int num(string&, int&);
int cal(string& s) {
int pos = 0;
return expression(s, pos);
}
int expression(string& s, int& pos) {
int left = kou(s, pos);
while (1) {
char op = s[pos++];
if (op == '+') {
int right = kou(s, pos);
left += right;
}
else if (op == '-') {
int right = kou(s, pos);
left -= right;
}
else if (op == '=') {
return left;
}
else if (op == ')') {
return left;
}
}
}
int kou(string& s, int& pos) {
int left = num(s, pos);
while (1) {
char op = s[pos++];
if (op == '*') {
left *= num(s, pos);
}
else if (op == '/') {
left /= num(s, pos);
}
else {
--pos;
return left;
}
}
}
int num(string& s, int& pos) {
int ret = 0;
if (s[pos] == '(') {
++pos;
return expression(s, pos);
}
else {
while ('0' <= s[pos] && s[pos] <= '9') {
ret *= 10;
ret += s[pos] - '0';
++pos;
}
}
return ret;
}
int main() {
int n; cin >> n;
for (int i = 0; i < n; ++i) {
string s; cin >> s;
cout << cal(s) << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef string::const_iterator State;
class ParseError {};
struct Parser {
int number(State &begin) {
int ret = 0;
while(isdigit(*begin)) {
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
int factor(State &begin) {
if(*begin == '(') {
begin++;
int ret = expression(begin);
begin++;
return ret;
} else {
return number(begin);
}
}
int term(State &begin) {
int ret = factor(begin);
while(1) {
if(*begin == '*') {
begin++;
ret *= factor(begin);
} else if(*begin == '/') {
begin++;
ret /= factor(begin);
} else {
break;
}
}
return ret;
}
int expression(State &begin) {
int ret = term(begin);
while(1) {
if(*begin == '+') {
begin++;
ret += term(begin);
} else if(*begin == '-') {
begin++;
ret -= term(begin);
} else {
break;
}
}
return ret;
}
};
int main() {
int N;
cin >> N;
for(int i = 0; i < N; i++) {
Parser ps;
string s;
cin >> s;
State begin = s.begin();
int ans = ps.expression(begin);
cout << ans << endl;
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef string::const_iterator State;
class ParseError {};
struct Parser {
int number(State &begin) {
int ret = 0;
while(isdigit(*begin)) {
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
int factor(State &begin) {
if(*begin == '(') {
begin++;
int ret = expression(begin);
begin++;
return ret;
} else {
return number(begin);
}
}
int term(State &begin) {
int ret = factor(begin);
while(1) {
if(*begin == '*') {
begin++;
ret *= factor(begin);
} else if(*begin == '/') {
begin++;
ret /= factor(begin);
} else {
break;
}
}
return ret;
}
int expression(State &begin) {
int ret = term(begin);
while(1) {
if(*begin == '+') {
begin++;
ret += term(begin);
} else if(*begin == '-') {
begin++;
ret -= term(begin);
} else {
break;
}
}
return ret;
}
};
int main() {
int N;
cin >> N;
for(int i = 0; i < N; i++) {
Parser ps;
string s;
cin >> s;
State begin = s.begin();
int ans = ps.expression(begin);
cout << ans << endl;
}
return 0;
}
``` |
#include <iostream>
#include <cctype>
#include <cassert>
using namespace std;
string S;
int cur;
int expression();
char readerchar()
{
assert(cur < S.size());
char ret = S[cur];
cur += 1;
return ret;
}
char peek()
{
assert(cur < S.size());
return S[cur];
}
int digit()
{
assert(isdigit(peek()));
int n = readerchar() - '0';
return n;
}
int number()
{
int n=digit();
while(cur<S.size() && isdigit(peek()))
n = n*10 + digit();
return n;
}
int factor()
{
if(peek()!='(')
return number();
cur += 1;
int n = expression();
assert(peek() == ')');
cur += 1;
return n;
}
int term()
{
int a = factor();
while(cur < S.size() && (peek()=='*' || peek()=='/'))
{
char op = readerchar();
int b = factor();
if(op == '*')
a *= b;
else a /= b;
}
return a;
}
int expression()
{
int a = term();
while(cur < S.size() && (peek()=='+' || peek()=='-'))
{
char op = readerchar();
int b = term();
if(op == '+')
a += b;
else
a -= b;
}
return a;
}
int main()
{
int n,i;
cin >> n;
for(i=0;i<n;i++)
{
cur = 0;
cin >> S;
S.resize(S.size()-1);
cout << expression() << endl;
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <cctype>
#include <cassert>
using namespace std;
string S;
int cur;
int expression();
char readerchar()
{
assert(cur < S.size());
char ret = S[cur];
cur += 1;
return ret;
}
char peek()
{
assert(cur < S.size());
return S[cur];
}
int digit()
{
assert(isdigit(peek()));
int n = readerchar() - '0';
return n;
}
int number()
{
int n=digit();
while(cur<S.size() && isdigit(peek()))
n = n*10 + digit();
return n;
}
int factor()
{
if(peek()!='(')
return number();
cur += 1;
int n = expression();
assert(peek() == ')');
cur += 1;
return n;
}
int term()
{
int a = factor();
while(cur < S.size() && (peek()=='*' || peek()=='/'))
{
char op = readerchar();
int b = factor();
if(op == '*')
a *= b;
else a /= b;
}
return a;
}
int expression()
{
int a = term();
while(cur < S.size() && (peek()=='+' || peek()=='-'))
{
char op = readerchar();
int b = term();
if(op == '+')
a += b;
else
a -= b;
}
return a;
}
int main()
{
int n,i;
cin >> n;
for(i=0;i<n;i++)
{
cur = 0;
cin >> S;
S.resize(S.size()-1);
cout << expression() << endl;
}
return 0;
}
``` |
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
typedef string::const_iterator Iter;
int express(Iter &p);
int term(Iter &p);
int factor(Iter &p);
int number(Iter &p);
int express(Iter &p) {
int r = term(p);
for(;;) {
if(*p == '+') {
p++;
int rs = term(p);
r += rs;
}
else if(*p == '-') {
p++;
int rs = term(p);
r -= rs;
}
else
break;
}
return r;
}
int term(Iter &p) {
int r = factor(p);
for(;;) {
if(*p == '*') {
p++;
int rs = factor(p);
r *= rs;
}
else if(*p == '/') {
p++;
int rs = factor(p);
r /= rs;
}
else
break;
}
return r;
}
int factor(Iter &p) {
if(*p == '(') {
p++;
int r = express(p);
p++; // skip )
return r;
}
else
return number(p);
}
int number(Iter &p) {
int r = 0;
while(isdigit(*p)) {
r *= 10;
r += (*p) - '0';
p++;
}
return r;
}
int main() {
int N; cin >> N;
cin.ignore();
while(N--) {
string line;
getline(cin, line);
Iter begin = line.begin();
int ans = express(begin);
cout << ans << endl;
}
} | ### Prompt
Your task is to create a CPP solution to the following problem:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
typedef string::const_iterator Iter;
int express(Iter &p);
int term(Iter &p);
int factor(Iter &p);
int number(Iter &p);
int express(Iter &p) {
int r = term(p);
for(;;) {
if(*p == '+') {
p++;
int rs = term(p);
r += rs;
}
else if(*p == '-') {
p++;
int rs = term(p);
r -= rs;
}
else
break;
}
return r;
}
int term(Iter &p) {
int r = factor(p);
for(;;) {
if(*p == '*') {
p++;
int rs = factor(p);
r *= rs;
}
else if(*p == '/') {
p++;
int rs = factor(p);
r /= rs;
}
else
break;
}
return r;
}
int factor(Iter &p) {
if(*p == '(') {
p++;
int r = express(p);
p++; // skip )
return r;
}
else
return number(p);
}
int number(Iter &p) {
int r = 0;
while(isdigit(*p)) {
r *= 10;
r += (*p) - '0';
p++;
}
return r;
}
int main() {
int N; cin >> N;
cin.ignore();
while(N--) {
string line;
getline(cin, line);
Iter begin = line.begin();
int ans = express(begin);
cout << ans << endl;
}
}
``` |
#include "bits/stdc++.h"
#define int long long
#define range(i, a, b) for(int i = a; i < b; i++)
#define rep(i, a) range(i, 0, a)
#define all(a) (a).begin(),(a).end()
using namespace std;
const int MOD = 1e9 + 7, INF = 1e17;
using vi = vector <int>;
using vvi = vector <vi>;
//g++ -std==c++14
int expr(const char **);
int term(const char **);
int factor(const char **);
int number(const char **);
int expr(const char **p) {
int val = term(p);
while (1) {
if (**p == '+') {
(*p)++;
val += term(p);
} else if (**p == '-') {
(*p)++;
val -= term(p);
} else {
break;
}
}
return val;
}
int term(const char **p) {
int val = factor(p);
while (1) {
if (**p == '*') {
(*p)++;
val *= factor(p);
} else if (**p == '/') {
(*p)++;
val /= factor(p);
} else {
break;
}
}
return val;
}
int factor(const char **p) {
if (**p == '(') {
(*p)++;
int val = expr(p);
(*p)++;
return val;
}
return number(p);
}
int number(const char **p) {
int val = 0;
while ('0' <= **p && **p <= '9') {
val = val * 10 + **p - '0';
(*p)++;
}
return val;
}
signed main() {
int n;
scanf("%d", &n);
char s[101];
while (n--) {
scanf("%s", s);
const char *p = s;
printf("%d\n", expr(&p));
}
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
Your task is to write a program which reads an expression and evaluates it.
* The expression consists of numerical values, operators and parentheses, and the ends with '='.
* The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.
* Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
* You may assume that there is no division by zero.
* All calculation is performed as integers, and after the decimal point should be truncated
* Length of the expression will not exceed 100.
* -1 × 109 ≤ intermediate results of computation ≤ 109
Input
The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.
Output
For each datasets, prints the result of calculation.
Example
Input
2
4-2*3=
4*(8+4+3)=
Output
-2
60
### Response
```cpp
#include "bits/stdc++.h"
#define int long long
#define range(i, a, b) for(int i = a; i < b; i++)
#define rep(i, a) range(i, 0, a)
#define all(a) (a).begin(),(a).end()
using namespace std;
const int MOD = 1e9 + 7, INF = 1e17;
using vi = vector <int>;
using vvi = vector <vi>;
//g++ -std==c++14
int expr(const char **);
int term(const char **);
int factor(const char **);
int number(const char **);
int expr(const char **p) {
int val = term(p);
while (1) {
if (**p == '+') {
(*p)++;
val += term(p);
} else if (**p == '-') {
(*p)++;
val -= term(p);
} else {
break;
}
}
return val;
}
int term(const char **p) {
int val = factor(p);
while (1) {
if (**p == '*') {
(*p)++;
val *= factor(p);
} else if (**p == '/') {
(*p)++;
val /= factor(p);
} else {
break;
}
}
return val;
}
int factor(const char **p) {
if (**p == '(') {
(*p)++;
int val = expr(p);
(*p)++;
return val;
}
return number(p);
}
int number(const char **p) {
int val = 0;
while ('0' <= **p && **p <= '9') {
val = val * 10 + **p - '0';
(*p)++;
}
return val;
}
signed main() {
int n;
scanf("%d", &n);
char s[101];
while (n--) {
scanf("%s", s);
const char *p = s;
printf("%d\n", expr(&p));
}
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.