text
stringlengths 291
465k
|
---|
### Prompt
Generate a Cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
string ULtoD(string s){
stringstream ss;
ss << (char)tolower(s[0]);
FOR(i, 1, s.length()){
if(isupper(s[i])) ss << '_' << (char)tolower(s[i]);
else ss << (char)tolower(s[i]);
}
return ss.str();
}
string DtoUL(string s, char t){
stringstream ss;
ss << (char)(t == 'U' ? toupper(s[0]) : s[0]);
FOR(i, 1, s.length()){
if(s[i] == '_') ss << (char)toupper(s[++i]);
else ss << s[i];
}
return ss.str();
}
int main() {
string s; char t;
while(cin >>s >>t && t != 'X'){
char bt;
if(isupper(s[0])) bt = 'U';
else if(s.find('_') != string::npos) bt = 'D';
else bt = 'L';
if(bt == 'U'){
if(t == 'L') s[0] = tolower(s[0]);
if(t == 'D') s = ULtoD(s);
} else if(bt == 'L'){
if(t == 'U') s[0] = toupper(s[0]);
if(t == 'D') s = ULtoD(s);
} else if(t != 'D') s = DtoUL(s, t);
cout <<s <<endl;
}
return 0;
}
``` |
### Prompt
Please formulate a cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;
string solve(string s, char c){
if('A' <= s[0] && s[0] <= 'Z')
s[0] += 'a' - 'A';
s += '_';
int n = s.size();
int j = 0;
vector<string> vs;
for(int i=0; i<n; ++i){
if('A' <= s[i] && s[i] <= 'Z'){
s[i] += 'a' - 'A';
vs.push_back(s.substr(j, i-j));
j = i;
}else if(s[i] == '_'){
vs.push_back(s.substr(j, i-j));
j = i+1;
}
}
int m = vs.size();
if(c == 'U')
vs[0][0] += 'A' - 'a';
s = vs[0];
for(int i=1; i<m; ++i){
if(c == 'D'){
s += '_' + vs[i];
}else{
vs[i][0] += 'A' - 'a';
s += vs[i];
}
}
return s;
}
int main()
{
for(;;){
string s;
char c;
cin >> s >> c;
if(s == "EndOfInput")
return 0;
cout << solve(s, c) << endl;
}
}
``` |
### Prompt
Your challenge is to write a JAVA solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* Problem P04: CamelCase
* Java 7 later.
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
String[] words;
while ((line = br.readLine()) != null && !line.isEmpty()) {
String name = line.substring(0, line.indexOf(' '));
String type = line.substring(line.indexOf(' ') + 1);
if (type.equals("X")) break;
ArrayList<StringBuilder> tokens = new ArrayList<>();
tokens.add(new StringBuilder());
for (char c : name.toCharArray()) {
if (c == '_') {
tokens.add(new StringBuilder());
continue;
} else if ('A' <= c && c <= 'Z') {
if (tokens.get(0).length() != 0) {
tokens.add(new StringBuilder());
}
c += 32;
}
tokens.get(tokens.size() - 1).append(c);
}
StringBuilder out = new StringBuilder();
switch (type) {
case "U":
for (int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i).toString();
token = (char) (token.charAt(0) - 32) + token.substring(1);
out.append(token);
}
break;
case "L":
for (int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i).toString();
if (i > 0) {
token = (char) (token.charAt(0) - 32) + token.substring(1);
}
out.append(token);
}
break;
case "D":
for (int i = 0; i < tokens.size(); i++) {
if (i > 0) out.append("_");
out.append(tokens.get(i).toString());
}
break;
}
System.out.println(out.toString());
} //end while
} //end main
}
``` |
### Prompt
Please create a solution in JAVA to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
final String input = sc.next();
final String type = sc.next();
if("X".equals(type)){
break;
}
char[][] units;
if(input.indexOf("_") >= 0){
String[] ret = input.split("_");
units = new char[ret.length][];
for(int i = 0; i < ret.length; i++){
units[i] = ret[i].toCharArray();
}
}else{
char[] array = input.toCharArray();
int count = 0;
for(int i = 1; i < array.length; i++){
if('A' <= array[i] && array[i] <= 'Z'){
count++;
}
}
count++;
units = new char[count][];
int start = 0;
int cur = 0;
for(int i = 1; i < array.length; i++){
if('A' <= array[i] && array[i] <= 'Z'){
final int size = i - start;
units[cur] = new char[size];
for(int j = 0; j < size; j++){
if(j == 0){
if('A' <= array[start + j] && array[start + j] <= 'Z'){
units[cur][j] = (char) (array[start + j] - ('A' - 'a'));
}else{
units[cur][j] = array[start + j];
}
}else{
units[cur][j] = array[start + j];
}
}
start = i;
cur++;
}
}
final int size = input.length() - start;
units[cur] = new char[size];
for(int j = 0; j < size; j++){
if(j == 0){
if('A' <= array[start + j] && array[start + j] <= 'Z'){
units[cur][j] = (char) (array[start + j] - ('A' - 'a'));
}else{
units[cur][j] = array[start + j];
}
}else{
units[cur][j] = array[start + j];
}
}
}
/*
for(char[] ch : units){
System.out.println(Arrays.toString(ch));
}
*/
if("L".equals(type)){
for(int i = 0; i < units.length; i++){
for(int j = 0; j < units[i].length; j++){
System.out.print((char)(units[i][j] + (j == 0 && i != 0 ? 'A' - 'a' : 0)));
}
}
System.out.println();
}else if("U".equals(type)){
for(int i = 0; i < units.length; i++){
for(int j = 0; j < units[i].length; j++){
System.out.print((char)(units[i][j] + (j == 0 ? 'A' - 'a' : 0)));
}
}
System.out.println();
}else if("D".equals(type)){
for(int i = 0; i < units.length; i++){
for(int j = 0; j < units[i].length; j++){
System.out.print(units[i][j]);
}
if(i != units.length - 1){
System.out.print("_");
}
}
System.out.println();
}
}
}
}
``` |
### Prompt
Please create a solution in cpp to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int i;
char c;
string s;
while(cin>>s>>c&&c!='X'){
if(s.find('_')!=string::npos){
if(c=='U'){
s[0]+='A'-'a';
while(s.find('_')!=string::npos){
s[s.find('_')+1]+='A'-'a';
s.erase(s.find('_'),1);
}
}else if(c=='L'){
while(s.find('_')!=string::npos){
s[s.find('_')+1]+='A'-'a';
s.erase(s.find('_'),1);
}
}
}else if('a'<=s[0]&&s[0]<='z'){
if(c=='U'){
s[0]+='A'-'a';
}else if(c=='D'){
for(i=0;i<(int)s.length();i++){
for(;i<(int)s.length()&&'a'<=s[i]&&s[i]<='z';i++);
if(i!=(int)s.length()){
s[i]+='a'-'A';
s.insert(i,1,'_');
}
}
}
}else if('A'<=s[0]&&s[0]<='Z'){
if(c=='L'){
s[0]+='a'-'A';
}else if(c=='D'){
s[0]+='a'-'A';
for(i=0;i<(int)s.length();i++){
for(;i<(int)s.length()&&'a'<=s[i]&&s[i]<='z';i++);
if(i!=(int)s.length()){
s[i]+='a'-'A';
s.insert(i,1,'_');
}
}
}
}
cout<<s<<endl;
}
return 0;
}
``` |
### Prompt
Please provide a Cpp coded solution to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int main() {
string s;
char get_type, to_type;
while (cin >> s >> to_type) {
if (to_type == 'X')return 0;
if ((int)s[0] < 95)get_type = 'U';
else {
get_type = 'L';
for (char i : s)if (i == '_')get_type = 'D';
}
if (get_type == to_type)cout << s << endl;
else {
for (int i = 0; i < s.size(); i++) {
if (i == 0) {
if (to_type == 'U')cout << (char)(s[0] - 32);
else if (get_type != 'U')cout << s[0];
else cout << (char)(s[0] + 32);
}
else if (s[i] == '_') {
i++;
cout << (char)(s[i] - 32);
}
else if (s[i] < 95 && to_type == 'D')cout << '_' << (char)(s[i] + 32);
else cout << s[i];
}
cout << endl;
}
}
}
``` |
### Prompt
In cpp, your task is to solve the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
using namespace std;
vector<string> split(string s) {
vector<string> ret;
string buf;
if (isupper(s[0])) {
s[0] = tolower(s[0]);
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '_') {
ret.push_back(buf);
buf.clear();
} else if (isupper(s[i])) {
ret.push_back(buf);
buf.clear();
buf.push_back(tolower(s[i]));
} else {
buf.push_back(s[i]);
}
}
ret.push_back(buf);
return ret;
}
int main() {
string s; char type;
while (cin >> s >> type) {
if (type == 'X') break;
vector<string> words = split(s);
string output;
switch (type) {
case 'L':
for (int i = 1; i < words.size(); i++) {
words[i][0] = toupper(words[i][0]);
}
for (int i = 0; i < words.size(); i++) {
output += words[i];
}
break;
case 'U':
for (int i = 0; i < words.size(); i++) {
words[i][0] = toupper(words[i][0]);
output += words[i];
}
break;
case 'D':
for (int i = 0; i < words.size()-1; i++) {
words[i].push_back('_');
}
for (int i = 0; i < words.size(); i++) {
output += words[i];
}
break;
}
cout << output << endl;
}
return 0;
}
``` |
### Prompt
Generate a java solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.util.Scanner;
public class Main{
static String change(String s, String type, int num) {
String ans="";
if(type.equals("L")) {
if(num==0) {
ans = s.toLowerCase();
}
else {
ans=String.valueOf(s.charAt(0)).toUpperCase();
for(int i=1; i<s.length(); i++) {
ans+=String.valueOf(s.charAt(i)).toLowerCase();
}
}
}
else if(type.equals("U")) {
ans=String.valueOf(s.charAt(0)).toUpperCase();
for(int i=1; i<s.length(); i++) {
ans+=String.valueOf(s.charAt(i)).toLowerCase();
}
}
else {
if(num==0) {
ans = s.toLowerCase();
}
else {
ans = "_"+s.toLowerCase();
}
}
return ans;
}
public static void main(String[] args) {
try(Scanner sc = new Scanner(System.in)) {
while(sc.hasNext()) {
String str=sc.next();
String type=sc.next();
if(type.equals("X")) break;
int num=0;
String ans="";
if(str.contains("_")) {
String[] tokens=str.split("_");
num=0;
for(String t:tokens) {
ans+=change(t, type, num);
num++;
}
}
else {
int start=0,last=0;
num=0;
for(int i=0; i<str.length(); i++) {
if(Character.isUpperCase(str.charAt(i)) && last==i) {
int index=str.length()-1;
for(int j=i+1; j<str.length(); j++) {
if(Character.isUpperCase(str.charAt(j))) {
index=j;
last=index;
ans+=change(str.substring(start, index), type, num);
num++;
start=index;
break;
}
}
}
else if(Character.isUpperCase(str.charAt(i))&& start!=i) {
last=i;
ans+=change(str.substring(start, i), type, num);
if(i!=0)num++;
start=i;
}
}
if(last<str.length()) {
ans+=change(str.substring(last, str.length()), type, num);
}
}
System.out.println(ans);
}
}
}
}
``` |
### Prompt
Generate a Cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
//split delimiter??§?????????
vector<string> split(const string &input, char delimiter) {
vector<string> ret;
istringstream stream(input);
string s;
while (getline(stream, s, delimiter)) ret.push_back(s);
return ret;
}
string stoupper(string s) { transform(s.begin(), s.end(), s.begin(), ::toupper); return s; }
string stolower(string s) { transform(s.begin(), s.end(), s.begin(), ::tolower); return s; }
signed main() {
for (string s, t; cin >> s >> t&&t != "X";) {
vector<string> a;
if (s.find("_") != string::npos) {
a = split(s, '_');
}
else {
int l = 0;
rep(i, 1, s.size()) {
if (isupper(s[i])) {
a.push_back(stolower(s.substr(l, i - l)));
l = i;
}
}
a.push_back(stolower(s.substr(l, s.size())));
}
string ans = "";
if (t == "L") {
ans += a[0];
rep(i, 1, a.size()) {
a[i][0] = toupper(a[i][0]);
ans += a[i];
}
}
else if (t == "U") {
rep(i, 0, a.size()) {
a[i][0] = toupper(a[i][0]);
ans += a[i];
}
}
else {
ans += a[0];
rep(i, 1, a.size()) {
ans += "_" + a[i];
}
}
cout << ans << endl;
}
return 0;
}
``` |
### Prompt
Your challenge is to write a cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstdlib>
using namespace std;
int main() {
string str; char op;
while(cin>>str>>op) {
if(op == 'X') break;
for(int i=0; i<(int)str.size(); i++) {
if(str[i] == '_') { str[i] = ' '; }
else if(isupper(str[i])) {
char ch = tolower(str[i]);
str = str.substr(0, i) + " " + ch + str.substr(i+1);
}
else { str[i] = tolower(str[i]); }
}
stringstream ss(str);
string ans;
bool fsted = 0;
while(ss >> str) {
if(op == 'L') {
if(fsted) { ans+=toupper(str[0]); ans+=str.substr(1); }
else ans+=str;
fsted = 1;
}
if(op == 'U') {
ans+=toupper(str[0]); ans+=str.substr(1);
}
if(op == 'D') {
if(fsted) ans+='_';
ans+=str;
fsted = 1;
}
}
cout << ans << endl;
}
return 0;
}
``` |
### Prompt
Develop a solution in python3 to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```python3
while True:
n, t = input().split()
if t == "X":
break
if "_" in n:
names = n.split("_")
else:
n = list(n)
for i, c in enumerate(n):
if c.isupper():
if i:
n[i] = "#" + c.lower()
else:
n[i] = c.lower()
names = "".join(n).split("#")
if t == "U":
print(*[name.title() for name in names], sep="")
elif t == "L":
print(*[name.title() if i != 0 else name for i, name in enumerate(names)], sep="")
else:
print(*names, sep="_")
``` |
### Prompt
Your challenge is to write a Python3 solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```python3
def Upper(s) :
New = []
i = 0
while True :
if i == 0 :
New.append(s[i].upper())
elif s[i] == "_" :
i += 1
New.append(s[i].upper())
elif 65 <= ord(s[i]) <= 90 :
New.append(s[i].upper())
else :
New.append(s[i].lower())
i += 1
if i >= len(s) :
break
return New
def Lower(s) :
New = []
i = 0
while True :
if s[i] == "_" :
i += 1
New.append(s[i].upper())
elif 65 <= ord(s[i]) <= 90 and i != 0:
New.append(s[i].upper())
else :
New.append(s[i].lower())
i += 1
if i >= len(s) :
break
return New
def Under_Score(s) :
New = []
i = 0
while True :
if 65 <= ord(s[i]) <= 90 and i != 0:
New.append("_")
New.append(s[i].lower())
else :
New.append(s[i].lower())
i += 1
if i >= len(s) :
break
return New
while True :
l, x = map(str, input().split())
if x == "X" :
break
elif x == "U" :
print(*Upper(l), sep="")
elif x == "L" :
print(*Lower(l), sep="")
elif x == "D" :
print(*Under_Score(l), sep="")
``` |
### Prompt
Please formulate a CPP solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<stack>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii;
typedef long long ll ;
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int main(void){
string buf;
char com;
while(cin>>buf>>com,com!='X'){
string ans = "";
switch(com){
case 'L' :
ans = tolower(buf[0]);
loop(i,1,buf.size()){
if(buf[i] == '_'){
ans += toupper(buf[++i]);
}else{
ans += buf[i];
}
}
break;
case 'U' :
ans = toupper(buf[0]);
loop(i,1,buf.size()){
if(buf[i] == '_'){
ans += toupper(buf[++i]);
}else{
ans += buf[i];
}
}
break;
case 'D' :
ans = tolower(buf[0]);
loop(i,1,buf.size()){
if('A'<=buf[i] && buf[i]<='Z'){
ans += '_';
ans += tolower(buf[i]);
}else{
ans += buf[i];
}
}
}
cout<<ans<<endl;
}
}
``` |
### Prompt
Please create a solution in PYTHON3 to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```python3
import re
while True:
n,t=map(str,input().split())
if t == 'X':
break
if '_' in n :
if t == 'D':
print(n)
else:
m = n.title()
l= re.sub('_', '',m)
if t == 'U':
print(l)
elif t == 'L':
p = l[0].lower() + l[1:]
print(p)
else:
q = n[0].upper() + n[1:]
r = re.findall('[A-Z][a-z]*',q)
if t == 'D':
s = '_'.join(r)
print(s.lower())
elif t == 'U':
print("".join(r))
elif t == 'L':
u = "".join(r)
v = u[0].lower() + u[1:]
print(v)
``` |
### Prompt
Your task is to create a Cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
string s;
char c;
while (true) {
cin >> s >> c;
if (c == 'U') {
if (s.find('_', 0) == string::npos &&
'a' <= s[0] && s[0] <= 'z')
s[0] += ('A'-'a');
else
for (int i = 0; i < s.size(); i++)
if (s[i] == '_') {
s[i+1] += ('A'-'a');
s.erase(i, 1);
} else if (i == 0 && 'a' <= s[0] && s[0] <= 'z') {
s[i] += ('A'-'a');
}
cout << s << endl;
} else if (c == 'L') {
if (s.find('_', 0) == string::npos &&
'A' <= s[0] && s[0] <= 'Z')
s[0] -= ('A'-'a');
else
for (int i = 0; i < s.size(); i++)
if (s[i] == '_') {
s[i+1] += ('A'-'a');
s.erase(i, 1);
}
cout << s << endl;
} else if (c == 'D') {
for (int i = 0; i < s.size(); i++)
if (i == 0 && 'A' <= s[i] && s[i] <= 'Z') {
s[i] -= ('A'-'a');
} else if ('A' <= s[i] && s[i] <= 'Z') {
s[i] -= ('A'-'a');
s.insert(i, 1, '_');
}
cout << s << endl;
} else {
break;
}
}
return 0;
}
``` |
### Prompt
In PYTHON3, your task is to solve the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```python3
large = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
small = list("abcdefghijklmnopqrstuvwxyz")
while 1:
word, alpha = input().split()
if alpha == "X":
break
word = list(word)
ans = ""
if alpha == "L":
first = word.pop(0)
if first in large:
ans += small[large.index(first)]
else:
ans += first
while word != []:
w = word.pop(0)
if w == "_":
target = word.pop(0)
if target in small:
ans += large[small.index(target)]
else:
ans += target
else:
ans += w
elif alpha == "U":
first = word.pop(0)
if first in small:
ans += large[small.index(first)]
else:
ans += first
while word != []:
w = word.pop(0)
if w == "_":
target = word.pop(0)
if target in small:
ans += large[small.index(target)]
else:
ans += target
else:
ans += w
elif alpha == "D":
first = word.pop(0)
if first in large:
ans += small[large.index(first)]
else:
ans += first
while word != []:
w = word.pop(0)
if w in large:
ans += "_" + small[large.index(w)]
else:
ans += w
print(ans)
``` |
### Prompt
Please formulate a cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<string>
#include<vector>
#include<iostream>
using namespace std;
int main(){
string s;
char c;
while(cin>>s>>c,c!='X'){
int now=1,pre=0;
vector<string> box;
while(1){
if(now==s.size()){
box.push_back(s.substr(pre,now-pre));
break;
}
if(isupper(s[now])){
box.push_back(s.substr(pre,now-pre));
pre=now;
}else if(s[now]=='_'){
box.push_back(s.substr(pre,now-pre));
now++;
pre=now;
}
now++;
}
for(int i=0;i<box.size();i++){
string t=box[i];
char d=t[0];
if(c=='L'){
if(i==0&&isupper(d)){
t[0]=tolower(d);
}
if(i!=0&&islower(d)){
t[0]=toupper(d);
}
}else if(c=='U'){
if(islower(d))
t[0]=toupper(d);
}else if(c=='D'){
if(isupper(d))
t[0]=tolower(d);
if(i!=0)
cout<<'_';
}
cout<<t;
}
cout<<endl;
// for(int i=0;i<box.size();i++)
// cout<<box[i]<<endl;
}
return 0;
}
``` |
### Prompt
Create a solution in Cpp for the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
using namespace std;
int dif='a'-'A';
void outU( string &s ){
char c;
for(unsigned int i=0;i<s.size();i++ ){
c=s.at( i );
if( i==0 ) c-= dif;
cout << c;
}
}
int main(){
string name, type;
string names[100];
int nw;
while( true ){
cin >> name >> type;
// cout << name << "," << type << endl;
if( type=="X" ) break;
for( int i=0;i<100;i++ )names[i]="";
nw=0;
char c;
for( unsigned int i=0; i<name.size();i++ ){
c = name.at(i);
if( c== '_' ) nw++;
else if( c<='Z'){
c+= dif;
if( names[nw].size()!=0 ){
nw++; names[nw]="";
}
names[nw] += c;
}else
names[nw] += c;
}
if( c!='_' )
nw++;
// cout << "\ndiv "<<name<<endl;
// for( int i=0;i<nw;i++ ) cout << names[i] << endl;
if( type=="D" ){
int i;
for( i=0;i<nw-1;i++ )
cout << names[i] << "_" ;
cout << names[i] << endl;
}else{
if( type=="U" ) // テつ湘つ嘉つづδ淌つづδ古つ陛つカテつ偲つ堙つ療δアテつづδ催つ陛δ湘つ甘つキ
outU( names[0] );
else
cout << names[0];
for( int i=1;i<nw;i++ )
outU( names[i] );
cout << endl;
}
}
return 0;
}
``` |
### Prompt
Create a solution in cpp for the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <cstdio>
#include <cstring>
#include <cctype>
int main(){
while(true){
char str[101], cmd[2];
scanf("%100s %1s", str, cmd);
if(cmd[0] == 'X'){ break; }
bool isFirst = true, isPause = true;
for(char *p = str; *p != '\0'; p++){
if(!isFirst){
if(!(*p & 0x20)){ isPause = true; }
if(*p == '_'){
isPause = true;
if(*++p == '\0'){ break; }
}
}
switch(cmd[0]){
case 'U':
if(isPause){
putc(toupper(*p), stdout);
}else{
putc(tolower(*p), stdout);
}
break;
case 'L':
if(isPause && !isFirst){
putc(toupper(*p), stdout);
}else{
putc(tolower(*p), stdout);
}
break;
case 'D':
if(isPause && !isFirst){
putc('_', stdout); putc(tolower(*p), stdout);
}else{
putc(tolower(*p), stdout);
}
}
isPause = false;
isFirst = false;
}
puts("");
}
return 0;
}
``` |
### Prompt
Please create a solution in Cpp to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cctype>
using namespace std;
int main(){
string name;
char type,pre_type;
while(true){
cin >> name >> type;
if(type == 'X')break;
for(int i=0;i<name.size();i++){
if(type == 'U'){
if(i == 0){
cout << (char)toupper(name[i]);
continue;
}
if(name[i] == '_'){
name[i+1] = toupper(name[i+1]);
continue;
}
cout << name[i];
}
else if(type == 'L'){
if(i == 0){
cout << (char)tolower(name[i]);
continue;
}
if(name[i] == '_'){
name[i+1] = (char)toupper(name[i+1]);
continue;
}
cout << name[i];
}
else if(type == 'D'){
if(i == 0){
cout << (char)tolower(name[i]);
continue;
}
if(isupper(name[i])){
cout << "_" << (char)tolower(name[i]);
continue;
}
cout << name[i];
}
}
cout << endl;
}
}
``` |
### Prompt
Please provide a CPP coded solution to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif
#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define rep(i, n) for (int i = 0; i < n; ++i)
template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }
const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;
int main()
{
while (true)
{
string s;
char to;
cin >> s >> to;
if (to == 'X')
break;
vector<string> words;
s += '_';
while (!s.empty())
{
s[0] = tolower(s[0]);
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == '_' || isupper(s[i]))
{
words.push_back(s.substr(0, i));
words.back()[0] = words.back()[0];
s.erase(0, i + (s[i] == '_' ? 1 : 0));
break;
}
}
}
string res;
if (to == 'U' || to == 'L')
{
if (to == 'U')
words[0][0] = toupper(words[0][0]);
res += words[0];
for (int i = 1; i < words.size(); ++i)
{
words[i][0] = toupper(words[i][0]);
res += words[i];
}
}
else
for (int i = 0; i < words.size(); ++i)
res += words[i] + (i + 1 != words.size() ? "_" : "");
cout << res << endl;
}
}
``` |
### Prompt
Generate a JAVA solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.util.*;
class Main{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
String str,type;
while(true){
str=s.next();
type=s.next();
if(type.charAt(0)=='X')break;
String ret="";
String[]table=str.split("_");
switch(type.charAt(0)){
case 'U':
if(table.length==1){
ret=(char)(str.charAt(0)&(~32))+str.substring(1);
}else{
for(int i=0;i<table.length;i++){
ret+=(char)(table[i].charAt(0)&(~32))+table[i].substring(1);
}
}
break;
case 'L':
if(table.length==1){
ret=(char)(str.charAt(0)|(32))+str.substring(1);
}else{
ret+=table[0];
for(int i=1;i<table.length;i++){
ret+=(char)(table[i].charAt(0)&(~32))+table[i].substring(1);
}
}
break;
case 'D':
if(table.length!=1)ret=str;
else for(int i=0;i<str.length();i++){
if(i>0&&str.charAt(i)>='A'&&str.charAt(i)<='Z')ret+="_"+(char)(str.charAt(i)|32);
else ret+=(char)(str.charAt(i)|32);
}
break;
}
System.out.println(ret);
}
}
}
``` |
### Prompt
Construct a Cpp code solution to the problem outlined:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> parse(string s){
if('A' <= s[0] && s[0] <= 'Z') s[0] -= 'A'-'a';
vector<string> ret;
int t = 0;
while(s.size()){
if('A' <= s[t] && s[t] <= 'Z' || s[t] == '_'){
if(s[t] == '_'){ret.push_back(s.substr(0, t)); s = s.substr(t+1);t=0;}
else {ret.push_back(s.substr(0, t)); s = s.substr(t); s[0] -= 'A'-'a'; t=0;}
}
else if(t==s.size()){ret.push_back(s); break;}
else t++;
}
return ret;
}
int main(){
string s, t;
while(cin >> s >> t, t != "X"){
vector<string> v = parse(s);
for(int i=0; i< v.size(); i++){
if(t=="L"){
if(i!=0) v[i][0]+='A'-'a';
cout << v[i];
}
else if(t=="U"){
v[i][0]+='A'-'a';
cout << v[i];
}
else {
cout << (i==0?"":"_") << v[i];
}
}
cout << endl;
}
}
``` |
### Prompt
In CPP, your task is to solve the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cctype>
#include <iterator>
using namespace std;
vector<string> parseL(string s) {
const int L = s.length();
int i = 0, j = 0;
vector<string> res;
while(true) {
if(L <= j) break;
j++;
while(j < L && islower(s[j])) j++;
res.push_back(s.substr(i, j - i));
i = j;
}
return res;
}
vector<string> parseU(string s) {
s[0] = tolower(s[0]);
vector<string> res = parseL(s);
res[0][0] = toupper(res[0][0]);
return res;
}
vector<string> parseD(string s) {
const int L = s.length();
int i = 0;
vector<string> res;
while(true) {
string t;
while(i < L && s[i] != '_') {
t.push_back(s[i]);
i++;
}
res.push_back(t);
if(i == L) break;
i++;
}
return res;
}
void tr(vector<string> vec) {
copy(vec.begin(), vec.end(), ostream_iterator<string>(cerr, "_"));
}
int main() {
for(string text, type; cin >> text >> type, type != "X"; ) {
vector<string> vec;
if(find(text.begin(), text.end(), '_') != text.end()) {
vec = parseD(text);
}
else if(isupper(text[0])) {
vec = parseU(text);
}
else {
vec = parseL(text);
}
if(type == "L") {
for(int j = 0; j < (int)vec[0].size(); j++) cout << (char)tolower(vec[0][j]);
for(int i = 1; i < (int)vec.size(); i++) {
cout << (char)toupper(vec[i][0]);
for(int j = 1; j < (int)vec[i].size(); j++) {
cout << (char)tolower(vec[i][j]);
}
}
}
else if(type == "U") {
for(int i = 0; i < (int)vec.size(); i++) {
cout << (char)toupper(vec[i][0]);
for(int j = 1; j < (int)vec[i].size(); j++) {
cout << (char)tolower(vec[i][j]);
}
}
}
else {
for(int j = 0; j < (int)vec[0].size(); j++) cout << (char)tolower(vec[0][j]);
for(int i = 1; i < (int)vec.size(); i++) {
cout << '_';
for(int j = 0; j < (int)vec[i].size(); j++) {
cout << (char)tolower(vec[i][j]);
}
}
}
cout << endl;
}
}
``` |
### Prompt
Please provide a Cpp coded solution to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
using namespace std;
string check(string str) {
string str2;
if('A'<=str[0] && str[0] <='Z') {
str2 += str[0] +32;
for(int i=1;i<str.size();i++) {
if('A' <=str[i] && str[i] <= 'Z') {
str2 += '_';
str2 += str[i] + 32;
}
else str2 += str[i];
}
return str2;
}
for(int i=0;i<str.size();i++)
if(str[i] == '_') return str;
for(int i=0;i<str.size();i++) {
if('A'<=str[i] && str[i] <='Z') {
str2 += '_';
str2 += str[i] + 32;
}
else str2 += str[i];
}
return str2;
}
int main() {
while(1) {
string str;
cin >> str;
string ch;
cin >> ch;
if(ch == "X") break;
str= check(str);
// cout << str << endl;
if(ch == "D") cout << str << endl;
else if(ch == "L") {
cout << str[0];
for(int i=1;i<str.size();i++) {
if(str[i] == '_') {
i++;
str[i] -= 32;
cout << str[i];
}
else cout << str[i];
}
cout << endl;
}
else if(ch == "U"){
str[0] -= 32;
cout << str[0];
for(int i=1;i<str.size();i++) {
if(str[i] == '_') {
i++;
str[i] -= 32;
cout << str[i];
}
else {
cout << str[i];
}
}
cout << endl;
}
}
return 0;
}
``` |
### Prompt
Please formulate a Cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair
static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;
template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T &t = T()){
v.assign(a,vector<T>(b,t));
}
//v.erase(unique(v.begin(),v.end()),v.end());
bool foo1(char t){
if('A'<=t&&t<='Z') return true;
return false;
}
int main(){
string s;
char t;
while(cin>>s>>t,t!='X'){
vector<string> vs;
int x=0;
rep(i,s.size()){
if(i==0) continue;
if(foo1(s[i])){
vs.PB(s.substr(x,i-x));
x=i;
}
else if(s[i]=='_'){
vs.PB(s.substr(x,i-x));
x=i+1;
}
}
vs.PB(s.substr(x));
rep(i,vs.size()){
rep(j,vs[i].size()){
if(foo1(vs[i][j])){
vs[i][j]+='a'-'A';
}
}
}
if(t=='U'){
rep(i,vs.size()){
vs[i][0]+='A'-'a';
cout<<vs[i];
}
// cout<<endl;
}
else if(t=='L'){
rep(i,vs.size()){
if(i) vs[i][0]+='A'-'a';
cout<<vs[i];
}
}
else if(t=='D'){
rep(i,vs.size()){
if(i){
cout<<"_";
}
cout<<vs[i];
}
}
cout<<endl;
}
}
``` |
### Prompt
Please provide a CPP coded solution to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int i, j;
vector <string> word(string str);
int main(void)
{
string str;
char type;
while (cin >> str >> type, type != 'X'){
int flag = 0;
vector <string> vs = word(str);
if (type == 'L'){
cout << vs[0];
for (i = 1; i < vs.size(); i++){
vs[i][0] ^= 32;
cout << vs[i];
}
puts("");
}
if (type == 'U'){
for (i = 0; i < vs.size(); i++){
vs[i][0] ^= 32;
cout << vs[i];
}
puts("");
}
if (type == 'D'){
for (i = 0; i < vs.size(); i++){
cout << vs[i];
if (i + 1 != vs.size()) cout << "_";
}
puts("");
}
}
return 0;
}
vector <string> word(string str)
{
vector <string> vs;
bool flag = false;
for (i = 0; i < str.size(); i++){
if (str[i] == '_') {
flag = true;
break;
}
}
string tmp;
if (flag){
for (i = 0; i < str.size(); i++){
if (str[i] != '_'){
tmp += (str[i] |= 32);
}
else {
vs.push_back(tmp);
tmp.clear();
}
}
vs.push_back(tmp);
return vs;
}
if ('a' <= str[0] && str[0] <= 'z'){
for (i = 0; i < str.size(); i++){
if (!('A' <= str[i] && str[i] <= 'Z')){
tmp += (str[i] |= 32);
}
else {
vs.push_back(tmp);
tmp.clear();
break;
}
}
for (; i < str.size(); i++){
tmp += (str[i] |= 32);
if (('A' <= str[i + 1] && str[i + 1] <= 'Z') && i + 1 < str.size()){
vs.push_back(tmp);
tmp.clear();
}
}
vs.push_back(tmp);
return vs;
}
if ('A' <= str[0] && str[0] <= 'Z'){
for (i = 0; i < str.size(); i++){
tmp += (str[i] |= 32);
if (('A' <= str[i + 1] && str[i + 1] <= 'Z') && i + 1 < str.size()){
vs.push_back(tmp);
tmp.clear();
}
}
vs.push_back(tmp);
return vs;
}
return vs;
}
``` |
### Prompt
Construct a CPP code solution to the problem outlined:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(void){
string str;
char c;
while(cin >> str >> c){
int t;
vector<string> v;
string buf = "";
bool isOneWord = true;
if(c == 'X')break;
if('A' <= str[0] && str[0] <= 'Z'){
//upper camel case
for(int j=0;j<str.size();j++){
if('A' <= str[j] && str[j] <= 'Z'){
if(j!=0)v.push_back(buf);
buf = "";
buf += str[j] - 'A' + 'a';
}else{
buf += str[j];
}
}
v.push_back(buf);
}else{
for(int i=0;i<str.size();i++){
if(str[i] == '_'){
//snake case
for(int j=0;j<str.size();j++){
if(str[j] == '_'){
v.push_back(buf);
buf = "";
}else{
buf += str[j];
}
}
v.push_back(buf);
isOneWord = false;
break;
}else if('A' <= str[i] && str[i] <= 'Z'){
//lower cames case
for(int j=0;j<str.size();j++){
if('A' <= str[j] && str[j] <= 'Z'){
if(j!=0)v.push_back(buf);
buf = "";
buf += str[j] - 'A' + 'a';
}else{
buf += str[j];
}
}
v.push_back(buf);
isOneWord = false;
break;
}
}
if(isOneWord){
v.push_back(str);
}
}
//convert
if(c == 'L'){
for(int i=0;i<v.size();i++){
if(i==0){
cout << v[i];
}else{
v[i][0] -= 'a' - 'A';
cout << v[i];
}
}
}else if(c == 'U'){
for(int i=0;i<v.size();i++){
v[i][0] -= 'a' - 'A';
cout << v[i];
}
}else{
for(int i=0;i<v.size();i++){
cout << v[i];
if(i != v.size()-1) cout << "_";
}
}
cout << endl;
}
}
``` |
### Prompt
Please create a solution in cpp to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
char S[101],s;
while(1){
scanf("%s %c",S,&s);
if(s=='X') break;
else if(s=='L' || s=='U'){
for(int i=0,l=strlen(S);i<l;i++){
if(s=='L'&&i==0&&S[i]>='A'&&S[i]<='Z'){
printf("%c",S[i]-('A'-'a'));
}else if(S[i]=='_' ||(s=='U'&&i==0&&S[i]>='a'&&S[i]<='z')){
if(i!=0)i++;
printf("%c",S[i]+ ('A'-'a'));
}else printf("%c",S[i]);
}
}else if(s=='D'){
if(S[0]>='A'&&S[0]<='Z')printf("%c",S[0]-('A'-'a'));
else printf("%c",S[0]);
for(int i=1,l=strlen(S);i<l;i++){
if(S[i]>='A'&&S[i]<='Z'){
printf("_%c",S[i]-('A'-'a'));
}else{
printf("%c",S[i]);
}
}
}
puts("");
}
}
``` |
### Prompt
Please formulate a Cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<algorithm>
#include<vector>
#include<cctype>
using namespace std;
vector<string> split(string s){
vector<string> ret;
string tmp = s.substr(0,1);
tmp[0] = tolower(tmp[0]);
for(int i = 1; i < s.length(); i++){
if(s[i] == '_'){
ret.push_back(tmp);
tmp = "";
}else if(isupper(s[i])){
ret.push_back(tmp);
tmp = s.substr(i, 1);
tmp[0] = tolower(tmp[0]);
}else{
tmp += s.substr(i,1);
}
}
if(tmp != "") ret.push_back(tmp);
return ret;
}
int main(){
string s;
char op;
while(cin >> s >> op){
if(op == 'X') break;
vector<string> v = split(s);
int n = v.size();
if(op == 'L'){
cout << v[0];
for(int i = 1; i < n; i++){
v[i][0] = toupper(v[i][0]);
cout << v[i];
}
cout << endl;
}else if(op == 'U'){
for(int i = 0; i < n; i++){
v[i][0] = toupper(v[i][0]);
cout << v[i];
}
cout << endl;
}else if(op == 'D'){
for(int i = 0; i < n; i++){
cout << v[i] << "_\n"[i == n-1];
}
}
}
return 0;
}
``` |
### Prompt
Please create a solution in CPP to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<string>
using namespace std;
string S; char C;
int main() {
while (true) {
cin >> S >> C; string D;
if (C == 'X')break;
if (C == 'L') {
for (int i = 0; i < S.size(); i++) {
if (S[i] == '_') {
i++; if (S[i] <= 'Z')D += S[i];
else D += (char)(S[i] - 32);
}
else {
D += S[i];
}
}
if (D[0] <= 'Z')D[0] += 32;
}
if (C == 'U') {
for (int i = 0; i < S.size(); i++) {
if (S[i] == '_') {
i++; if (S[i] <= 'Z')D += S[i];
else D += (char)(S[i] - 32);
}
else {
D += S[i];
}
}
if (D[0] > 'Z')D[0] -= 32;
}
if (C == 'D') {
for (int i = 0; i < S.size(); i++) {
if (S[i] <= 'Z' && i >= 1) { D += '_'; }
if (S[i] <= 'Z') { D += (char)(S[i] + 32); }
else { D += S[i]; }
}
}
cout << D << endl;
}
return 0;
}
``` |
### Prompt
In cpp, your task is to solve the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
queue<char>stToQu(string str){
queue<char>q;
for(int i = 0;i < str.size();i++){
q.push(str[i]);
}
return q;
}
string CamelCase(vector<string>words){
string str;
str += words[0];
for(int i = 1;i < words.size();i++){
string a = words[i];
a[0] -= 0x20;
str += a;
}
return str;
}
string UnderScore(vector<string>words){
string str;
str += words[0];
for(int i = 1;i < words.size();i++){
str += "_";
str += words[i];
}
return str;
}
string solve(string str,char type){
//cout << str;
vector<string>words;
queue<char> q = stToQu(str);
string a = "";
while(!q.empty()){
string word;
word = "";
word += a;
a = "";
while(!q.empty()){
char s = q.front();
q.pop();
//cout << s << "*" <<endl;
if(s <= 'Z'){
a.push_back(s + 0x20);
break;
}
if(s == '_'){
a = "";
break;
}
word.push_back(s);
//cout << "*" <<endl;
}
if(!word.empty())words.push_back(word);
}
if(!a.empty())words.push_back(a);
//for(int i= 0;i < words.size();i++)cout << words[i] <<endl;
string st;
//cout << type <<endl;
switch (type){
case 'U':
st = CamelCase(words);
st[0] -= 0x20;
break;
case 'L':
st = CamelCase(words);
break;
case 'D':
st = UnderScore(words);
break;
}
return st;
}
int main(){
vector<string> a;
while(1){
string str;
char type;
cin >> str >> type;
if(type == 'X')break;
a.push_back(solve(str,type));
}
for(int i = 0;i < a.size();i++){
cout << a[i] <<endl;
}
return 0;
}
``` |
### Prompt
Generate a CPP solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
using namespace std;
char get_type(string s)
{
if (islower(s[0]))
return s.find('_') != -1 ? 'D' : 'L';
else
return 'U';
}
vector<string> split_d(string s)
{
vector<string> v;
int p;
for (; (p = s.find('_')) != -1; )
{
if (p > 0) v.push_back(s.substr(0, p));
s = s.substr(p + 1);
}
if (!s.empty()) v.push_back(s);
return v;
}
vector<string> split_c(string s)
{
s[0] = tolower(s[0]);
vector<string> v;
for (int i = 0; i < s.length();)
{
if (isupper(s[i]))
{
string t = s.substr(0, i);
v.push_back(t);
s = s.substr(i);
s[0] = tolower(s[0]);
i = 0;
}
else
{
i++;
}
}
if (!s.empty()) v.push_back(s);
return v;
}
string make_d(vector<string> v)
{
string s = "";
for (int i = 0; i < v.size(); i++)
{
if (i != 0) s += "_";
s += v[i];
}
return s;
}
string make_c(vector<string> v, bool upper_camel)
{
string s = "";
for (int i = 0; i < v.size(); i++)
{
if (i != 0 || upper_camel) v[i][0] = toupper(v[i][0]);
s += v[i];
}
return s;
}
int main()
{
string s;
char c;
for(;cin>>s>>c, c!='X';)
{
char type = get_type(s);
vector<string> v;
switch (type)
{
case 'L':
case 'U':
v = split_c(s);
break;
case 'D':
v = split_d(s);
break;
}
switch (c)
{
case 'L':
cout << make_c(v, false);
break;
case 'U':
cout << make_c(v, true);
break;
case 'D':
cout << make_d(v);
break;
}
cout << endl;
}
return 0;
}
``` |
### Prompt
Your challenge is to write a JAVA solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
String s = sc.next();
String t = sc.next();
if(t.charAt(0)=='X') break;
char[] a = s.toCharArray();
char[] b = t.toCharArray();
if(s.contains("_")==true){
if(b[0]=='D') System.out.print(s);
else{
if(b[0]=='U' && (int)a[0]>96) a[0]-=32;
else if(b[0]=='L' && (int)a[0]<96) a[0]+=32;
for(int i=0;i<a.length;i++){
if(a[i]=='_') a[i+1]-=32;
else System.out.print(a[i]);
}
}
}else{
if(b[0]=='D'){
if((int)a[0]<96) a[0]+=32;
for(int i=0;i<a.length;i++){
if((int)a[i]<96){
System.out.print('_');
a[i]+=32;
}
System.out.print(a[i]);
}
}else{
if(b[0]=='U' && (int)a[0]>96) a[0]-=32;
else if(b[0]=='L' && (int)a[0]<96) a[0]+=32;
for(int i=0;i<a.length;i++) System.out.print(a[i]);
}
}
System.out.println();
}
}
}
``` |
### Prompt
Construct a JAVA code solution to the problem outlined:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.util.*;
import java.math.*;
import java.awt.geom.*;
import java.io.*;
public class Main {
static int INF = 2 << 27;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String name = sc.next();
char type = sc.next().charAt(0);
StringBuilder sb = new StringBuilder();
if(type == 'X') break;
else if(type == 'U') {
sb.append(Character.toUpperCase(name.charAt(0)));
for(int i = 1; i < name.length(); i++) {
if(name.charAt(i) == '_') {
sb.append(Character.toUpperCase(name.charAt(i+1)));
i++;
}
else {
sb.append(name.charAt(i));
}
}
}
else if(type == 'L') {
sb.append(Character.toLowerCase(name.charAt(0)));
for(int i = 1; i < name.length(); i++) {
if(name.charAt(i) == '_') {
sb.append(Character.toUpperCase(name.charAt(i+1)));
i++;
}
else {
sb.append(name.charAt(i));
}
}
}
else if(type == 'D') {
sb.append(Character.toLowerCase(name.charAt(0)));
for(int i = 1; i < name.length(); i++) {
if(Character.isUpperCase(name.charAt(i))) {
sb.append("_");
sb.append(Character.toLowerCase(name.charAt(i)));
}
else {
sb.append(name.charAt(i));
}
}
}
System.out.println(sb.toString());
}
}
}
``` |
### Prompt
Please formulate a cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <complex>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <functional>
#include <iterator>
using namespace std;
#define dump(n) cerr<<"# "<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;
int main()
{
for(string s,t;cin>>s>>t,t!="X";){
vs ss;
for(int i=0,j=1;i<s.size();j++){
if(j==s.size() || isupper(s[j])){
ss.push_back(s.substr(i,j-i));
i=j;
}
if(s[j]=='_'){
ss.push_back(s.substr(i,j-i));
i=++j;
}
}
if(t=="U")
rep(i,ss.size())
rep(j,ss[i].size())
putchar(j==0?toupper(ss[i][j]):tolower(ss[i][j]));
if(t=="L")
rep(i,ss.size())
rep(j,ss[i].size())
putchar(i&&j==0?toupper(ss[i][j]):tolower(ss[i][j]));
if(t=="D")
rep(i,ss.size()){
cout<<(i?"_":"");
rep(j,ss[i].size())
putchar(tolower(ss[i][j]));
}
cout<<endl;
}
return 0;
}
``` |
### Prompt
Generate a CPP solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
#include <sstream>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-9
void parseUL(const string& s, vector<string>& v){
string ts = s;
int i;
for(i=0; i<ts.size();){
string tmp;
ts[i] = tolower(ts[i]);
for(;i<ts.size() && islower(ts[i]); i++)tmp+=ts[i];
v.push_back(tmp);
}
}
void parseD(const string& s, vector<string>& v){
int i;
for(i=0; i<s.size(); i++){
string tmp;
for(;i<s.size() && s[i]!='_'; i++)tmp+=s[i];
v.push_back( tmp );
}
}
string ext(vector<string>& v, const string& type){
string ret;
if( type=="L" ){
rep(i,v.size()){
if( i!=0 )v[i][0]=toupper(v[i][0]);
ret += v[i];
}
}else if( type=="U" ){
rep(i,v.size()){
v[i][0]=toupper(v[i][0]);
ret += v[i];
}
}else{
rep(i,v.size()){
if( i!=0 )ret+="_";
ret += v[i];
}
}
return ret;
}
int main(){
string s,type;
while(cin>>s>>type,type!="X"){
vector<string> v;
if( s.find('_') != string::npos ){
parseD(s,v);
}else{
parseUL(s,v);
}
cout << ext(v,type) << endl;
}
return 0;
}
``` |
### Prompt
In CPP, your task is to solve the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
using namespace std;
#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
int N;
string in;
char type;
void solve() {
if (type == 'X') return;
N = in.size();
bool is_us = false;
vector<string> ret;
if (in.find('_') != -1) { is_us = true; }
if (is_us) {
// underscore
if (type == 'D') { printf("%s\n",in.c_str()); return; }
int cur = 0;
for (int i = 1; i < in.size(); ++i) {
if (in[i] == '_') {
ret.push_back(in.substr(cur,i-cur));
cur = i+1;
}
}
ret.push_back(in.substr(cur, in.size()-cur));
rep(i,ret.size()) ret[i][0] = toupper(ret[i][0]);
if (type == 'L') ret[0][0] = tolower(ret[0][0]);
rep(i,ret.size()) printf("%s",ret[i].c_str()); puts("");
} else {
// L or U
int cur = 0;
for (int i = 1; i < in.size(); ++i) {
if (isupper(in[i])) {
ret.push_back(in.substr(cur, i-cur));
cur = i;
}
}
ret.push_back(in.substr(cur, in.size()-cur));
if (type == 'D') {
rep(i,ret.size()) ret[i][0] = tolower(ret[i][0]);
rep(i,ret.size()) {
if(i) putchar('_'); printf("%s",ret[i].c_str());
} puts("");
} else {
if (type == 'U') ret[0][0] = toupper(ret[0][0]);
else ret[0][0] = tolower(ret[0][0]);
rep(i,ret.size()) printf("%s",ret[i].c_str()); puts("");
}
}
}
int main() {
while (cin >> in >> type) solve();
return 0;
}
``` |
### Prompt
Develop a solution in CPP to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<string>
#include<cctype>
#include<algorithm>
#include<functional>
using namespace std;
bool C(char c){return isupper(c);}
int main()
{
string s;
string::iterator i;
char c;
int x;
while(cin>>s>>c,c-'X')
{
s[0]=toupper(s[0]);
while(x=s.find('_'),x!=string::npos)
s.erase(x,1),s[x]=toupper(s[x]);
if(c-'U')s[0]=tolower(s[0]);
if(c=='D')
for(i=s.begin();(i=find_if(i,s.end(),ptr_fun(C)))!=s.end();++i)
*i=tolower(*i),i=s.insert(i,'_');
cout<<s<<endl;
}
return 0;
}
``` |
### Prompt
Please create a solution in CPP to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<string>
#include<cctype>
#include<algorithm>
using namespace std;
void UpperCamelCase(string&);
void LowerCamelCase(string&);
void UnderScoreCase(string&);
bool IsUpperCase(char);
int main(){
char c;
string str;
while(1){
cin >> str >> c;
if(c == 'X') break;
else if(c == 'U') UpperCamelCase(str);
else if(c == 'L') LowerCamelCase(str);
else if(c == 'D') UnderScoreCase(str);
cout << str << endl;
}
return 0;
}
void UpperCamelCase(string& s){
string::iterator i;
if(s[0] >= 'a' && s[0] <= 'z')
s[0] = toupper(s[0]);
i = s.begin();
while(1){
i = find(i, s.end(), '_');
if(i == s.end()) break;
i = s.erase(i);
*i = toupper(*i);
}
}
void LowerCamelCase(string& s){
string::iterator i;
if(s[0] >= 'A' && s[0] <= 'Z')
s[0] = tolower(s[0]);
i = s.begin();
while(1){
i = find(i, s.end(), '_');
if(i == s.end()) break;
i = s.erase(i);
*i = toupper(*i);
}
}
void UnderScoreCase(string& s){
string::iterator i;
if(s[0] >= 'A' && s[0] <= 'Z')
s[0] = tolower(s[0]);
i = s.begin();
while(1){
i = find_if(i, s.end(), IsUpperCase);
if(i == s.end()) break;
*i = tolower(*i);
i = s.insert(i, '_');
++i;
}
}
bool IsUpperCase(char c){
if(c >= 'A' && c <= 'Z')
return true;
else
return false;
}
``` |
### Prompt
In PYTHON3, your task is to solve the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```python3
while True:
a,b=input().split()
if b=='X':
break
a=list(a)
if ('_' in a) and (b=='U' or b=='L'):
c=0
for i in range(len(a)):
if i==0 and b=='U':
a[i]=a[i].upper()
elif a[i]=='_':
c+=1
elif c==1:
a[i]=a[i].upper()
c=0
a=[i for i in a if i!='_']
elif b=='U':
a[0]=a[0].upper()
elif b=='L':
a[0]=a[0].lower()
else:
s=0
for i in range(len(a)):
if i==0:
a[0]=a[0].lower()
s+=1
elif a[s].isupper():
a[s]=a[s].lower()
a.insert(s,'_')
s+=2
else:
s+=1
for i in range(len(a)):
if i==len(a)-1:
print(a[i])
else:
print(a[i],end='')
``` |
### Prompt
Generate a java solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.util.*;
public class Main {
static char[] tango;
static String str;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(true){
str = cin.next();
String c = cin.next();
if(c.equals("X")){
break;
}
tango = new char[300];
System.out.println(CamelCase(c.charAt(0)));
}
}
static String CamelCase(char c){
String res = "";
int wordCnt=0;
int before=0;
boolean under=false;;
String[] words = new String[100];
for(int i = 0; i < 100; i++){
words[i]="";
}
for(int i = 0; i < str.length(); i++){
if(str.charAt(i)=='_'){
under=true;
words[wordCnt]=str.substring(before, i);
before = i+1;
wordCnt++;
}
}
if(under){
words[wordCnt]=str.substring(before, str.length());
wordCnt++;
}
else{
for(int i = 1; i < str.length();i++){
char a = str.charAt(i);
if(a <= 'Z' && a >= 'A'){
words[wordCnt++]=str.substring(before, i);
before=i;
}
}
words[wordCnt++]=str.substring(before, str.length());
}
for(int i = 0; i < wordCnt; i++){
//System.out.println(words[i]);
}
if(c == 'L'){
for(int i = 0; i < wordCnt; i++){
if(i==0){
char a = words[i].charAt(0);
a = Character.toLowerCase(a);
words[i] = a + words[i].substring(1, words[i].length());
}
else{
char a = words[i].charAt(0);
a = Character.toUpperCase(a);
words[i] = a + words[i].substring(1, words[i].length());
}
res += words[i];
}
}
else if(c == 'U'){
for(int i = 0; i < wordCnt; i++){
char a = words[i].charAt(0);
a = Character.toUpperCase(a);
words[i] = a + words[i].substring(1, words[i].length());
res += words[i];
}
}
else if(c == 'D'){
for(int i = 0; i < wordCnt; i++){
char a = words[i].charAt(0);
a = Character.toLowerCase(a);
words[i] = a + words[i].substring(1, words[i].length());
if(i!=0){
res += "_";
}
res += words[i];
}
}
return res;
}
}
``` |
### Prompt
Please formulate a Cpp solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
template<C T>void pr(T a){cout<<a;ln;}
template<C T,C T2>void pr(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
void Main() {
string s,t;
while(cin >> s >> t && t!="X") {
vector<string> v;
string r="";
s+='_';
rep(i,s.size()) {
if(isupper(s[i])) {
if(r.size()) v.pb(r);
r=tolower(s[i]);
} else if(s[i]=='_') {
if(r.size()) v.pb(r);
r="";
} else r+=s[i];
}
string ans="";
if(t=="L") {
rep(i,v.size()) {
if(i) v[i][0]=toupper(v[i][0]);
ans+=v[i];
}
} else if(t=="U") {
rep(i,v.size()) {
v[i][0]=toupper(v[i][0]);
ans+=v[i];
}
} else {
rep(i,v.size()) {
if(i) ans+='_';
ans+=v[i];
}
}
pr(ans);
}
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
Main();return 0;
}
``` |
### Prompt
Develop a solution in Cpp to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
using namespace std;
string UOP(string str){
string ans;
if('a' <= str[0] && str[0] <= 'z') str[0] = toupper(str[0]);
for(int i = 0 ; i < str.size() ; i++){
if(str[i] == '_'){
if('a' <= str[i+1] && str[i+1] <= 'z') str[i+1] = toupper(str[i+1]);
}
else ans += str[i];
}
return ans;
}
string LOP(string str){
string ans;
if('A' <= str[0] && str[0] <= 'Z') str[0] = tolower(str[0]);
for(int i = 0 ; i < str.size() ; i++){
if(str[i] == '_'){
if('a' <= str[i+1] && str[i+1] <= 'z') str[i+1] = toupper(str[i+1]);
}
else ans += str[i];
}
return ans;
}
string DOP(string str){
string ans;
for(int i = 0 ; i < str.size() ; i++){
if('A' <= str[i] && str[i] <= 'Z') str[i] = tolower(str[i]);
ans += str[i];
if('A' <= str[i+1] && str[i+1] <= 'Z') ans += '_';
}
return ans;
}
int main(){
string str;
char op;
while(cin >> str >> op, op != 'X'){
if(op == 'U'){
cout << UOP(str) << endl;
}
else if(op == 'L'){
cout << LOP(str) << endl;
}
else if(op == 'D'){
cout << DOP(str) << endl;
}
}
return 0;
}
``` |
### Prompt
Create a solution in CPP for the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<string>
using namespace std;
string toLowerCamel(string str){
string l = "";
char pre = ' ';
for ( int i = 0; i < str.size(); i++ ){
if ( isalpha(str[i]) ){
if ( pre == '_' ) l += toupper(str[i]);
else l += str[i];
}
pre = str[i];
}
l[0] = tolower(l[0]);
return l;
}
int main(){
string name;
char com;
while(1){
cin >> name >> com;
if ( com == 'X' ) break;
string l = toLowerCamel(name);
if ( com == 'L' ) cout << l << endl;
else if ( com == 'U' ){
l[0] = toupper(l[0]);
cout << l << endl;
} else if ( com == 'D' ){
for ( int i = 0; i < l.size(); i++ ){
if ( 'A' <= l[i] && l[i] <= 'Z' ){
cout << "_";
cout << (char)(tolower(l[i]));
} else cout << l[i];
}
cout << endl;
}
}
}
``` |
### Prompt
Please formulate a CPP solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using State = string::const_iterator;
string expr(State &itr, int mode){
string res = "";
res.push_back(*itr);
++itr;
if(mode==0){
res[0] = toupper(res[0]);
}else{
res[0] = tolower(res[0]);
}
while(1){
if(islower(*itr)){
res.push_back(*itr);
++itr;
}else{
break;
}
}
if(mode==0 || mode==1){
if(*itr == '_') ++itr;
}else{
if(isalpha(*itr)) res.push_back('_');
}
return res;
}
int main(){
string s;
char op;
while(cin >> s >> op){
if(op == 'X') break;
string ans = "";
int m;
if(op == 'U') m = 0;
if(op == 'L') m = 1;
if(op == 'D') m = 2;
State begin = s.begin();
while(begin != s.end()){
ans += expr(begin,m);
if(m==1) m = 0;
}
cout << ans << endl;
}
return 0;
}
``` |
### Prompt
Your challenge is to write a JAVA solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```java
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
int f=0;
String str4[]=new String[100];
str4=br.readLine().split(" ");
String str1=str4[0];
String str2=str4[1];
String str3[]=new String[100];
char ch=str2.charAt(0);
int n=str1.indexOf("_");
//原文の判定
if(n>=0){
str3=str1.split("_");
f=2;
}
else if(Character.isUpperCase(str1.charAt(0)))f=1;
String str="";
//規則の判定
if(ch=='X')break;
else if(ch=='U'){
//変換
if(f==0)str=str1.substring(0,1).toUpperCase()+str1.substring(1);
else if(f==2){
for(int i=0;i<str3.length;i++)
str+=str3[i].substring(0,1).toUpperCase()+str3[i].substring(1);
}
else str=str1;
}
else if(ch=='L'){
//変換
if(f==0)str=str1;
else if(f==1)str=str1.substring(0,1).toLowerCase()+str1.substring(1);
else if(f==2){
str+=str3[0];
for(int i=1;i<str3.length;i++)
str+=str3[i].substring(0,1).toUpperCase()+str3[i].substring(1);
}
}
else if(ch=='D') {
//変換
str1=str1.substring(0,1).toLowerCase()+str1.substring(1);
int idx=0;
for(int i=0;i<str1.length();i++){
if(Character.isUpperCase(str1.charAt(i))){
str+=str1.substring(idx,i)+"_"+str1.substring(i,i+1).toLowerCase();
idx=i+1;
}
}
str+=str1.substring(idx);
}
System.out.println(str);
}
}
}
``` |
### Prompt
Please create a solution in cpp to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
using namespace std;
#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second
#define PI acos(-1.0)
#define INF 10e15+9
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
string s;
char type;
int main(){
while (cin >> s >> type){
string ans = "";
if (type == 'X')break;
if (type == 'L'){
REP(i, s.size()){
if (i == 0)ans += tolower(s[i]);
else {
if (s[i] == '_'){
i++;
ans += toupper(s[i]);
}
else ans += s[i];
}
}
}
else if (type == 'U'){
REP(i, s.size()){
if (i == 0)ans += toupper(s[i]);
else{
if (s[i] == '_'){
i++;
ans += toupper(s[i]);
}
else ans += s[i];
}
}
}
else{
REP(i, s.size()){
if (i != 0 && s[i] >= 'A'&&s[i] <= 'Z')ans += '_';
ans += tolower(s[i]);
}
}
cout << ans << endl;
}
}
``` |
### Prompt
Develop a solution in CPP to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <iostream>
#include <string>
using namespace std;
void solve()
{
string str;
char c;
while(cin >> str)
{
cin >> c;
string Answer;
int size = str.size();
if(c == 'X')
{
break;
}
else if(c == 'L')
{
if('A' <= str[0] && str[0] <= 'Z')
{
Answer += str[0] - 'A' + 'a';
}
else if('a' <= str[0] && str[0] <= 'z')
{
Answer += str[0];
}
bool flag = false;
for(int i = 1; i < size; ++i)
{
if(str[i] == '_')
{
flag = true;
continue;
}
if(flag && 'a' <= str[i] && str[i] <= 'z')
{
Answer += str[i] - 'a' + 'A';
flag = false;
continue;
}
Answer += str[i];
flag = false;
}
}
else if(c == 'U')
{
if('A' <= str[0] && str[0] <= 'Z')
{
Answer += str[0];
}
else if('a' <= str[0] && str[0] <= 'z')
{
Answer += str[0] - 'a' + 'A';
}
bool flag = false;
for(int i = 1; i < size; ++i)
{
if(str[i] == '_')
{
flag = true;
continue;
}
if(flag && 'a' <= str[i] && str[i] <= 'z')
{
Answer += str[i] - 'a' + 'A';
flag = false;
continue;
}
Answer += str[i];
flag = false;
}
}
else if(c == 'D')
{
if('A' <= str[0] && str[0] <= 'Z')
{
Answer += str[0] - 'A' + 'a';
}
else if('a' <= str[0] && str[0] <= 'z')
{
Answer += str[0];
}
for(int i = 1; i < size; ++i)
{
if(str[i] == '_')
{
Answer += '_';
continue;
}
if('A' <= str[i] && str[i] <= 'Z')
{
Answer += '_';
Answer += str[i] - 'A' + 'a';
continue;
}
Answer += str[i];
}
}
cout << Answer << endl;
}
}
int main()
{
solve();
return(0);
}
``` |
### Prompt
Develop a solution in Python3 to the problem described below:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```python3
while True:
s = input()
s, order = s.split()
if order == 'X':
break
if '_' in s:
if order == 'U':
s = s.split('_')
a = ''
for i in s:
a += i.capitalize()
print(a)
elif order == 'L':
s = s.split('_')
a = ''
a += s[0].lower()
for i in s[1:]:
a += i.capitalize()
print(a)
else:
print(s.lower())
else:
if order == 'U':
if s[0].isupper():
print(s)
else:
s = s[0].upper() + s[1:]
print(s)
elif order == 'L':
if s[0].islower():
print(s)
else:
s = s[0].lower() + s[1:]
print(s)
else:
index = []
c = 0
for i in range(len(s[1:])):
if s[i+1].isupper():
index.append(i + 1+c)
c += 1
for j in index:
s = s[: j] + '_' + s[j:]
print(s.lower())
``` |
### Prompt
Your challenge is to write a CPP solution to the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;
typedef long long Integer;
typedef double Real;
template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
os << '(' << car(p) << ", " << cdr(p) << ')';
return os;
}
template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
os << v[0];
for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
return os;
}
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };
inline bool isD(string s) { return s.find('_') != string::npos; }
inline bool isL(string s) { return !isD(s) && islower(s[0]); }
inline bool isU(string s) { return !isD(s) && isupper(s[0]); }
vector<string>
splitL(string s) {
vector<string> ret;
int idx = 0;
rep(i, s.size()) {
if (isupper(s[i])) {
string w = char(tolower(s[idx])) + s.substr(idx + 1, i - idx - 1);
ret.push_back(w);
idx = i;
}
}
string w = char(tolower(s[idx])) + s.substr(idx + 1, s.size() - idx - 1);
ret.push_back(w);
return ret;
}
vector<string>
splitU(string s) {
return splitL(char(tolower(s[0])) + s.substr(1, s.size() - 1));
}
vector<string>
splitD(string s) {
vector<string> ret;
int idx = 0;
rep(i, s.size()) {
if (s[i] == '_') {
ret.push_back(s.substr(idx, i - idx));
idx = i + 1;
}
}
ret.push_back(s.substr(idx, s.size() - idx));
return ret;
}
string toL(vector<string>&v) {
string r = v[0];
for (int i=1; i < v.size(); ++i) {
r += char(toupper(v[i][0])) + v[i].substr(1, v[i].size() - 1);
}
return r;
}
string toU(vector<string>&v) {
string r = "";
for (int i=0; i < v.size(); ++i) {
r += char(toupper(v[i][0])) + v[i].substr(1, v[i].size() - 1);
}
return r;
}
string toD(vector<string>&v) {
string r = v[0];
for (int i=1; i < v.size(); ++i) {
r += "_" + v[i];
}
return r;
}
int main() {
more:
string s, t;
cin >> s >> t;
if (t == "X") return 0;
vector<string> v;
if (isL(s)) {
v = splitL(s);
} else if (isU(s)) {
v = splitU(s);
} else if (isD(s)) {
v = splitD(s);
}
if (t == "U") {
cout << toU(v) << endl;
} else if (t == "L") {
cout << toL(v) << endl;
} else if (t == "D") {
cout << toD(v) << endl;
}
goto more;
}
``` |
### Prompt
Create a solution in Cpp for the following problem:
When naming identifiers (variables and functions) in programming, compound words that concatenate words are used. However, if you concatenate them as they are, you will not be able to understand the word breaks, so in general, select and apply the one that is unified from the following naming conventions:
* Set to Upper CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word.
Example: GetUserName
* Set to Lower CamelCase
Connect words directly to form a compound word, and capitalize only the first letter of each word. However, the first letter of the compound word should be lowercase.
Example: getUserName
* Connect with underscore
Words are concatenated with underscores to form a compound word. Make all letters of the word lowercase.
Example: get_user_name
Create a program that outputs the given identifier by applying the specified naming convention. It is assumed that any of the above naming conventions has already been applied to the identifier given.
Input
Multiple datasets are given as input. Each dataset is given in the following format:
name type (identifier, naming convention: space-separated strings and characters)
type is a character indicating the naming convention and is as shown in the table below:
type | Naming convention
--- | ---
U | Upper CamelCase
L | Lower CamelCase
D | Connect with underscore
The number of characters in the given identifier is 1 or more and 100 or less.
End of input when type is'X'. Do not output to this input.
Output
For each dataset, print the identifier with the naming convention on one line.
Example
Input
get_user_name L
getUserName U
GetUserName D
EndOfInput X
Output
getUserName
GetUserName
get_user_name
### Response
```cpp
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
char S;
while(1){
cin>>str;
cin>>S;
if(S=='X') break;
else if(S=='L'){
if(str[0]>='A' && str[0]<='Z') str[0]+=32;
for(int i=0;i<str.size()-1;i++){
if(str[i]=='_' && str[i+1]>='a' && str[i+1]<='z' ) str[i+1]-=32;
}
for(int i=0;i<str.size();i++){
if(str[i]=='_') continue;
else cout<<str[i];
}
cout<<endl;
}
else if(S=='U') {
if(str[0]>='a' && str[0]<='z') str[0]-=32;
for(int i=0;i<str.size()-1;i++){
if(str[i]=='_'&& str[i+1]>='a' && str[i+1]<='z') str[i+1]-=32;
}
for(int i=0;i<str.size();i++){
if(str[i]=='_') continue;
else cout<<str[i];
}
cout<<endl;
}
else if(S=='D'){
for(int i=0;i<str.size();i++){
if(str[i]>=65 && str[i]<=90){
str[i]+=32;
}
if(i>=0 && i<str.size()-1 && str[i+1]>=65 && str[i+1]<=90){
cout<<str[i]<<"_";
}
else cout<<str[i];
}
cout<<endl;
}
}
return 0;
}
``` |
### Prompt
Your challenge is to write a PYTHON solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python
while 1:
H = int(raw_input())
if H == 0: break
A = [map(int,raw_input().split())+[0] for _ in range(H)]
ans = 0
while 1:
vanish = False
for h in range(H):
l = r = 0
while r < 6:
if A[h][r] != A[h][l]:
if r-l >= 3:
if A[h][l] == 0:break
ans += A[h][l]*(r-l)
A[h][l:r] = [0]*(r-l)
vanish = True
l = r
r += 1
if not vanish: break
for h in range(H)[::-1]:
for w in range(5):
if A[h][w] == 0:
for hh in range(h)[::-1]:
if A[hh][w] != 0:
A[h][w] = A[hh][w]
A[hh][w] = 0
break
print ans
``` |
### Prompt
Your task is to create a cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <vector>
#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
int main()
{
int H;
while (true)
{
scanf("%d", &H);
if (H == 0) { break; }
vector<vector<int> > M(H, vector<int>(5));
for (int i = 0; i < H; i++)
{
for (int j = 0; j < 5; j++)
{
scanf("%d", &M[i][j]);
}
}
int ret = 0;
while (true)
{
for (int i = 0; i < H; i++)
{
for (int j = 5; j >= 3; j--)
{
for (int k = 0; k <= 5 - j; k++)
{
bool ok = (M[i][k] != 0);
for (int l = k + 1; l < k + j; l++)
{
if (M[i][l - 1] != M[i][l] || M[i][l] == 0)
{
ok = false;
}
}
if (ok)
{
ret += M[i][k] * j;
for (int l = k; l < k + j; l++)
{
M[i][l] = 0;
}
}
}
}
}
bool ok = false;
for (int rep = 0; rep < H; rep++)
{
for (int i = H - 1; i >= 1; i--)
{
for (int j = 0; j < 5; j++)
{
if (M[i][j] == 0 && M[i - 1][j] != 0)
{
M[i][j] = M[i - 1][j]; M[i - 1][j] = 0; ok = true;
}
}
}
}
if (!ok) { break; }
}
printf("%d\n", ret);
}
return 0;
}
``` |
### Prompt
Your task is to create a CPP solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<iostream>
#include<vector>
using namespace std;
int main(){
int h;
cin >> h;
while(h!=0){
vector <int> lines[5];
int b[10][5];
int i,j,k;
for(i=0;i<h;i++){
for(j=0;j<5;j++){
cin >> b[i][j];
}
}
for(i=h-1;i>=0;i--){
for(j=0;j<5;j++){
lines[j].push_back(b[i][j]);
}
}
int o=0;
int f=0,ba,l;
while(f==0){
k=0;
for(i=0;i<5;i++) k+=lines[i].size();
for(i=h-1;i>=0;i--){
l=0;
ba=0;
for(j=0;j<5;j++){
if(lines[j].size()>i){
if(ba==0){
ba=lines[j][i];
l=1;
}else if(ba==lines[j][i]){
l++;
}else{
if(l>=3){
o+=ba*l;
for(int a=1;a<=l;a++) lines[j-a].erase(lines[j-a].begin()+i);
}
ba=lines[j][i];
l=1;
}
}else{
ba=0;
l=0;
}
}
if(l>=3){
o+=ba*l;
for(int a=0;a<l;a++) lines[4-a].erase(lines[4-a].begin()+i);
}
}
for(i=0;i<5;i++) k-=lines[i].size();
if(k==0) f=1;
}
cout << o << endl;
cin >> h;
}
return 0;
}
``` |
### Prompt
Develop a solution in Cpp to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <iostream>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, s, n) for(int i = s; i < n; i++)
#define repr(i, n) for(int i = n-1; i >= 0; i--)
#define repr2(i, s, n) for(int i = n-1; i >= s; i--)
using namespace std;
int main(){
while(1){
int h;
cin >> h;
if(h == 0) break;
vector<vector<int>> board(h, vector<int>(5));
rep(i, h)rep(j, 5) cin >> board[i][j];
int score = 0;
bool flag;
while(1){
flag = true;
// 消滅させる
rep(i, h){
int count;
int prev = -1;
int now;
rep(j, 5){
now = board[i][j];
if (now == 0) count = 0;
else if (now != prev) count = 1;
else count++;
if (count == 3){
flag = false; // 一回でも消滅したらもう一度判定する
score += now * 3;
board[i][j] = board[i][j-1] = board[i][j-2] = 0;
}
else if (count > 3){
score += now;
board[i][j] = 0;
}
prev = now;
}
}
// 消滅しなかったら終わり
if (flag) break;
// 消滅した分、下に下げる
rep(k, h-1)repr2(i, k+1, h)rep(j, 5){
if (board[i][j] == 0){
board[i][j] = board[i-1][j];
board[i-1][j] = 0;
}
}
}
cout << score << endl;
}
return 0;
}
``` |
### Prompt
Construct a CPP code solution to the problem outlined:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <cstdio>
using namespace std;
int main() {
const int MAX_H = 10;
int H, board[MAX_H][5];
int res;
bool updated;
bool zeroseq;
int seq;
while(true){
scanf("%d", &H);
if (H == 0) {
break;
}
for (int i = 0;i < H;i++) {
for (int j = 0;j < 5;j++) {
scanf("%d", &board[i][j]);
}
}
updated = true;
res = 0;
while (updated) {
updated = false;
for (int i = 0;i < H;i++) {
seq = 1;
for (int j = 0;j < 5;j++) {
if (j == 4 || board[i][j] != board[i][j + 1]) {
if (seq >= 3) {
updated = true;
res += board[i][j] * seq;
for (int k = 0;k < seq;k++) {
board[i][j - k] = 0;
}
}
seq = 1;
}
else if (j + 1 < 5 && board[i][j] == board[i][j + 1] && board[i][j] != 0) {
seq++;
}
}
}
for (int j = 0;j < 5;j++) {
zeroseq = true;
for (int i = 0;i < H;i++) {
if (board[i][j] == 0 && !zeroseq) {
for (int k = i;k > 0;k--) {
board[k][j] = board[k - 1][j];
}
board[0][j] = 0;
}
else {
zeroseq = false;
}
}
}
}
printf("%d\n", res);
}
return 0;
}
``` |
### Prompt
Your challenge is to write a java solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```java
import java.text.CollationElementIterator;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
static int[][] map;
static int[][] directions8 = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 },
{ 1, 1 } };
static int[][] directions4 = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
static int ans;
static int[][] puyo;
static int H;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
H = sc.nextInt();
if (H == 0) {
break;
}
puyo = new int[H][5];
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
puyo[i][j] = sc.nextInt();
}
}
ans = 0;
int tmpAns = -1;
while (ans != tmpAns) {
tmpAns = ans;
// 各行を見る
for (int i = 0; i < H; i++) {
// それぞれの開始列
for (int j = 0; j <= 2; j++) {
if (puyo[i][j] == 0) {
continue;
}
int samePuyos = 1;
// 後の数字と比べる
for (int k = j + 1; k < 5; k++) {
if (puyo[i][j] == puyo[i][k]) {
samePuyos++;
}
if (puyo[i][j] != puyo[i][k] || k == 4) {
if (samePuyos > 2) {
ans += puyo[i][j] * samePuyos;
for (int l = j; l < j + samePuyos; l++) {
// 消えたぷよを0にする
puyo[i][l] = 0;
}
}
break;
}
}
}
}
puyoFall();
}
System.out.println(ans);
}
}
static void puyoFall() {
for (int k = 0; k < H; k++) {
for (int i = H - 1; i > 0; i--) {
for (int j = 0; j < 5; j++) {
if (puyo[i][j] == 0) {
puyo[i][j] = puyo[i - 1][j];
puyo[i - 1][j] = 0;
}
}
}
}
}
static int getNum(List<Integer> column, int idx, int count) {
if (column.get(count) != 0) {
idx--;
}
if (idx == -1 || column.get(count) == -1) {
return column.get(count);
}
count++;
return getNum(column, idx, count);
}
// BFS用に二つの配列を足し算する
static int[] addArrayElms(int[] a, int[] b) {
int[] c = new int[a.length];
for (int i = 0; i < a.length; i++) {
c[i] = a[i] + b[i];
}
return c;
}
// //二分探索
// k <= num となる最小の配列要素kのインデックスを返す
static private int binarySearch(long num, long[] orderedArray) {
int lowerBorder = -1;
int upperBorder = orderedArray.length;
int mid;
while (upperBorder - lowerBorder > 1) {
mid = (upperBorder + lowerBorder) / 2;
if (orderedArray[mid] <= num) {
lowerBorder = mid;
} else {
upperBorder = mid;
}
}
return lowerBorder;
}
// 二分探索
// k <= num となる最小のList要素kのインデックスを返す
static private int binarySearch(long num, ArrayList<Long> orderedList) {
int lowerBorder = -1;
int upperBorder = orderedList.size();
int mid;
while (upperBorder - lowerBorder > 1) {
mid = (upperBorder + lowerBorder) / 2;
if (orderedList.get(mid) <= num) {
lowerBorder = mid;
} else {
upperBorder = mid;
}
}
return lowerBorder;
}
// aとbの最小公倍数を求める
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
}
``` |
### Prompt
Please provide a cpp coded solution to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
bool flag;
int sum, H;
while (cin >> H, H) {
sum = 0;
vector< vector<int> > a(5, vector<int>(H, 0));
for (int j = 0; j < H; j++) {
for (int i = 0; i < 5; i++) {
cin >> a[i][j];
sum += a[i][j];
}
}
do {
flag = false;
for (int j = H - 1; j >= 0; j--) {
for (int i = 0; i < 3; i++) {
if (a[i][j] != 0 && a[i][j] == a[i + 1][j] && a[i][j] == a[i + 2][j]) {
if (i <= 1 && a[i][j] == a[i + 3][j]) {
if (i == 0 && a[i][j] == a[i + 4][j]) {
a[i + 4][j] = 0;
}
a[i + 3][j] = 0;
}
a[i][j] = a[i + 1][j] = a[i + 2][j] = 0;
flag = true;
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < H - 1; j++) {
if (a[i][j + 1] == 0 && a[i][j] != 0) {
a[i][j + 1] = a[i][j];
a[i][j] = 0;
j = -1;
}
}
}
} while (flag);
for (int j = 0; j < H; j++)
for (int i = 0; i < 5; i++)
sum -= a[i][j];
cout << sum << endl;
}
return 0;
}
``` |
### Prompt
Create a solution in PYTHON for the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python
while 1:
h = input()
if h == 0:
break
M = [map(int, raw_input().split()) for i in xrange(h)]
ans = 0
update = 1
while update:
update = 0
for i in xrange(h):
prev = 0; cnt = 0
for j in xrange(5):
if prev != M[i][j]:
if cnt >= 3:
M[i][j-cnt:j] = [0]*cnt
ans += prev * cnt
update |= prev
cnt = 1
else:
cnt += 1
prev = M[i][j]
if cnt >= 3:
M[i][5-cnt:5] = [0]*cnt
ans += prev * cnt
update |= prev
for j in xrange(5):
cur = h-1
for i in xrange(h-1, -1, -1):
if M[i][j]:
if cur != i:
M[cur][j] = M[i][j]
M[i][j] = 0
cur -= 1
print ans
``` |
### Prompt
Construct a Java code solution to the problem outlined:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```java
import java.util.Scanner;
public class Main
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
while(true)
{
int input = scan.nextInt();
if(input == 0)
{
break;
}
else
{
new DataSet(input);
}
}
}
static class DataSet
{
int[][] tile;
int col;
int score = 0;
public DataSet(int arg)
{
col = arg;
tile = new int[5][col];
for(int y = 0; y < col; y++)
{
for(int x = 0; x < 5; x++)
{
tile[x][y] = scan.nextInt();
}
}
while(true)
{
// System.out.println("PHASE");
if(checkMatches() == 0)
{
break;
}
boolean fell = true;
while(fell == true)
{
// System.out.println("fall");
fell = fall();
}
}
System.out.println(score);
}
boolean fall()
{
// System.out.println("FALL");
boolean fell = false;
for(int y = col-1; y >= 0; y--)
{
for(int x = 0; x < 5; x++)
{
if(tile[x][y] == 0)
{
if(y > 0)
{
if(tile[x][y-1] > 0) {fell = true;}
tile[x][y] = tile[x][y-1];
tile[x][y-1] = 0;
}
}
}
}
return fell;
}
int checkMatches()
{
int scoreGot = 0;
for(int y = 0; y < col; y++)
{
int curNum = -1;
int occurences = 0;
for(int x = 0; x < 5; x++)
{
int sel = tile[x][y];
if(sel > 0)
{
if(sel == curNum)
{
occurences += 1;
}
else
{
if(occurences >= 3)
{
//left to this disappears
// System.out.println("Score " + tile[x-1][y] + " x " + occurences + " at " + y);
for(int xDis = x - occurences; xDis <= x-1; xDis++)
{
scoreGot += tile[xDis][y];
tile[xDis][y] = 0;
}
}
curNum = sel;
occurences = 1;
}
}
else
{
occurences = 0;
}
}
//
if(occurences >= 3)
{
//left to this disappears
// System.out.println("Score " + tile[4][y] + " x " + occurences + " at " + y);
for(int xDis = 5 - occurences; xDis <= 4; xDis++)
{
scoreGot += tile[xDis][y];
tile[xDis][y] = 0;
}
}
//
}
score += scoreGot;
return scoreGot;
}
}
}
``` |
### Prompt
Your challenge is to write a JAVA solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```java
import java.util.Scanner;
public class Main {
static int[][] board;
static boolean[][] delete;
static boolean[][] check;
static int sum;
static int H;
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
//??\??????
H = sc.nextInt();
if(H == 0)
return;
board = new int[H][5];
delete = new boolean[H][5];
check = new boolean[H][5];
int sumarray[] = new int[H];
sum = 0;
for(int i=0;i<H;i++)
{
for(int j=0;j<5;j++)
{
board[i][j] = sc.nextInt();
delete[i][j] = false;
check[i][j] = false;
}
}
//??????
boolean flag = true;
int count=0;
while(flag)
{
fall();
for(int j=0;j<5;j++)
{
for(int i=H-1;i>=0;i--)
check[i][j] = false;
}
del();
int x=0;
int y=0;
boolean fl = true;
for(x=0;x<H;x++)
{
for(y=0;y<5;y++)
{
// System.out.print(" "+board[x][y]);
if(delete[x][y]){
fl = false;
// break;
}
}
// System.out.println();
}
//System.out.println();
if(fl)
{
flag = false;
}
}
System.out.println(sum);
}//while??????
}//main?????????
public static int check(int i,int j){
if(i < 0)
return 0;
if(check[i][j])
return check(i-1,j);
else
{
check[i][j] = true;
return board[i][j];
}
/*
if(i < 0)
return 0;
else if(check[i][j])
return check(i-1,j);
else
return board[i][j];
*/
}
public static boolean fall()
{
for(int j=0;j<5;j++)
{
int count = 0;
for(int i=H-1;i>=0;i--)
{
board[i][j] = check(i,j);
check[i][j] = true;
if(delete[i][j])
delete[i][j] = false;
/*
int tmp =count;
if(delete[i][j])
{
count = count(i,j,count);
delete[i][j] = false;
}
if(i -count >0 && delete[i-count][j])
{
delete[i-count][j] = false;
count = count(i-count,j,count);
}
if(i-count < 0)
board[i][j] = 0;
else if(board)
board[i][j] =board[i-(count+tmp)][j];
else
board[i][j] = board[i-count][j];
*/
}
}
return true;
}
public static int count(int i,int j,int count){
count++;
if(i > 0&& delete[i-1][j] == true)
{
return count(i-1,j,count);
}
else
return count;
}
public static void del()
{
for(int i=0;i<H;i++)
{
int num = board[i][0];
int count = 1;
for(int j=1;j<5;j++)
{
if(board[i][j] == num && num !=0)
{
count++;
if(j == 4 && count >= 3)
{
//?????£??¨?????????????????????
sum += count * num;
for(int k=0;k<count;k++)
{
delete[i][j-k] = true;
check[i][j-k] = true;
}
}
}
else
{
if(count >= 3)
{
//?????£??¨?????????????????????
sum += count * num;
for(int k=1;k<=count;k++)
{
delete[i][j-k] = true;
check[i][j-k] = true;
}
}
num = board[i][j];
count = 1;
}
}
}//??????????????????
}
}
``` |
### Prompt
Construct a Java code solution to the problem outlined:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```java
import java.util.*;
public class Main {
static int[][] map;
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
while(true) {
int H = stdIn.nextInt();
if(H == 0) break;
map = new int[H][5];
for(int i = 0; i < H; i++) {
for(int j = 0; j < 5; j++) {
map[i][j] = stdIn.nextInt();
}
}
int ans = 0;
while(true) {
int x = check();
if(x == 0) break;
ans += x;
remove();
drop();
}
System.out.println(ans);
}
}
public static int check() {
int ans = 0;
for(int i = 0; i < map.length; i++) {
int l = 0;
int c = 0;
for(int j = 0; j < 5; j++) {
if(l == map[i][j]) {
c++;
}
else if(c >= 3){
ans += c * l;
c = 1;
}
else {
c = 1;
}
l = map[i][j];
if(j == 4 && c >= 3) {
ans += c * l;
}
}
}
return ans;
}
public static void remove() {
for(int i = 0; i < map.length; i++) {
int l = 0;
int c = 0;
int s = 0;
for(int j = 0; j < 5; j++) {
if(l == map[i][j]) {
c++;
}
else if(c >= 3){
for(int k = s; k < c+s; k++) {
map[i][k] = 0;
}
c = 1;
s = j;
}
else {
c = 1;
s = j;
}
l = map[i][j];
if(j == 4 && c >= 3) {
for(int k = s; k < c+s; k++) {
map[i][k] = 0;
}
}
}
}
}
public static void drop() {
for(int i = map.length; i >= 0; i--) {
for(int j = 0; j < 5; j++) {
int count = i;
while(true) {
if(count < 2) break;
if(map[i-1][j] == 0) {
map[i-1][j] = map[count-2][j];
map[count-2][j] = 0;
count--;
}
else {
break;
}
}
}
}
}
}
``` |
### Prompt
Please formulate a cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int erase(vector<vector<int>>& bd){
for(int i=0;i<bd.size();i++){
for(int j=0;j<3;j++){
if( bd[i][j]%10 == bd[i][j+1]%10 && bd[i][j+1]%10 == bd[i][j+2]%10 ){
bd[i][j] += 10;
bd[i][j+1] += 10;
bd[i][j+2] += 10;
}
}
}
int points = 0;
for(int i=0;i<bd.size();i++){
for(int j=0;j<5;j++){
if(bd[i][j] >= 10){
points += bd[i][j]%10;
bd[i][j] = 0;
}
}
}
return points;
}
void drop(vector<vector<int>>& bd){
bool update = true;
while(update){
update = false;
for(int i=1;i<bd.size();i++){
for(int j=0;j<5;j++){
if(bd[i][j] == 0 && bd[i-1][j] != 0){
bd[i][j] = bd[i-1][j];
bd[i-1][j] = 0;
update = true;
}
}
}
}
}
bool solve(){
int H;
cin >> H;
if(H == 0)return false;
vector<vector<int>> bd(H,vector<int>(5));
for(int i=0;i<H;i++){
for(int j=0;j<5;j++){
cin >> bd[i][j];
}
}
int ret = 0,p;
while((p = erase(bd)) != 0){
ret += p;
drop(bd);
}
cout << ret << endl;
return true;
}
int main(void){
while(solve()){
}
return 0;
}
``` |
### Prompt
Your challenge is to write a PYTHON3 solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
from functools import lru_cache
def DEBUG(*args): pass # print('@', *args)
@lru_cache(maxsize=None)
def pat(s, n):
return ' '.join([s] * n)
DEBUG(pat('1', 3))
def removeAll(xs, s):
while s in xs:
xs.remove(s)
xs.append('#')
return xs
def lmap(f, s): return list(map(f, s))
digits = lmap(str, range(1, 10))
def main(n):
data = '\n'.join(map(lambda x: input(), range(n)))
score = 0
DEBUG(data)
while True:
removed = False
sum1 = sum(map(lambda x: 0 if x == '#' else int(x), data.split()))
for d in digits:
if pat(d, 3) in data:
data = data.replace(pat(d, 5), pat('0', 5))
data = data.replace(pat(d, 4), pat('0', 4))
data = data.replace(pat(d, 3), pat('0', 3))
removed = True
if removed == False:
break
DEBUG(data)
score += (sum1 - sum(map(lambda x: 0 if x == '#' else int(x), data.split())))
data = zip(*map(lambda x: x.split(), data.split('\n')))
data = map(lambda x: list(x[::-1]), data)
data = map(lambda x: removeAll(x, '0'), data)
data = lmap(lambda s: ' '.join(s), zip(*data))
data = '\n'.join(data[::-1])
DEBUG(data)
DEBUG(data)
print(score)
while True:
n = int(input())
if n == 0: break
main(n)
``` |
### Prompt
Please provide a PYTHON3 coded solution to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
while True:
h = int(input())
if h==0:
break
puyo = [list(map(int, input().split())) for _ in range(h)]
p = 0
fin = 0
turn = 1
while fin==0:
#[print(*puyo[i]) for i in range(h)]
fin = 1
for i in range(h):
cnt = 1
for j in range(1, 5):
if puyo[i][j-1] == puyo[i][j] and puyo[i][j]>0:
cnt += 1
else:
if cnt>=3:
p += cnt * puyo[i][j-1]
for k in range(cnt):
puyo[i][j-k-1] = -1
fin = 0
cnt = 1
if j==4:
if cnt>=3:
p += cnt * puyo[i][j-1]
for k in range(cnt):
puyo[i][j-k] = -1
fin = 0
#print('after remove')
#[print(*puyo[i]) for i in range(h)]
for i in range(5):
d_fin = 0
while d_fin==0:
d_fin = 1
for j in range(h-1, 0, -1):
if puyo[j][i]==-1 and puyo[j-1][i]>0:
puyo[j][i] = puyo[j-1][i]
puyo[j-1][i] = -1
d_fin = 0
#print(f'turn {turn}')
turn += 1
print(p)
``` |
### Prompt
Develop a solution in cpp to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
void p(int ma[][5],int h){
cout<<endl;
for(int i=0;i<h;i++){
for(int j=0;j<5;j++){
if(j)cout<<" ";
cout<<ma[i][j];
}
cout<<endl;
}
}
int main(){
int h;
int ma[10][5];
while(cin>>h,h){
for(int i=0;i<h;i++){
for(int j=0;j<5;j++)
cin>>ma[i][j];
}
int ans=0;
int flag=1;
while(flag){
flag=0;
for(int x=0;x<h;x++){
for(int i=0;i<3;i++){
if(ma[x][i]==0)continue;
int k=1;
for(int j=i+1;j<5;j++){
if(ma[x][j]==ma[x][j-1])k++;
else break;
}
if(k>=3){
flag=1;
for(int j=0;j<k;j++){
ans+=ma[x][i+j];
ma[x][i+j]=0;
}
break;
}
}
}
// p(ma,h);
for(int i=0;i<5;i++){
for(int j=h-1;j>=0;j--){
if(ma[j][i]==0){
//cout<<"a"<<endl;
for(int k=j;k>=0;k--){
if(ma[k][i]!=0){
swap(ma[j][i],ma[k][i]);
break;
}
}
}
}
}
// p(ma,h);
}
cout<<ans<<endl;
}
return 0;
}
``` |
### Prompt
Generate a python3 solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
while True:
score = 0
h = int(input())
if h == 0: break
w = 5
field = [list(map(int, input().split())) for x in range(h)]
def delete(field):
flg = True
for row in field:
target = None
cnt = 0
fs = []
f = None
t = None
for i, cell in enumerate(row):
if target != None and cell == target:
cnt += 1
else:
if cnt >= 2:
f = fs[-1]
t = i - 1
fs.append(i)
target = cell
cnt = 0
if cnt >= 2:
f = fs[-1]
t = i
if f != None:
global score
score += (t - f + 1) * row[f]
for i in range(f, t + 1):
row[i] = None
flg = False
if flg: return flg
return field
def drop(field):
for y, row in enumerate(field[:-1]):
for x, cell in enumerate(row):
if field[y + 1][x] == None:
for n in range(y + 1, 0, -1):
field[n][x] = field[n - 1][x]
else:
field[0][x] = None
return field
while True:
field = delete(field)
if field == True:
break
field = drop(field)
# for x in field: print(*map(lambda x: "X" if x == None else x, x))
# print("===========")
print(score)
``` |
### Prompt
Develop a solution in Cpp to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;
int g[11][5];
bool used[11][5];
int main(){
int h;
while(cin>>h,h){
int s=0;
rep(i,h)rep(j,5) cin>>g[i][j];
while(1){
bool update=false;
rep(i,h){
int cnt=1;
FOR(j,1,5){
if(g[i][j]!=0&&g[i][j]==g[i][j-1]) ++cnt;
else{
if(cnt>=3){
s+=g[i][j-1]*cnt,update=true;
rep(k,cnt) g[i][j-1-k]=0;
}
cnt=1;
}
}
if(cnt>=3){
s+=g[i][4]*cnt,update=true;
rep(k,cnt) g[i][4-k]=0;
}
}
if(!update) break;
rep(j,5){
int cnt=0;
rep(i,h){
if(g[h-i-1][j]!=0){
swap(g[h-cnt-1][j],g[h-i-1][j]);
++cnt;
}
}
}
}
cout<<s<<endl;
}
return 0;
}
``` |
### Prompt
Please formulate a cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <iostream>
using namespace std;
static const int N = 10;
int main(){
int H,i,j,k,l,com,cnt,start,score,flag;//com=compare
char c;
string str[N],cp[N];
while(1){
score=0;
cin >> H;
if(H==0) break;
for(i=0;i<H;i++){
str[i]="";
for(j=0;j<5;j++){
cin >> c;
str[i]+=c;
}
}
while(1){
for(i=0;i<H;i++){
cnt=0;
com=str[i][0];
start=0;
for(j=0;j<5;j++){
if(com==str[i][j]) cnt++;
else{
if(cnt>=3){
if(com!='e'){
score+=cnt*(com-'0');
}
for(k=start;k<=j-1;k++){
str[i][k]='e';//e=end
}
com=str[i][j];
start=j;
cnt=1;
}else{
com=str[i][j];
start=j;
cnt=1;
}
}
}
if(cnt>=3){
if(com!='e'){
score+=cnt*(com-'0');
}
for(k=start;k<=j;k++){
str[i][k]='e';//e=end
}
}
}
for(i=0;i<5;i++){
for(j=H-1;j>=0;j--){
if(j-1>=0&&str[j][i]=='e'){
l=j;
while(1){
l--;
if(l==-1) break;
if(str[l][i]!='e'){
str[j][i]=str[l][i];
str[l][i]='e';
break;
}
}
}
}
}
flag=0;
for(i=0;i<H;i++) if(cp[i]!=str[i]) flag=1;
if(flag==0) break;
for(i=0;i<H;i++) cp[i]=str[i];
}
cout << score << endl;
}
return 0;
}
``` |
### Prompt
Your challenge is to write a JAVA solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```java
import java.util.*;
public class Main {
public Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
new Main();
}
public Main() {
new aoj1193().doIt();
}
class aoj1193{
int a[][];
int point(int n){
int result = 0;
for(int i = 0;i < n;i++){
if(a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][2] == a[i][3] && a[i][3] == a[i][4] && a[i][0] != 0){
result = result + (a[i][0] * 5);
for(int j = 0;j < 5;j++)a[i][j] = 0;
}
for(int j = 0;j < 2;j++){
if(a[i][j] == a[i][j+1] && a[i][j+1] == a[i][j+2] && a[i][j+2] == a[i][j+3] && a[i][j] != 0){
result = result + (a[i][j] * 4);
for(int k = j;k < j + 4;k++)a[i][k] = 0;
}
}
for(int j = 0;j < 3;j++){
if(a[i][j] == a[i][j+1] && a[i][j+1] == a[i][j+2] && a[i][j] != 0){
result = result + (a[i][j] * 3);
for(int k = j;k < j + 3;k++)a[i][k] = 0;
}
}
}
return result;
}
void sort(int n){
while(true){
boolean s = false;
for(int i = n-1;i > 0;i--){
for(int j = 0;j < 5;j++){
if(a[i][j] == 0 && a[i-1][j] != 0){
a[i][j] = a[i-1][j];
a[i-1][j] = 0;
s = true;
}
}
}
if(!s)break;
}
}
void doIt() {
while(true){
int n = sc.nextInt();
if(n == 0)break;
int cnt = 0;
a = new int [n][5];
for(int i = 0;i < n;i++){
for(int j = 0;j < 5;j++){
a[i][j] = sc.nextInt();
}
}
while(true){
int num = point(n);
cnt = cnt + num;
if(num == 0)break;
sort(n);
}
System.out.println(cnt);
}
}
}
}
``` |
### Prompt
Your task is to create a CPP solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<iostream>
using namespace std;
int h,a[11][5];
int main()
{
while(cin>>h,h)
{
for(int i=0;i<h;i++)for(int j=0;j<5;j++)cin>>a[i][j];
int ans=0;
bool flag=1;
while(flag)
{
flag=0;
for(int i=0;i<h;i++)
{
for(int j=0;j<3;j++)
{
if(a[i][j]&&a[i][j]==a[i][j+1]&&a[i][j]==a[i][j+2])
{
ans+=a[i][j]*3;
flag=1;
if(j+3<5&&a[i][j]==a[i][j+3])
{
ans+=a[i][j],a[i][j+3]=0;
if(j+4<5&&a[i][j]==a[i][j+4])ans+=a[i][j],a[i][j+4]=0;
}
a[i][j]=a[i][j+1]=a[i][j+2]=0;
}
}
}
for(int j=0;j<5;j++)
{
for(int i=h;i-->0;)
{
if(a[i][j]==0)
{
int k;
for(k=i-1;k>=0&&a[k][j]==0;k--);
if(k<0)break;
a[i][j]=a[k][j];
a[k][j]=0;
}
}
}
}
cout<<ans<<endl;
}
}
``` |
### Prompt
In cpp, your task is to solve the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main()
{
for(int h;cin>>h && h;){
vector<vector<int>> a(h,vector<int>(5));
rep(i,h) rep(j,5) cin>>a[i][j];
int res=0;
for(;;){
int add=0;
for(auto& b:a) for(int j=0;j<5;){
if(b[j]==0){
j++;
continue;
}
int k=j;
while(k<5 && b[k]==b[j]) k++;
if(k-j>=3){
add+=(k-j)*b[j];
fill(begin(b)+j,begin(b)+k,0);
}
j=k;
}
if(add==0) break;
res+=add;
rep(i,5) for(int j=h,k=h;k--;)
if(a[k][i]) swap(a[--j][i],a[k][i]);
}
cout<<res<<endl;
}
}
``` |
### Prompt
Develop a solution in CPP to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <iostream>
using namespace std;
const int H = 10, W = 5;
int board[H][W];
int main(){
int h, score, inc;
while(cin >> h, h){
inc = 1;
score = 0;
for(int i = 0;i < h;++i){
for(int j = 0;j < W;++j){
cin >> board[i][j];
}
}
while(inc){
int c, k;
inc = 0;
for(int i = 0;i < h;++i){
for(int j = 0;j < W;++j){
c = 0;
for(int k = j;k < W && board[i][j] == board[i][k];++k){
++c;
}
if(c >= 3){
inc += board[i][j] * c;
for(int k = j;k < j + c;++k){
board[i][k] = 0;
}
}
j += (c - 1);
}
}
for(int i = 1;i < h;++i){
for(int j = 0;j < W;++j){
if(!board[i][j]){
for(int k = i - 1;0 <= k;--k){
board[k + 1][j] = board[k][j];
}
board[0][j] = 0;
}
}
}
score += inc;
}
cout << score << endl;
}
return 0;
}
``` |
### Prompt
Please create a solution in cpp to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<iostream>
using namespace std;
int main(){
while(true){
int H;
cin>>H;
if(H==0)
break;
int S[10][5];
for(int y=0;y<H;y++)
for(int x=0;x<5;x++)
cin>>S[y][x];
int sco=0;
while(true){
bool flg =false;
for(int y=0;y<H;y++){
int m=S[y][2];
if(m==0)
continue;
if(S[y][0]==m && S[y][1]==m){
sco += m*3;
S[y][0]=S[y][1]=S[y][2]=0;
flg=true;
if(S[y][3]==m){
sco += m;
S[y][3]=0;
if(S[y][4]==m){
sco += m;
S[y][4]=0;
}
}
}
else if(S[y][1]==m&&S[y][3]==m){
sco += m*3;
S[y][1]=S[y][2]=S[y][3]=0;
flg=true;
if(S[y][4]==m){
sco += m;
S[y][4]=0;
}
}
else if(S[y][4]==m&&S[y][3]==m){
sco += m*3;
S[y][2]=S[y][3]=S[y][4]=0;
flg=true;
}
}
if(!flg)
break;
for(int x=0;x<5;x++){
for(int y=H-1;y>=0;y--){
if(S[y][x]!=0)
continue;
for(int y2=y-1;y2>=0;y2--){
if(S[y2][x]!=0){
S[y][x]=S[y2][x];
S[y2][x]=0;
break;
}
}
}
}
}
cout<<sco<<endl;
}
return 0;
}
``` |
### Prompt
Please provide a Python3 coded solution to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
while True:
h = int(input())
if h == 0:
break
stones = [list() for i in range(h)]
for i in range(h):
stones[i] = list(map(int, input().split()))
ans = 0
while True:
# 石を消してスコアを計算
tmp = 0
for y in range(h):
for j in range(3):
num = stones[y][j]
count = 0
for k in range(j, 5):
if num == stones[y][k]:
count += 1
else:
break
if count >= 3:
for k in range(j, j+count):
stones[y][k] = 0
tmp += count * num
break
if tmp == 0:
break
ans += tmp
# 石を落下させる
for x in range(5):
settle = h-1
for y in range(h-1, -1, -1):
if stones[y][x] != 0:
tmp = stones[y][x]
stones[y][x] = 0
stones[settle][x] = tmp
settle -= 1
print(ans)
``` |
### Prompt
Your task is to create a Java solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```java
import java.util.Scanner;
public class Main {
int[][] cell;
int w, h;
int remove(int k) {
int res = 0;
int cnt = 0;
for (int i = 0; i < 3; i++) {
int a = Math.abs(cell[k][i]);
int b = Math.abs(cell[k][i + 1]);
int c = Math.abs(cell[k][i + 2]);
if (a != 0 && a == b && b == c) {
res = a;
cell[k][i] = -a;
cell[k][i + 1] = -b;
cell[k][i + 2] = -c;
}
}
for (int i = 0; i < w; i++) {
if (cell[k][i] < 0) {
cnt++;
cell[k][i] = 0;
}
}
return Math.max(0, res * cnt);
}
void refresh(int k) {
int id = h - 1;
for (int i = h - 1; 0 <= i; i--) {
if (cell[i][k] != 0) {
cell[id--][k] = cell[i][k];
}
}
for (int i = id; 0 <= i; i--) {
cell[i][k] = 0;
}
}
void run() {
Scanner sc = new Scanner(System.in);
while (true) {
w = 5;
h = sc.nextInt();
if (h == 0) {
break;
}
cell = new int[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cell[i][j] = sc.nextInt();
}
}
boolean flg = true;
int score = 0;
while (flg) {
flg = false;
for (int i = 0; i < h; i++) {
int tmp = remove(i);
if (0 < tmp) {
score += tmp;
flg = true;
}
}
for (int i = 0; i < w; i++) {
refresh(i);
}
}
System.out.println(score);
}
}
public static void main(String[] args) {
new Main().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------" + '\n');
}
}
``` |
### Prompt
Create a solution in cpp for the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)
#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
typedef long long ll;
typedef pair<int, int> PII;
const double EPS = 1e-10;
const int H = 10;
const int W = 5;
int h;
int c[H][W];
int main() {
int i, j, k, l;
int res;
while (1) {
scanf("%d ", &h);
if (!h) break;
for (i=0; i<h; i++)
for (j=0; j<W; j++)
scanf("%d ", &(c[i][j]));
res = 0;
bool update = true;
while (update) {
update = false;
for (i=h-1; i>=0; i--) {
for (j=0; j<3; j++) {
k = 1;
while (c[i][j]!=0 && j+k<W && c[i][j+k]==c[i][j]) k++;
if (k >= 3) {
res += k * c[i][j];
for (l=j; l<j+k; l++) c[i][l] = 0;
update = true;
break;
}
}
}
for (i=h-2; i>=0; i--)
for (j=0; j<W; j++) {
int d = i;
while (d+1<h && c[d+1][j]==0) d++;
if (i!=d) {
c[d][j] = c[i][j];
c[i][j] = 0;
}
}
}
printf("%d\n", res);
}
return 0;
}
``` |
### Prompt
Your task is to create a Cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<iostream>
#include<string.h>
using namespace std;
int main(){
while(1){
int h;
cin >> h;
if(h == 0) break;
int puzle[10][5];
int reffer [10][5];
memset(puzle,0,sizeof(puzle));
for(int i=0;i < h;i++){
for(int j = 0;j < 5;j++){
cin >> puzle[i][j];
reffer[i][j] = i;
}
}
int point = 0;
while(true){
int lastpoint = point;
for(int i=0;i < h;i++){
for(int j = 0;j < 5;j++){
if(reffer[i][j] == -1) continue;
int k;
int left = puzle[reffer[i][j]][j];
for(k = 1;j+k < 5 && left == puzle[reffer[i][j+k]][j+k];k++){
}
if(k >= 3){
point += k*left;
for(int p = i;p >= 0;p--){
for(int q = j;q < j+k;q++){
if(p == 0){
reffer[p][q] = -1;
}
else{
reffer[p][q] = reffer[p-1][q];
}
}
}
j += k;
}
}
}
if(lastpoint == point) break;
}
cout << point << endl;
}
return 0;
}
``` |
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
int h,b[10][5],a,e;
int main(){
while(scanf("%d",&h),h){
a=0;
memset(b,0,sizeof(b));
rep(i,h)rep(j,5)scanf("%d",b[i]+j);
e=0;
while(!e){
e=1;
for(int i=h-1;i>=0;i--)rep(j,3){
int k=j;
if(b[i][j]){
while(k+1<5&&b[i][j]==b[i][k+1])k++;
if(k-j>=2){
a+=(k-j+1)*b[i][j];
for(int v=j;v<=k;v++)b[i][v]=0;
}
}
}
for(int i=h-1;i>=0;i--)rep(j,5)if(!b[i][j])for(int k=i-1;k>=0;k--)if(b[k][j]){
b[i][j]=b[k][j];b[k][j]=0;e=0;break;
}
}
printf("%d\n",a);
}
}
``` |
### Prompt
Please formulate a cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> mt;
ll h;
mt f;
int main(){
while(cin >> h){
if(h == 0) return 0;
f = mt(h+1,vl(6));
ll ans = 0;
for(ll i = 1;i <= h;i++){
for(ll j = 0;j < 5;j++) cin >> f[i][j];
}
bool flag;
do{
flag = false;
for(ll i = 1;i <= h;i++){
ll cnt = 0;
ll prev = 0;
for(ll j = 0;j <= 5;j++){
if(f[i][j] == prev) cnt++;
else{
if(cnt >= 3 && prev != 0){
flag = true;
// cerr << cnt << " " << prev << " " << j << " " << i << endl;
for(ll k = j-1,l = 0;l < cnt;l++,k--){
ans += f[i][k];
f[i][k] = 0;
}
}
prev = f[i][j];
cnt = 1;
}
}
}
for(ll s = 0;s <= h+5;s++){
for(ll i = h;i > 0;i--){
for(ll j = 0;j < 5;j++){
if(f[i][j] == 0) swap(f[i][j],f[i-1][j]);
}
}
}
// for(ll i = 1;i <= h;i++){
// for(ll j = 0;j < 5;j++){
// cerr << f[i][j] << " ";
// }
// cerr << endl;
// }
}while(flag);
cout << ans << endl;
}
}
``` |
### Prompt
Create a solution in JAVA for the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```java
import java.util.Arrays;
import java.util.Scanner;
public class Main {
Scanner sc;
int ex(int src, int ratio) {
return (src * (100 + ratio))/100;
}
void run() {
for (; ; ) {
int h = ni();
if(h==0) {
break;
}
int[][] f = new int[h][5];
for(int i = 0; i < h; ++i) {
for(int j = 0; j < 5; ++j) {
f[i][j] = ni();
}
}
int score = 0;
for(;;) {
int cnt = 0;
for(int i = 0; i < h; ++i) {
for(int j = 0; j < 3; ++j) {
int v = f[i][j];
boolean flag = true;
for(int k = 0; k < 3; ++k) {
flag &= f[i][j + k] == v;
}
if(flag) {
for(int k = j; k < 5; ++k) {
if(f[i][k] != v) {
break;
}
cnt += v;
for(int l = i; l > 0; --l) {
f[l][k] = f[l-1][k];
}
f[0][k] = 0;
}
break;
}
}
}
if(cnt == 0) {
break;
}
score += cnt;
}
System.out.println(score);
}
}
Main() {
sc = new Scanner(System.in);
}
int ni() {
return sc.nextInt();
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... os) {
System.err.println(Arrays.deepToString(os));
}
}
``` |
### Prompt
Develop a solution in CPP to the problem described below:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXH = 11;
const int W = 5;
int H;
int G[MAXH][W+1];
int main() {
while(cin >> H && H) {
for(int i = 0; i < H; ++i) {
for(int j = 0; j < W; ++j) {
cin >> G[i][j];
}
}
int score = 0;
bool update;
do {
update = false;
for(int i = 0; i < H; ++i) {
for(int j = 0, k = 0; j <= W; ++j) {
if(G[i][k] != G[i][j]) {
if(G[i][k] && j-k >= 3) {
update = true;
score += (j-k) * G[i][k];
while(k < j) G[i][k++] = 0;
}
k = j;
}
}
}
for(int j = 0; j < W; ++j) {
for(int i = H-1, k = H-1; i >= 0; --i) {
if(G[i][j]) {
int tmp = G[i][j];
G[i][j] = 0;
G[k--][j] = tmp;
}
}
}
} while(update);
cout << score << endl;
}
return 0;
}
``` |
### Prompt
Please create a solution in CPP to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<cstdio>
#include<cstring>
#include<algorithm>
int h;
int ans;
int a[11][6];
bool fall_and_count()
{
bool did=0;
for(int i=1;i<=h;i++)
{
int val=-1,sum=0;
int j;
for(j=1;j<=5;j++)
{
if(a[i][j]!=a[i][j-1])
{
if(sum>=3 && val!=-1)
{
for(int k=j-1;k>=j-sum;k--) a[i][k]=-1;
ans+=val*sum;
did=1;
}
val=a[i][j];
sum=1;
} else sum++;
}
if(sum>=3 && val!=-1)
{
for(int k=5;k>=j-sum;k--) a[i][k]=-1;
ans+=val*sum;
did=1;
}
}
return did;
}
void summarize()
{
for(int i=1;i<=5;i++)
{
int pos=0;
for(int j=1;j<=h;j++)
{
if(a[j][i]!=-1)
{
a[++pos][i]=a[j][i];
}
}
for(int j=pos+1;j<=h;j++) a[j][i]=-1;
}
}
int main()
{
while(~scanf("%d",&h) && h)
{
ans=0;
for(int i=h;i>=1;i--)
{
for(int j=1;j<=5;j++)
{
scanf("%d",&a[i][j]);
}
}
while(fall_and_count())
{
summarize();
// for(int i=h;i>=1;i--)
// {
// for(int j=1;j<=5;j++)
// {
// printf("%d\t",a[i][j]);
// }
// printf("\n");
// }
// printf("\n\n");
}
printf("%d\n",ans);
}
}
``` |
### Prompt
Create a solution in Cpp for the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int H;
char mas[10][5];
while( cin >> H, H){
for(int i = 0; i < H; i++){
for(int j = 0; j < 5; j++){
cin >> mas[i][j];
}
}
int ret = 0;
bool changed = true;
for(int foo = 0; foo < 8; foo++){
for(int q = 0; q < 8; q++){
for(int i = H - 1; i > 0; i--){
for(int j = 0; j < 5; j++){
if(mas[i][j] == '*'){
swap(mas[i][j],mas[i - 1][j]); //上から持ってくる
}
}
}
}
for(int i = 0; i < H; i++){
char prev = mas[i][0];
int cnt = 0;
for(int j = 0; j < 5; j++){
if(mas[i][j] == '*'){
cnt = 0;
continue;
}
if(mas[i][j] != prev){
prev = mas[i][j];
cnt = 0;
}
cnt++;
if(cnt == 3){
ret += (mas[i][j] - '0') * 3;
for(int k = j, l = 0; l < cnt; l++, k--){
mas[i][k] = '*';
}
} else if(cnt > 3){
ret += (mas[i][j] - '0');
mas[i][j] = '*';
}
}
}
}
cout << ret << endl;
}
}
``` |
### Prompt
Please formulate a PYTHON3 solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
while True:
N = int(input())
if not N: break
cells = [[int(x) for x in input().split()] for _ in range(N)]
point = 0
while True:
flag = False
for i, cell in enumerate(cells):
j = 0
while j < 5:
s, color = j, cell[j]
while j < 5 and cell[j] == color:
j += 1
if color != 0 and j - s >= 3:
point += color * (j - s)
cell[s:j] = [0] * (j-s)
flag = True
for i in range(5):
for j in reversed(range(N)):
s = j
while s >= 0 and not cells[s][i]:
s -= 1
if s >= 0:
cells[j][i], cells[s][i] = cells[s][i], cells[j][i]
if not flag:
break
print(point)
``` |
### Prompt
Please create a solution in CPP to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <cstdio>
using namespace std;
int main(){
int h,tmp,filled,combo;
int last_score, score;
int a[10][5];
while(1){
score = 0;
last_score = -1;
scanf(" %d", &h);
if(h == 0){
break;
}
for(int i=0;i<h;i++){
for(int j=0;j<5;j++){
scanf(" %d", (a[i]+j));
}
}
while(last_score != score){ //while2 begin
last_score = score;
//erasing and scoring
for(int i=0;i<h;i++){
combo = 0;
for(int j=1;j<5;j++){
if(a[i][j] == a[i][j-1]){
combo ++;
}else{
if(combo >= 2){
for(int k=combo;k>=0;k--){
score += a[i][j-1-k];
a[i][j-1-k] = 0;
}
}
combo = 0;
}
}
//not DRY...
if(combo >= 2){
for(int k=combo;k>=0;k--){
score += a[i][4-k];
a[i][4-k] = 0;
}
}
}
//dropping
for(int j=0;j<5;j++){
filled = 0;
for(int i=h-1;i>=0;i--){
if(a[i][j] != 0){
tmp = a[i][j];
a[i][j] = 0;
a[h-1 - filled][j] = tmp;
filled ++;
}
}
}
//debug
/* printf("score:%d\n",score);
for(int i=0;i<h;i++){
for(int j=0;j<5;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
*/
} //while2 end
printf("%d\n",score);
}
return 0;
}
``` |
### Prompt
Please formulate a Cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<iostream>
using namespace std;
int map[10][5];
int h,w=5;
int chain(int y,int x,int p,int *cnt){
int ans=0;
if(x+1<w && map[y][x+1]==p &&p>0 ){
(*cnt)++;
ans=max(ans,chain(y,x+1,p,cnt));
}
if(*cnt>=3){
map[y][x]=0;
ans=*cnt;
return ans*p;
}
return ans;
}
void dlt(){
while(1){
int f=0;
for(int i=h-1;i>=0;i--)
for(int j=w-1;j>=0;j--){
if(map[i][j]==0 && i-1>=0)swap(map[i][j],map[i-1][j]);
else if(map[i][j]==0)map[i][j]=-1;
}
for(int i=0;i<h;i++)
for(int j=0;j<w;j++) if(map[i][j]==0)f=1;
if(f==0) break;
}
}
int main(){
while(1){
int ans=0,ansf=0;
int cnt;
cin>>h;
if(h==0) break;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>map[i][j];
while(1){
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
cnt=1;
ans+=chain(i,j,map[i][j],&cnt);
}
if(ansf==ans) break;
ansf=ans;
dlt();
}
cout<<ans<<endl;
}
return 0;
}
``` |
### Prompt
Construct a Cpp code solution to the problem outlined:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)
int main(){
while(1){
int h; cin >> h; if(h == 0) break;
vector<deque<int>> stone(5, deque<int>(h));
rep(j, h) rep(i, 5) cin >> stone[i][j]; // j = 0が天井, h-1が底
int ans = 0;
while(1){
int tmp = ans;
rep(j, h){ // 上から見る
int l = 0, r = 1; // [l, r) : 同じ数字が並ぶ区間
rep(i,4){
if(stone[i][j] == stone[i+1][j]){
r++;
}else{
if(r - l >= 3) break;
l = i + 1;
r = i + 2;
}
}
if(stone[l][j] != -1 and r - l >= 3){
FOR(i, l, r){
ans += stone[i][j];
stone[i].erase(stone[i].begin() + j); // 消して
stone[i].push_front(-1); // 上に詰める
}
}
}
if(tmp == ans) break;
}
cout << ans << endl;
}
}
``` |
### Prompt
Construct a cpp code solution to the problem outlined:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include "bits/stdc++.h"
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using ll=long long;
const int M=100010;
int x[12][5];
int erase(){ //まず、全体を見る.消去する数字を0にして、総得点を出力;
int a=2,ans=0,tmp;
rep(h,0,12){
rep(i,0,3){
if(x[h][i]&&x[h][i]==x[h][i+1]&&x[h][i+1]==x[h][i+2])
tmp=x[h][i],a++;
}
if(a>2){
rep(i,0,4)if(x[h][i]==tmp&&x[h][i]==x[h][i+1]){
x[h][i]=0;
if(i==3||x[h][i+1]!=x[h][i+2])x[h][i+1]=0;
}
ans+=a*tmp;
a=2;
}
}
return ans;
}
void fall(){ //上の段から探索し、一つ下の段が0なら落とす;
for(int i=11;i!=0;i--){
rep(j,0,5){
if(x[i][j]!=0&&x[i-1][j]==0){
x[i-1][j]=x[i][j];
x[i][j]=0;
if(x[i+1][j]!=0&&x[i][j]==0){
i+=2,j=-1;
}
}
}
}
}
int main(){
while(1){
int h,ans=0;
cin>>h;
if(h==0)break;
rep(i,0,12){
rep(j,0,5)x[i][j]=0;
}
for(int i=h-1;i>=0;i--){
rep(j,0,5)cin>>x[i][j];
}
int z=-1;
while(z!=0){
z=erase();
ans+=z;
fall();
// for(int i=11;i>=0;i--)
// rep(j,0,5){
// cout<<x[i][j];
// if(j==4)cout<<endl;
// }
//
// cout<<ans<<endl;
// cout<<"--------------"<<endl;
}
cout<<ans<<endl;
}
}
``` |
### Prompt
Please create a solution in PYTHON3 to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def resolve():
while True:
H = I()
if H==0:
break
else:
s = [LI() for _ in range(H)]
score = 0
while True:
# 消滅判定
score_tmp = 0
for i in range(H):
prev = 0
cnt = 1
for j in range(5):
if s[i][j]==prev:
cnt += 1
else:
if cnt>=3:
score_tmp += prev*cnt
for k in range(cnt):
s[i][j-cnt+k] = 0
prev = s[i][j]
cnt = 1
if cnt>=3:
score_tmp += prev*cnt
for k in range(cnt):
s[i][j+1-cnt+k] = 0
# print('s')
# for i in s:
# print(i)
if score_tmp==0:
break
score += score_tmp
# 落下処理
# その石がどれだけ落下するかの算出
drop = [[0]*5 for _ in range(H)]
for i in range(5):
for j in range(H-1):
drop[H-j-2][i] = drop[H-j-1][i] + (1 if s[H-j-1][i]==0 else 0)
# print('drop')
# for i in drop:
# print(i)
# 落下先にコピー
for i in range(5):
for j in range(H):
if drop[H-j-1][i]!=0:
s[H-j-1+drop[H-j-1][i]][i] = s[H-j-1][i]
s[H-j-1][i] = 0
# print('dropped s')
# for i in s:
# print(i)
print(score)
if __name__ == '__main__':
resolve()
``` |
### Prompt
In python3, your task is to solve the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
def main(h):
lst=[list(map(int,input().split())) for _ in range(h)]
check=1
sm=0
while check:
check=0
for i in range(h):
for j in range(3):
if lst[i][j]==0 : continue
if lst[i][j+1]!=lst[i][j] : continue
if lst[i][j+2]!=lst[i][j] : continue
sm+=3*lst[i][j]
for k in range(j+3,5):
if lst[i][k]==lst[i][j]:
lst[i][k]=0
sm+=lst[i][j]
else : break
lst[i][j:j+3]=[0,0,0]
check=1
for i in range(h-1,0,-1):
for j in range(5):
if lst[i][j]==0:
for k in range(i-1,-1,-1):
if lst[k][j]==0 : continue
lst[i][j]=lst[k][j]
lst[k][j]=0
break
print(sm)
#print(lst)
while 1:
h=int(input())
if h==0 : break
main(h)
``` |
### Prompt
Construct a PYTHON3 code solution to the problem outlined:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```python3
while True:
n = int(input())
if n == 0:
break
field = [input().split() for _ in range(n)]
field = list(reversed(field))
score = 0
while True:
f = True
for row in field:
clear_index = set()
for i in range(3):
if row[i] == -1:
continue
if row[i] == row[i+1] and row[i+1] == row[i+2]:
f = False
clear_index.add(i)
clear_index.add(i+1)
clear_index.add(i+2)
for i in clear_index:
score += int(row[i])
row[i] = -1
if f:
break
for i,row in enumerate(field):
for j in range(5):
if row[j] != -1:
continue
for k in range(i+1,n):
if field[k][j] != -1:
field[k][j],row[j] = row[j],field[k][j]
break
print(score)
``` |
### Prompt
In CPP, your task is to solve the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()
const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;
int main(){
int n;
while(cin >> n, n){
vector<vector<int> > v(n);
for(int i=0; i< n; i++){
for(int j=0; j < 5; j++){
int m; cin >> m; v[i].push_back(m);
}
}
int ret = 0;
while(true){
bool p = false;
for(int i=0; i < n; i++){
for(int j=0; j< 3; j++){
if(v[i][j] == -1) continue;
int count = 0;
while(v[i][j] == v[i][j+count] && j+count < 5) count++;
if(3 <= count){
p = true; ret += v[i][j]*count;
rep(k, count) v[i][j+k] = -1;
}
}
}
if(!p) break;
else {
for(int i=n-1; 1 <= i; i--){
for(int j=0; j< 5; j++){
if(v[i][j] == -1){
for(int k =0; 0 <= i-k; k++) if(v[i-k][j] != -1){
v[i][j] = v[i-k][j]; v[i-k][j] = -1; break;
}
}
}
}
}
}
cout << ret << endl;
}
}
``` |
### Prompt
Construct a CPP code solution to the problem outlined:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <iostream>
#define EMP -1000
using namespace std;
int stage[11][11]={};
int h;
void outStage(){
for(int i=0;i<h;i++){
for(int j=0;j<5;j++){
cout<<stage[i][j];
}
cout<<endl;
}
}
bool delStage(){
bool conti=false;
for(int i=0;i<h;i++){
for(int j=0;j<3;j++){
int samecount=1;
int now=-1;
for(int k=0;;k++){
if(now==-1){
now=stage[i][j+k];
}
else if(now==stage[i][j+k] && now!=EMP){
samecount++;
}
if( (now!=-1 && now!=stage[i][j+k]) || k==4){
if(samecount>=3){
conti=true;
for(int l=0;l<samecount;l++){
stage[i][j+l]=EMP;
}
samecount=1;
}
goto end;
}
}
end:;
}
}
return conti;
}
void downNum(){
for(int t=0;t<h;t++){
for(int i=1;i<h-t;i++){
for(int j=0;j<5;j++){
if( i-1>=0 && stage[i][j]==EMP){
swap(stage[i][j],stage[i-1][j]);
}
}
}
}
}
int countScore(){
int sum=0;
for(int i=0;i<h;i++){
for(int j=0;j<5;j++){
if(stage[i][j]!=EMP)
sum+=stage[i][j];
}
}
return sum;
}
int main(){
while(1){
int sum=0;
cin>>h;
if(h==0)break;
for(int i=0;i<h;i++){
for(int j=0;j<5;j++){
cin>>stage[i][j];
sum+=stage[i][j];
}
}
//end input;
while(1){
if(delStage()==false)break;
downNum();
}
cout<<sum-countScore()<<endl;
}
return 0;
}
``` |
### Prompt
Please formulate a cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
int n,a[11][5];
void solve()
{
while(cin>>n,n){
REP(i,n)REP(j,5)cin>>a[i][j];
int ans=0;bool flag=1;
while(flag){
flag=0;
REP(i,n){
int now=a[i][0],cnt=1,idx=0;
rep(j,1,5){
if(a[i][j]==a[i][j-1])cnt++;
else if(cnt<3)now=a[i][j],cnt=1,idx=j;
else break;
}
if(cnt>=3&&a[i][idx]!=0){
ans+=cnt*a[i][idx];
flag=1;
rep(j,idx,idx+cnt)a[i][j]=0;
}
}
REP(i,5)REP(j,n)if(a[j][i]==0){
for(int k=j;k>0;k--)swap(a[k][i],a[k-1][i]);
}
}
cout<<ans<<endl;
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
``` |
### Prompt
In Cpp, your task is to solve the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int map[60][5];
int main(void) {
vector<int> list;
for (;;) {
int h,score=0;
cin >> h;
if (!h)return 0;
int cnt = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < 5; j++) {
cin >> map[i][j];
}
}
bool update = true;
for (; update;) {
update = false;
for (int i = 0; i < h; i++) {
int pos = 0, num = 0, cnt = 0;
for (int j = 0; j < 5; j++) {
if (num != map[i][j]) {
if (cnt > 2 && num) {
for (int j = pos; j < pos + cnt; j++) {
map[i][j] = 0;
}
score += num*cnt;
update = true;
}
pos = j;
num = map[i][j];
cnt = 1;
}
else cnt++;
}
if (cnt > 2 && num) {
for (int j = pos; j < pos + cnt ; j++) {
map[i][j] = 0;
}
score += num*cnt;
update = true;
}
}
for (int j = 0; j < 5; j++) {
int pos = h - 1;
for (int i = h - 1; i >= 0; i--) {
if (map[i][j]) {
map[pos][j] = map[i][j];
if (pos != i)map[i][j] = 0;
pos--;
}
}
}
}
cout << score << endl;
}
return 0;
}
``` |
### Prompt
In CPP, your task is to solve the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
int t[100][6];
int ans = 0;
bool remove(){
bool ret = 0;
rep(i,20)rep(j,4){
if(t[i][j] != 0 && t[i][j] == t[i][j+1] && t[i][j+1] == t[i][j+2]){
//cout << t[i][j] << " " << t[i][j+1] << endl;
for(int jj = j + 1; t[i][jj] == t[i][j]; jj++){
ans += t[i][jj];
t[i][jj] = 0;
}
ans += t[i][j];
t[i][j] = 0;
ret = 1;
}
}
return ret;
}
void fall(){
rep(tt,50)rep(i,20)rep(j,5)if(t[i][j] == 0)t[i][j] = t[i+1][j],t[i+1][j] = 0;
}
int main(){
int x;
while(cin >> x, x){
ans = 0;
rep(i,100)rep(j,6)t[i][j] = 0;
rep(i,x)rep(j,5) cin >> t[x-i-1][j];
rep(i,500){
remove();
fall();
//rep(i,x){{rep(j,5)cout << t[i][j] << " ";}cout<<endl;}cout << endl;
}
cout << ans << endl;
}
}
``` |
### Prompt
Your task is to create a cpp solution to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
int board[10][5];
int main(){ _;
int h;
while(cin>>h,h!=0){
REP(i,10)REP(j,5)board[i][j]=0;
REP(i,h){
REP(j,5){
cin>>board[h-i-1][j];
}
}
int sum=0;
bool chain=false;
do{
chain=false;
REP(i,h){
REP(j,3){
if(board[i][j]==0)continue;
int k=j+1;
for(;k<5;k++){
if(board[i][j]!=board[i][k])break;
}
if(k-j>=3){
sum+=board[i][j]*(k-j);
for(;j<k;j++)
board[i][j]=0;
break;
}
}
}
REP(j,5){
REP(i,h-1){
if(board[i][j]==0){
int k=i+1;
for(;k<10;k++){
if(board[k][j]!=0){
board[i][j]=board[k][j];
board[k][j]=0;
chain=true;
break;
}
}
}
}
}
// REP(i,10){
// REP(j,5)
// cout<<board[i][j]<<" ";
// cout<<endl;
// }
// cout<<endl;
}while(chain);
cout<<sum<<endl;
}
}
``` |
### Prompt
Please create a solution in cpp to the following problem:
Chain Disappearance Puzzle
We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.
<image>
The puzzle proceeds taking the following steps.
1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
<image>
Input
The input consists of multiple datasets. Each dataset is formed as follows.
> Board height H
> Stone placement of the row 1
> Stone placement of the row 2
> ...
> Stone placement of the row H
>
The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72
Example
Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output
36
38
99
0
72
### Response
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
int stage[10][5];
void downstone(int h){
for(int i=h-1;i>=0;--i){
for(int j=0;j<5;++j){
if(stage[i][j]==0){
for(int k=i-1;k>=0;k--){
if(stage[k][j]){
swap(stage[k][j],stage[i][j]);
break;
}
}
}
}
}
return;
}
int deletestone(int h){
int res=0;
for(int i=h-1;i>=0;--i){
for(int j=0;j<5;++j){
int len=0;
for(int k=j;k<5;++k){
if(stage[i][j]==stage[i][k]) len++;
else break;
}
if(len>=3){
res+=stage[i][j]*len;
for(int k=0;k<len;++k) stage[i][j+k]=0;
break;
}
}
}
return res;
}
void show(int h){
for(int i=0;i<h;++i){
for(int j=0;j<5;++j) cout << stage[i][j] << " " ;
cout << endl;
}
cout << endl;
}
int main(void){
int h;
while(cin>>h){
if(h==0)
break;
for(int i=0;i<h;++i)for(int j=0;j<5;++j) cin >> stage[i][j];
int ans=0;
while(1){
int cur=deletestone(h);
if(cur==0) break;
ans+=cur;
//show(h);
downstone(h);
//show(h);
}
cout << ans << endl;
}
return 0;
}
``` |