forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert all DOS line endings to Unix
- Loading branch information
Showing
147 changed files
with
9,196 additions
and
9,196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,131 @@ | ||
// 2008-06-16 | ||
#include <iostream> | ||
#include <cstring> | ||
#define MAX(a,b) ((a)>(b)?(a):(b)) | ||
#define MIN(a,b) ((a)<(b)?(a):(b)) | ||
#define MAXS 10000 | ||
using namespace std; | ||
struct bignum | ||
{ | ||
char* digits; | ||
int length; | ||
bignum(int size) | ||
{ | ||
length=1; | ||
digits=new char[size]; | ||
digits[0]=0; | ||
} | ||
bignum(int size,const bignum& x) //Copy constructor | ||
{ | ||
length=x.length; | ||
digits=new char[size]; | ||
memcpy(digits,x.digits,x.length); | ||
} | ||
bignum(int size,int x) | ||
{ | ||
length=0; | ||
digits=new char[size]; | ||
while (x>0) | ||
{ | ||
digits[length++]=x%10; | ||
x/=10; | ||
} | ||
if (length==0) {digits[0]=0; length=1;} | ||
} | ||
~bignum() | ||
{ | ||
delete digits; | ||
} | ||
void add(bignum& x) | ||
{ | ||
int l=MAX(length,x.length); | ||
int d; | ||
int carry=0; | ||
x.digits[x.length]=0; | ||
memset(digits+length,0,l-length+1); | ||
for (d=0; d<=l; d++) | ||
{ | ||
int sum=carry; | ||
if (d<length) | ||
sum+=digits[d]; | ||
if (d<x.length) | ||
sum+=x.digits[d]; | ||
digits[d]=sum; | ||
if (digits[d]>=10) {digits[d]-=10;carry=1;} else carry=0; | ||
} | ||
length=l; | ||
if (digits[length]) length++; | ||
if (length==0) {digits[0]=0; length=1;} | ||
} | ||
void sub(bignum& x) | ||
{ | ||
int d; | ||
int borrow=0; | ||
digits[length]=0; | ||
for (d=0; d<=length; d++) | ||
{ | ||
digits[d]-=borrow; | ||
if (d<x.length) | ||
digits[d]-=x.digits[d]; | ||
if (digits[d]<0) {digits[d]+=10;borrow=1;} else borrow=0; | ||
} | ||
while (length>0&&!digits[length-1]) length--; | ||
if (length==0) length=1; | ||
} | ||
void print() | ||
{ | ||
int i; | ||
for (i=length-1; i>=0; i--) | ||
putchar(digits[i]+48); | ||
} | ||
void operator=(const bignum& y) | ||
{ | ||
length=y.length; | ||
memcpy(digits,y.digits,y.length); | ||
} | ||
void operator=(const char* s) | ||
{ | ||
length=strlen(s); | ||
int i=length; | ||
while (i--) | ||
digits[i]=s[length-i-1]-48; | ||
} | ||
}; | ||
int main() | ||
{ | ||
int i,j,T; | ||
scanf("%d",&T); | ||
char s1[MAXS],s2[MAXS],s3[MAXS]; | ||
bignum n1(MAXS),n2(MAXS),n3(MAXS); | ||
for (j=0; j<T; j++) | ||
{ | ||
scanf("%s + %s = %s",&s1,&s2,&s3); | ||
//one of the numbers has a "machula" | ||
for (i=0; i<strlen(s1); i++) | ||
if (s1[i]=='m') | ||
{ | ||
n2=s2; | ||
n3=s3; | ||
n1=n3; | ||
n1.sub(n2); | ||
} | ||
for (i=0; i<strlen(s2); i++) | ||
if (s2[i]=='m') | ||
{ | ||
n1=s1; | ||
n3=s3; | ||
n2=n3; | ||
n2.sub(n1); | ||
} | ||
for (i=0; i<strlen(s3); i++) | ||
if (s3[i]=='m') | ||
{ | ||
n1=s1; | ||
n2=s2; | ||
n3=n1; | ||
n3.add(n2); | ||
} | ||
n1.print(); printf(" + "); n2.print(); printf(" = "); n3.print(); printf("\n"); | ||
} | ||
return 0; | ||
} | ||
// 2008-06-16 | ||
#include <iostream> | ||
#include <cstring> | ||
#define MAX(a,b) ((a)>(b)?(a):(b)) | ||
#define MIN(a,b) ((a)<(b)?(a):(b)) | ||
#define MAXS 10000 | ||
using namespace std; | ||
struct bignum | ||
{ | ||
char* digits; | ||
int length; | ||
bignum(int size) | ||
{ | ||
length=1; | ||
digits=new char[size]; | ||
digits[0]=0; | ||
} | ||
bignum(int size,const bignum& x) //Copy constructor | ||
{ | ||
length=x.length; | ||
digits=new char[size]; | ||
memcpy(digits,x.digits,x.length); | ||
} | ||
bignum(int size,int x) | ||
{ | ||
length=0; | ||
digits=new char[size]; | ||
while (x>0) | ||
{ | ||
digits[length++]=x%10; | ||
x/=10; | ||
} | ||
if (length==0) {digits[0]=0; length=1;} | ||
} | ||
~bignum() | ||
{ | ||
delete digits; | ||
} | ||
void add(bignum& x) | ||
{ | ||
int l=MAX(length,x.length); | ||
int d; | ||
int carry=0; | ||
x.digits[x.length]=0; | ||
memset(digits+length,0,l-length+1); | ||
for (d=0; d<=l; d++) | ||
{ | ||
int sum=carry; | ||
if (d<length) | ||
sum+=digits[d]; | ||
if (d<x.length) | ||
sum+=x.digits[d]; | ||
digits[d]=sum; | ||
if (digits[d]>=10) {digits[d]-=10;carry=1;} else carry=0; | ||
} | ||
length=l; | ||
if (digits[length]) length++; | ||
if (length==0) {digits[0]=0; length=1;} | ||
} | ||
void sub(bignum& x) | ||
{ | ||
int d; | ||
int borrow=0; | ||
digits[length]=0; | ||
for (d=0; d<=length; d++) | ||
{ | ||
digits[d]-=borrow; | ||
if (d<x.length) | ||
digits[d]-=x.digits[d]; | ||
if (digits[d]<0) {digits[d]+=10;borrow=1;} else borrow=0; | ||
} | ||
while (length>0&&!digits[length-1]) length--; | ||
if (length==0) length=1; | ||
} | ||
void print() | ||
{ | ||
int i; | ||
for (i=length-1; i>=0; i--) | ||
putchar(digits[i]+48); | ||
} | ||
void operator=(const bignum& y) | ||
{ | ||
length=y.length; | ||
memcpy(digits,y.digits,y.length); | ||
} | ||
void operator=(const char* s) | ||
{ | ||
length=strlen(s); | ||
int i=length; | ||
while (i--) | ||
digits[i]=s[length-i-1]-48; | ||
} | ||
}; | ||
int main() | ||
{ | ||
int i,j,T; | ||
scanf("%d",&T); | ||
char s1[MAXS],s2[MAXS],s3[MAXS]; | ||
bignum n1(MAXS),n2(MAXS),n3(MAXS); | ||
for (j=0; j<T; j++) | ||
{ | ||
scanf("%s + %s = %s",&s1,&s2,&s3); | ||
//one of the numbers has a "machula" | ||
for (i=0; i<strlen(s1); i++) | ||
if (s1[i]=='m') | ||
{ | ||
n2=s2; | ||
n3=s3; | ||
n1=n3; | ||
n1.sub(n2); | ||
} | ||
for (i=0; i<strlen(s2); i++) | ||
if (s2[i]=='m') | ||
{ | ||
n1=s1; | ||
n3=s3; | ||
n2=n3; | ||
n2.sub(n1); | ||
} | ||
for (i=0; i<strlen(s3); i++) | ||
if (s3[i]=='m') | ||
{ | ||
n1=s1; | ||
n2=s2; | ||
n3=n1; | ||
n3.add(n2); | ||
} | ||
n1.print(); printf(" + "); n2.print(); printf(" = "); n3.print(); printf("\n"); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,67 @@ | ||
// 2014-04-27 | ||
// modified 2014-10-07 | ||
#include <iostream> | ||
#include <string> | ||
#include <set> | ||
#include <vector> | ||
#include <sstream> | ||
#include <cstring> | ||
#include <cctype> | ||
using namespace std; | ||
int main() { | ||
for (;;) { | ||
int n; cin >> n; if (n == 0) return 0; | ||
set<string> insignificant; | ||
for (int i = 0; i < n; i++) { | ||
string s; cin >> s; insignificant.insert(s); | ||
} | ||
for (;;) { | ||
string acro; cin >> acro; cin.ignore(); | ||
string phrase; getline(cin, phrase); | ||
if (phrase == "CASE") break; | ||
istringstream iss(phrase); | ||
vector<string> words; | ||
for (;;) { | ||
string word; iss >> word; | ||
if (word == "") break; | ||
if (insignificant.find(word) == insignificant.end()) { | ||
words.push_back(word); | ||
} | ||
} | ||
// dp[i][j] is the number of ways to use the first i words and the | ||
// first j letters of the acronym | ||
int dp[151][151]; memset(dp, 0, sizeof(dp)); | ||
dp[0][0] = 1; | ||
int dp2[151][151]; | ||
for (int i = 0; i < words.size(); i++) { | ||
for (int j = 0; j < acro.length(); j++) { | ||
// How many letters of this word do we want to use? | ||
int mx = min(acro.length() - j, words[i].length()); | ||
for (int k = 0; k <= words[i].length(); k++) { | ||
dp2[k][0] = 1; | ||
} | ||
for (int k = 1; k <= mx; k++) { | ||
dp2[0][k] = 0; | ||
} | ||
for (int k = 0; k < words[i].length(); k++) { | ||
for (int m = 0; m < mx; m++) { | ||
dp2[k+1][m+1] = dp2[k][m+1]; | ||
if (words[i][k] == tolower(acro[j+m])) { | ||
dp2[k+1][m+1] += dp2[k][m]; | ||
} | ||
} | ||
} | ||
for (int k = 1; k <= mx; k++) { | ||
dp[i+1][j+k] += dp[i][j] * dp2[words[i].length()][k]; | ||
} | ||
} | ||
} | ||
if (dp[words.size()][acro.length()] == 0) { | ||
cout << acro << " is not a valid abbreviation" << endl; | ||
} else { | ||
cout << acro << " can be formed in " | ||
<< dp[words.size()][acro.length()] << " ways" << endl; | ||
} | ||
} | ||
} | ||
} | ||
// 2014-04-27 | ||
// modified 2014-10-07 | ||
#include <iostream> | ||
#include <string> | ||
#include <set> | ||
#include <vector> | ||
#include <sstream> | ||
#include <cstring> | ||
#include <cctype> | ||
using namespace std; | ||
int main() { | ||
for (;;) { | ||
int n; cin >> n; if (n == 0) return 0; | ||
set<string> insignificant; | ||
for (int i = 0; i < n; i++) { | ||
string s; cin >> s; insignificant.insert(s); | ||
} | ||
for (;;) { | ||
string acro; cin >> acro; cin.ignore(); | ||
string phrase; getline(cin, phrase); | ||
if (phrase == "CASE") break; | ||
istringstream iss(phrase); | ||
vector<string> words; | ||
for (;;) { | ||
string word; iss >> word; | ||
if (word == "") break; | ||
if (insignificant.find(word) == insignificant.end()) { | ||
words.push_back(word); | ||
} | ||
} | ||
// dp[i][j] is the number of ways to use the first i words and the | ||
// first j letters of the acronym | ||
int dp[151][151]; memset(dp, 0, sizeof(dp)); | ||
dp[0][0] = 1; | ||
int dp2[151][151]; | ||
for (int i = 0; i < words.size(); i++) { | ||
for (int j = 0; j < acro.length(); j++) { | ||
// How many letters of this word do we want to use? | ||
int mx = min(acro.length() - j, words[i].length()); | ||
for (int k = 0; k <= words[i].length(); k++) { | ||
dp2[k][0] = 1; | ||
} | ||
for (int k = 1; k <= mx; k++) { | ||
dp2[0][k] = 0; | ||
} | ||
for (int k = 0; k < words[i].length(); k++) { | ||
for (int m = 0; m < mx; m++) { | ||
dp2[k+1][m+1] = dp2[k][m+1]; | ||
if (words[i][k] == tolower(acro[j+m])) { | ||
dp2[k+1][m+1] += dp2[k][m]; | ||
} | ||
} | ||
} | ||
for (int k = 1; k <= mx; k++) { | ||
dp[i+1][j+k] += dp[i][j] * dp2[words[i].length()][k]; | ||
} | ||
} | ||
} | ||
if (dp[words.size()][acro.length()] == 0) { | ||
cout << acro << " is not a valid abbreviation" << endl; | ||
} else { | ||
cout << acro << " can be formed in " | ||
<< dp[words.size()][acro.length()] << " ways" << endl; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.