2074病句の修正
説明
私たちは小学校、中学校の時、よく病気の文を修正する国語の問題をします.非常によく使われる記号の一つで、対変調記号と呼ばれ、記号や記号で表されています.ここで、第1のシンボルは、縦線の両端のコンテンツ交換を表し、第2のシンボルは、中間部分の位置が変わらず、両端のコンテンツ交換を表す.例:
実際には「cdab」に等しく、私たちは以下のように記しています:(ab*cd);
実際には「fghicdeab」と同等であり、(ab|cde|fghi)と記す.
文字列が複雑な上記の反転過程を経て、最終的な結果が何なのかを教えてくれるプログラムを書いてほしいです.
入力
入力された最初の行は、データのグループ数t(t<=100)の整数である.
各テストデータのセットは1行のみで、スペースは含まれず、長さは1000を超えない文字列です.文字列には、大文字と小文字、数字、および(*)(||)式のみが含まれます.2つの式の意味は、タイトルの説明に示されています.
カッコが一致することを保証し、カッコ内にアスタリスクがある場合はアスタリスクが1つしかなく、縦線がある場合は縦線が2つしかなく、1つのカッコ内にアスタリスクと縦線が同時に現れない可能性がありますが、同時に現れない可能性があります.すなわち,(a*bc|d|e)は不正であるが,(a*b(c|d|e))は合法であり,その結果bedcaとなる.
しゅつりょく
テストデータごとに1行出力し、文字列を変換した結果です.この結果には、大文字と小文字、数字のみが含まれます.
サンプル入力
5
(ab*cd)
(ab|cde|fghi)
(a*b(c|d|e))
(ab*c)defg
(ab*c(d|e|f))g(h*ij)
サンプル出力
cdab
fghicdeab
bedca
cabdefg
cfedabgijh
私たちは小学校、中学校の時、よく病気の文を修正する国語の問題をします.非常によく使われる記号の一つで、対変調記号と呼ばれ、記号や記号で表されています.ここで、第1のシンボルは、縦線の両端のコンテンツ交換を表し、第2のシンボルは、中間部分の位置が変わらず、両端のコンテンツ交換を表す.例:
実際には「cdab」に等しく、私たちは以下のように記しています:(ab*cd);
実際には「fghicdeab」と同等であり、(ab|cde|fghi)と記す.
文字列が複雑な上記の反転過程を経て、最終的な結果が何なのかを教えてくれるプログラムを書いてほしいです.
入力
入力された最初の行は、データのグループ数t(t<=100)の整数である.
各テストデータのセットは1行のみで、スペースは含まれず、長さは1000を超えない文字列です.文字列には、大文字と小文字、数字、および(*)(||)式のみが含まれます.2つの式の意味は、タイトルの説明に示されています.
カッコが一致することを保証し、カッコ内にアスタリスクがある場合はアスタリスクが1つしかなく、縦線がある場合は縦線が2つしかなく、1つのカッコ内にアスタリスクと縦線が同時に現れない可能性がありますが、同時に現れない可能性があります.すなわち,(a*bc|d|e)は不正であるが,(a*b(c|d|e))は合法であり,その結果bedcaとなる.
しゅつりょく
テストデータごとに1行出力し、文字列を変換した結果です.この結果には、大文字と小文字、数字のみが含まれます.
サンプル入力
5
(ab*cd)
(ab|cde|fghi)
(a*b(c|d|e))
(ab*c)defg
(ab*c(d|e|f))g(h*ij)
サンプル出力
cdab
fghicdeab
bedca
cabdefg
cfedabgijh
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
int t;
cin >> t;
for ( int e = 0; e < t; e++ )
{
//char a[ 1004 ];
string a;
char t1[ 1004 ];
char t2[ 1004 ];
char t3[ 1004 ];
cin >> a;
char s[ 1004 ];
bool flag;
bool se = true;
bool fk2 = false;
int k, k1, k2;
int p;
int top = -1;
for ( int i = 0; i < a.size(); i++ )
{
//cout << "xixi" << endl;
if ( a[ i ] != ')' )
{
se = true;
top++;
s[ top ] = a[ i ];
}
else
{
fk2 = false;
int j;
for ( j = top-1; s[j] != '('; j-- )
{
if ( s[ j ] == '*' )
{
flag = false;
k = j;
se = false;
}
else if ( s[ j ] == '|' )
{
se = false;
flag = true;
if ( fk2 == false )
{
k2 = j;
fk2 = true;
}
else
{
k1 = j;
}
}
}
p = j;
top++;
s[ top ] = ')';
if ( flag == false && se == false )
{
top--;
/*for ( int i = 0; i <= top; i++ )
cout << s[ i ];
cout << endl;*/
// cout << "xixi"<< p << endl;
// for ( int i = 0; i <= top; i++ )
// cout << s[ i ];
//cout << endl;
//cout << "t1" <<" " << p << endl;
for ( int j = p+1; j < k; j++ )
{
t1[ j-p-1 ] = s[ j ];
//cout << t1[ j-p-1 ];
}
//cout << "end" << endl;
//cout << "t2" << endl;
for ( int j = k+1; j <= top; j++ )
{
t2[ j-k-1 ] = s[ j ];
//cout << t2[ j-k-1 ];
}
//cout << "end" << endl;
for ( int j = p; j < top-k+p; j++ )
{
s[ j ] = t2[ j-p ];
}
//cout << "t3" << endl;
/*cout << "hehe1"<< endl;
for ( int i = 0; i <= top; i++ )
cout << s[ i ];*/
for ( int j = top-k+p; j < top-1; j++ )
{
s[ j ] = t1[ j-top+k-p ];
//cout << t1[ j-top+k-p ];
}
/*cout << "hehe"<< endl;
for ( int i = 0; i <= top; i++ )
cout << s[ i ];
cout << endl;*/
//cout << "end" << endl;
top= top-2;//cout << top << endl;
}
else if ( se == false )
{
top--;
for ( int j = p+1; j < k1; j++ )
{
t1[ j-p-1 ] = s[ j ];
}
for ( int j = k1 + 1; j < k2; j++ )
{
t2[ j-k1-1 ] = s[ j ];
}
for ( int j = k2+1; j <= top; j++ )
{
t3[ j-k2-1 ] = s[ j ];
}
for ( int j = p; j < top-k2+p; j++ )
{
s[ j ] = t3[ j-p ];
}
for ( int j = top-k2+p; j < top + p - k1 -1; j++ )
{
s[ j ] = t2[ j-top+k2-p];
}
for ( int j = top + p - k1 -1; j <= top - 3; j++ )
{
s[ j ] = t1[ j-top-p+k1+1 ];
}
top = top -3;
}
else
{
for ( int j = p; j < top; j++ )
{
s[ j ] = s[ j+1 ];
}
top = top - 2;
}
}
/*cout << "hehe" << s[ top ] << endl;
for ( int i = 0; i <= top; i++ )
cout << s[ i ];
cout << endl;*/
}
//cout << top << endl;
for ( int i = 0; i <= top; i++ )
cout << s[ i ];
cout << endl;
}
return 0;
}