10361 - Automatic Poetry

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream> using namespace std;
int n;
char s1[], s2[];
int pr1, pr2, pl1, pl2;
int main()
{
//ifstream cin("test.txt");
int i;
cin>>n;
cin.get();
while (n--)
{
cin.getline(s1, );
cin.getline(s2, );
for (i = ; s1[i] != '<'; i++)
cout<<s1[i];
pl1 = i;
for (i++; s1[i] != '>'; i++)
cout<<s1[i];
pr1 = i;
for (i++; s1[i] != '<'; i++)
cout<<s1[i];
pl2 = i;
for (i++; s1[i] != '>'; i++)
cout<<s1[i];
pr2 = i;
for (i++; s1[i]; i++)
cout<<s1[i];
cout<<endl;
for (i = ; s2[i] != '.'; i++)
cout<<s2[i];
for (i = pl2 + ; i < pr2; i++)
cout<<s1[i];
for (i = pr1 + ; i < pl2; i++)
cout<<s1[i];
for (i = pl1 + ; i < pr1; i++)
cout<<s1[i];
for (i = pr2 + ; s1[i]; i++)
cout<<s1[i];
cout<<endl; }
return ;
}

537 - Artificial Intelligence?

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream> using namespace std;
int n;
string s;
char res[][];
int main()
{
//ifstream cin("test.txt");
double U, I, P, scale;
bool fu, fi, fp;
cin>>n;
int k = , i, j, p;
cin.get();
while (n--)
{
fu = fi = fp = false;
p = ;
cout<<"Problem #"<<k++<<endl;
getline(cin, s, '\n');
for (i = ; s[i]; i++)
{
//if (i > 0 && s[i] == '=' &&(s[i - 1] == 'P' || s[i - 1] == 'I' || s[i - 1] == 'U'))
if (s[i] == '=')
{
for (i--, j = ; s[i] != 'V' && s[i] != 'A' && s[i] != 'W'; i++, j++)
res[p][j] = s[i];
res[p][j] = '\0';
p++;
}
}
for (i = ; i < ; i++)
{
int len = strlen(res[i]) - ;
scale = ;
if (!isdigit(res[i][len]))
{
if (res[i][len] == 'k')
scale = ;
else
if (res[i][len] == 'M')
scale = ;
else
scale = 0.001;
res[i][len] = '\0';
}
double tmp = atof(&res[i][]) * scale;
if (res[i][] == 'I')
{
fi = true;
I = tmp;
}
else
if (res[i][] == 'U')
{
fu = true;
U = tmp;
}
else
{
fp = true;
P = tmp;
}
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision();
if (fu && fi)
{
cout<<"P="<<U * I<<"W"<<endl;
}
else
if (fu && fp)
{
cout<<"I="<<P / U<<"A"<<endl;
}
else
{
cout<<"U="<<P / I<<"V"<<endl;
}
cout<<endl;
}
return ;
}

409 - Excuses, Excuses!

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream> using namespace std;
int k, e;
string words[];
string ex[];
int numex[]; bool cmp(const string str)
{
for (int i = ; i < k; i++)
if (str == words[i])
return true;
return false;
}
void calc()
{
string tmp;
int i, j, p, kmax = ;
memset(numex, , sizeof(numex));
for (i = ; i < e; i++)
{
j = ;
while (ex[i][j])
{
if (!isalpha(ex[i][j]))
{
j++;
continue;
}
tmp.clear();
while (isalpha(ex[i][j]))
{
tmp +=(ex[i][j++] | );
}
if (cmp(tmp))
{
numex[i]++;
}
}
if (numex[i] > kmax)
kmax = numex[i];
}
for (i = ; i < e; i++)
if (numex[i] == kmax)
cout<<ex[i]<<endl;
cout<<endl;
}
int main()
{
ifstream cin("test.txt");
int i, j, t = ;
while (cin>>k>>e)
{
cin.ignore();
for (i = ; i < k; i++)
getline(cin, words[i]);
for (i = ; i < e; i++)
getline(cin, ex[i]);
cout<<"Excuse Set #"<<t++<<endl;
calc();
}
return ;
}

10878 - Decode the tape

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream>
#include <cmath> using namespace std;
string str;
int main()
{
//ifstream cin("test.txt");
int s, p, i;
getline(cin, str);
while (getline(cin, str), str[] != '_')
{
s = p = ;
for (i = ; i > ; i--)
{
if (str[i] == '.')
continue;
if (str[i] == 'o')
s += pow(, p);
p++;
}
cout<<(char)s;
}
return ;
}

10815 - Andy's First Dictionary

#include <iostream>
#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
class cmp
{
public:
bool operator()(const string s1, const string s2)
{
return s1 < s2;
}
}; set<string, cmp> s;
int main()
{
freopen("test.txt", "r", stdin);
string tmp;
char ch = ;
while (ch != EOF)
{
ch = getchar();
if (!isalpha(ch))
continue;
tmp.clear();
while (true)
{
tmp += (ch | );
ch = getchar();
if (!isalpha(ch))
break;
}
s.insert(tmp);
}
for (set<string>::iterator it = s.begin(); it != s.end(); it++)
cout<<*it<<endl;
return ;
}

644 - Immediate Decodability

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
string set[];
bool cmp(const string s1, const string s2)
{
int i = ;
while (s1[i] && s2[i])
{
if (s1[i] != s2[i])
break;
i++;
}
if (!s1[i] || !s2[i])
return true;
return false;
}
int main()
{
//ifstream cin("test.in");
int n, i, j, t = ;
while (getline(cin, set[]))
{
cout<<"Set "<<t++<<" is ";
n = ;
while (getline(cin, set[n]), set[n][] != '')
n++;
for (i = ; i < n - ; i++)
{
for (j = i + ; j < n; j++)
{
if (cmp(set[i], set[j]))
break;
}
if (j < n)
break;
}
if (i < n - )
cout<<"not ";
cout<<"immediately decodable"<<endl;
}
return ;
}

10115 - Automatic Editing

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define N 15
using namespace std;
string f[N], re[N];
string edit;
int n, q;
bool cmp(int p)
{
int i, j, k, m;
for (i = q; i < n; i++)
{
for (m = p; edit[m]; m++)
{
for (j = , k = m; f[i][j] && edit[k]; j++, k++)
{
if (edit[k] != f[i][j])
break;
}
if (!f[i][j])//替换
{
edit.replace(edit.begin() + m, edit.begin() + k, re[i]);
if (i != q)
q = i;
return true;
}
} }
return false;
}
int main()
{
int i, j;
while (cin>>n, n)
{
cin.get();
q = ;
for (i = ; i < n; i++)
{
getline(cin, f[i]);
getline(cin, re[i]);
}
getline(cin, edit);
for (i = ; edit[i]; i++)
{
if (cmp(i))
i = -;
}
cout<<edit<<endl;
}
}
05-11 17:04